PhpStorm 2021.2 is a major update that introduces preliminary support for generics in PHP, enums in PHP 8.1, one-line array shape annotations, and improved automatic formatting of PHP code, as well as new inspections and refactorings.
PHP does not provide support for generics on a language level at the moment, but their use via annotations has already become quite popular. They make code safer and more predictable when working with collections, containers, factories, and other applications.
We have added support for generics via the @template
annotation. This makes working with collections more straightforward in many cases, because the suggestions will contain elements of the expected type.
For factory patterns, you can now use the class-string<T>
annotation and you will get better code completion.
This preliminary support for generics is not complete and has known limitations. You can learn more about it in this blog post.
The new 8.1 version of the PHP interpreter is expected to be released at the end of 2021. But as always, we're starting to roll out support early.
One of the most highly anticipated features of the PHP 8.1 release is enums.
With PhpStorm 2021.2, you can start using enums immediately. The IDE offers code suggestions and validation to protect you from using them incorrectly.
An enum in PHP contains a set of cases, can have methods, and can be used as a type hint – in all of these cases PhpStorm will provide error highlighting and proper suggestions to save you time.
Enum cases can have scalar equivalents. This is useful if you need to save an enum to a data store. Such enums are known as backed enums.
With a quick-fix, you can add a scalar type declaration to a backed enum.
Enums also work well with other features, such as conditions and match expressions.
If there are object-like arrays in your code, you can now define their structure with this PHPDoc annotation: array{key: type, key: type, ...}
.
PhpStorm provides code completion for such annotated arrays, reducing the time you spend on routine typing and protecting you from mistakes.
The support is limited to one-line array shape definitions. For larger structures, it is often better to use real objects and classes.
One of the most frequently used PhpStorm refactorings, Extract Method, just got better.
It allows you to split big methods into smaller units, and it also eliminates duplicate code.
To use this refactoring, select any piece of code and press ⌘⌥M (Cmd+Alt+M / Ctrl+Alt+M).
PhpStorm has a Duplicated code fragment inspection. It highlights any chunks of code that are very similar – the formatting, variable names, and statement order can be different as long as the end result is the same.
This inspection highlights the first lines of duplicated fragments. You can now quickly refactor such code by pressing Alt+Enter on the highlighted line and invoking the Extract Method refactoring from the duplicate code quick-fix.
PhpStorm can also find duplicates of smaller code fragments in the current scope (method, class, or file) during refactoring. Select a code fragment and then call the refactoring with ⌘⌥M (Cmd+Alt+M / Ctrl+Alt+M). You will see the Review and replace duplicates checkbox at the bottom of the dialog.
You can also invoke the Extract Method refactoring without selecting any code. In this case, PhpStorm suggests a list of possible code fragments. The suggestions are filtered and contain only segments that make sense to refactor.
You probably already know that you can select a piece of code with the Expand / Shrink selection action ⌥+Up / ⌥+Down (Ctrl+W / Ctrl+Shift+W). This is one of the fastest ways to select code for refactoring.
This release introduces a small but useful update – the ability to select blocks without curly braces.
The Extract Method refactoring has even more improvements! Check out all the details in this blog post.
Every release comes with a bunch of inspections to reduce the time you spend looking for bugs and fixing them. Press Alt+Enter on any highlighted code in PhpStorm to apply a fix.
Replace unnecessary checks for null with the new safe operator from PHP 8. Press Alt+Enter to perform the transformation.
PhpStorm will highlight constants that are accessed via subclass or subinterface instead of the entity where the constant is defined. Replace the name of the entity with the one where the constant is defined using the Alt+Enter quick-fix.
PhpStorm will now evaluate parts of conditions to identify any redundancies. This can be useful when the checks are not obvious and you can’t tell which ones are redundant just by looking at them.
We’ve added a new group of inspections in the PHP section of Settings / Preferences | Editor | Inspections.
This group includes inspections that can help identify good candidates for refactoring. These inspections are disabled by default, but even in this disabled state, you’ll see an additional gutter icon near the code they detect (instead of having it underlined in the editor).
Clicking the gutter icon will invoke the suggested refactoring.
These inspections are combinations of various code quality metrics: cyclomatic complexity, nesting depth, number of variables used, total number of lines, and many others.
You can find the list of metrics used in the description of each inspection.
We’ve expanded the list of possible pre-commit actions with the ability to execute tests. Tick the Run Tests checkbox and select the configuration to run. It is useful to get the code verified before it is in the VCS.
You can now also customize the Analyze code and Cleanup options by clicking Choose profile next to them.
We’ve added a new configuration page under Preferences / Settings | Tools | Actions on Save.
There you’ll find a list of actions that can be triggered with Ctrl+S or on implicit autosave. These actions include code checks, reformatting, code cleanup, deployment, and many others.
PhpStorm will open a diff comparison between the initial and changed files in the editor by default, no matter where you’ve invoked the Show Diff action. If tracking the changes in a separate window is more convenient for you, simply drag and drop the desired file from the editor.
Local History has been the unsung hero of many developers, and now it has been improved to allow you to quickly find the text you need in the local revisions by using the search field in the Local History dialog.
PhpStorm 2021.2 offers a new way to secure your commits. You can now enable Git commit signing with GPG. Just go to Settings | Version Control | Git and click Configure GPG Key from the drop-down list.
Starting from this version, you can enjoy the fully localized UI of PhpStorm in Chinese, Korean and Japanese. Localization is available as non-bundled language pack plugins, which can be easily installed to your IDE*. More than 1.5 mln users started using the partially localized EAP version of our language packs. Now you can enjoy the full localization experience!
All the new features and improvements from WebStorm 2021.2 are also available in PhpStorm 2021.2, either out of the box or with free plugins from the Plugin Marketplace.
PhpStorm can now rename both the useState
’s values and functions. Place the caret on a state value and press ⇧F6/Shift+F6 or go to Refactor | Rename from the right-click context menu.
PhpStorm can now add missing import statements when you use code completion when working with CommonJS modules.
Take a look at What’s new in DataGrip 2021.2 – all these features are available in PhpStorm, too. Here are just a couple of them.
It is now possible to generate a DDL data source based on a real one. The DDL files will be created on the disk and the new data source will be based on them. That way you’ll always be able to regenerate these files and refresh the DDL data source.
Our code completion has now come to the MongoDB console.
Here are just a few of the many additional improvements and enhancements in PhpStorm 2021.2:
dirname()
.