The fourth build of the PhpStorm 2021.2 Early Access Program comes with a set of new inspections and quick-fixes for PHP, including a whole new group of Refactoring Opportunities inspections.
Like other EAP builds, this build is free to use but it will expire 30 days after the build date.
New inspections and quick-fixes
Every PhpStorm release comes with many new inspections to help prevent possible bugs at an early stage while you’re in the editor and the code has not yet been committed.
We also add quick-fixes to free you from boring routine tasks and help you focus on what’s important in your coding.
So here are some of the new inspections coming in the 2021.2 release.
Condition can be replaced with `?->`
This inspection highlights expressions that can be replaced with the null safe operator from PHP 8. Press Alt+Enter to perform the transformation.
‘switch’ with common parts
If the cases of a switch block contain some repeated code, PhpStorm will suggest pulling it up. Use Alt+Enter to move the common part outside of the switch.
Create a function from undefined usage
When you’re designing code quickly, it can be convenient to make method stub calls and create the implementation later, once you are able to see the big picture.
If you type a non-existent method call inside a class, you can quickly create it by pressing Alt+Enter and selecting Add method:
For consistency, we have added a similar quick-fix to create a new function. Use Alt+Enter and select Add function:
Use constant from a class where it is defined
Let’s say there is a hierarchy of classes and interfaces and there is a constant defined in the top base class.
To access this constant, you can use any subclass or subinterface as an entry point. But doing so could make the code a bit confusing, as the constant is defined in some other place.
PhpStorm will highlight such constants and suggest replacing the name of the entity with the one where the constant is defined.
Redundant conditions in logical expressions
PhpStorm will now evaluate parts of conditions to identify any redundant ones.
Here is a simple example:
And here’s a less obvious one:
Refactoring Opportunities inspections
We’ve added a new group of inspections in the PHP section of Settings/Preferences | Editor | Inspections.
This group includes inspections that can help find good candidates for refactoring. They are disabled by default, but even in this disabled state, you’ll see an additional gutter icon near the code detected by these inspections (instead of having it underlined in the editor). Clicking the gutter icon will invoke the suggested refactoring.
Let’s see three of them in action and look at how to combine them with PhpStorm refactorings.
Complex class should be refactored
These inspections detect bloated classes based on three metrics: TCC (Tight Class Cohesion), a measure of class cohesiveness; WMC (Weighted Method Count), the sum of cyclomatic complexities for all methods in the class; and ATFD (Access To Foreign Data), the number of fields from another class accessed in this class.
PhpStorm suggests extracting methods to a new class in this case. If the original class is too big, it might need a few more iterations of refactorings.
Complex function should be refactored
This inspection calculates four different metrics for methods: cyclomatic complexity, nesting depth, number of variables used, and total number of lines.
If a method’s overall score is poor, then it’s likely a good idea to split it into simpler, more manageable parts.
PhpStorm suggests some candidate blocks to extract to a new method.
Method has Feature Envy of another class
When a method accesses the data of another object more than its own data, that is a sign of a Feature Envy.
You might want to move the full method or a part of it to the other class.
Feel free to explore and enable other inspections from the Refactoring Opportunities group.
Generic support for web-types
There might be some regressions around Vue.js support while the migration is still in progress. We’re hoping to stabilize everything in the next couple of weeks. Please let us know if you spot any issues!
For the full list of changes in this build, please see the release notes.
Important! PhpStorm EAP builds are not fully tested and may be unstable.
You can install an EAP build side by side with a stable PhpStorm version to try out the latest features.
EAP builds are free to use but expire 30 days after the build date.
Please report any problems you find to our issue tracker, or by commenting on this post.
Your JetBrains PhpStorm team
The Drive to Develop
Permanent link to this post here