What’s New in PhpStorm 2021.2

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.

Generics for PHP

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.

Generics for PHP

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.

PHP 8.1 Enums

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.

Backed Enums

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.

Learn more

Array shapes

Array shapes

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.

Improved Extract Method Refactoring

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).

Refactoring duplicate code

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.

New smart selector for code fragments

New smart selector for code fragments

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.

Improvements to Expand selection

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.

Inspections and Quick-Fixes

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.

Condition can be replaced with `?->`

Condition can be replaced with `?->`

Replace unnecessary checks for null with the new safe operator from PHP 8. Press Alt+Enter to perform the transformation.

Use constant from a class where it is defined

Use constant from a class where it is defined

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.

Redundant conditions in logical expressions

Redundant conditions in logical expressions

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.

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 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.

Learn more

IDE

Run tests before commit

Run tests before commit

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.

Actions on save

Actions on save

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.

Unified Show Diff

Unified Show Diff

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.

Text search in Local History revisions

Text search in Local History revisions

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.

Text search in Local History revisions

GPG signature

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.

Localized UI

Localized UI in Chinese, Japanese, and Korean

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!

Web Technologies

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.

Rename refactoring for React useState hooks

Rename refactoring for React useState hooks

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.

Auto-import for require()

Auto-import for require()

PhpStorm can now add missing import statements when you use code completion when working with CommonJS modules.

Database Tools

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.

DDL data source

DDL data source

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.

Completion of fields and operators for MongoDB

Completion of fields and operators for MongoDB

Our code completion has now come to the MongoDB console.

Other Enhancements

Here are just a few of the many additional improvements and enhancements in PhpStorm 2021.2:

  • Preview HTML and PHP files in a browser using the PhpStorm’s built-in web server. PhpStorm can now automatically reload the opened pages on save.
  • It is now easier to reorganize tool windows. Hover over the top of the tool window you want to move, then drag and drop it to where you want it.
  • PhpStorm automatically cleans up any directories of caches and logs that were last updated more than 180 days ago. You can also clean up manually via Help | Delete Leftover IDE Directories.
  • New terminal options have been added for setting a cursor shape and for using Option as a meta key.
  • A new Advanced settings page that provides options for fine-tuning PhpStorm has been added in Preferences.
  • Significantly improved the speed of FTP uploads for large collections of files.
  • Added support for levels parameter in dirname().
  • Resolved all outstanding issues with the formatting of PHP code.