PhpStorm 2021.3 is a major update that introduces support for the recently released PHP 8.1, better handling of generics in PHP, and enhancements for deployment, HTTP client, and VCS integration.
A new version of the interpreter has just been released! Check out the video from Brent Roose and community members for an overview of all the new language features.
One of the most highly anticipated features of PHP 8.1 is enums.
Support for enums was introduced in PhpStorm 2021.2, offering 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 provides error highlighting and proper suggestions to save you time.
Readonly properties are the properties that cannot be changed after initialization, that is, after a value is assigned to them.
PhpStorm 2021.3 will help you make use of readonly properties in your code and make sure they are used correctly.
A dedicated inspection will check that a readonly property is initialized only in the same scope where it’s declared, either the property declaration itself or in a class method.
Since readonly properties must always be typed, PhpStorm will check if the type declaration is specified.
A readonly property cannot have a default value, but you can initialize it in the constructor as a promoted property, so PhpStorm provides a quick-fix for this.
In PHP 8.1, you can use new expressions when initializing default parameters’ values, static variables, global constants, and attribute arguments.
PhpStorm will verify such usages and highlight invalid cases such as providing a new object as a class constant or property.
PHP 8.1 lets you get a reference to any function by means of the
first-class callable syntax. Now, instead of writing
$fn = Closure::fromCallable('strlen');
, you can use a shorter
$fn = strlen(...);
form, and PhpStorm will provide you
with an Alt+Enter quick-fix to convert your code to the new syntax.
PHP 8.1 brings intersection types, whose value must satisfy multiple type constraints instead of just a single one.
PhpStorm will now provide code completion for these constructs and check that an intersection type is not combined with a union type. And if you have an intersection type specified as a PHPDoc annotation, PhpStorm will interpret it correctly, too.
PHP 8.1 introduces a never
type for return values. If a function
is declared with the never
type, then it will not return a value
and will either throw an exception or end the script’s execution (just like
the die()
, exit()
, or trigger_error()
functions do).
In PHP 8.1 you can declare final
class constants so that
they cannot be overridden in child classes, and PhpStorm will verify such cases.
In addition to new features, PHP 8.1 brings several deprecations aimed at making the language more consistent and easier to use. PhpStorm will help you locate such deprecated code. Here are a couple of examples:
Instead of Serializable
interfaces, it’s recommended to use the
__serialize()
and __unserialize()
magic methods. In PHP 8.1,
you can still use Serializable
, but only if the implementing class uses both
__serialize()
and __unserialize()
. PhpStorm 2021.3 highlights
incorrect code and offers an Alt+Enter quick-fix for adding the magic methods.
Using the $GLOBALS
superglobal variable in PHP 8.1 is now restricted –
it is not allowed to perform write or read/write operations on the $GLOBALS
taken as a whole. PhpStorm will highlight such problematic usages.
PHP does not provide support for generics on a language level yet, but using them via annotations
has already become quite popular. They make code safer and more predictable when working
with collections, containers, factories, and other applications. PhpStorm 2021.3 expands
the support for generics via the @template
annotation.
PhpStorm brings support for the IteratorAggregate
interface and iterating
over Doctrine Collections now works out of the box.
In the upcoming Laravel release, the illuminate/collections package will support generics annotations. PhpStorm 2021.3 provides better code completion for such collections.
If there is a generic @template
parameter in a constructor
and you want to specify the type by passing an object as an argument,
PhpStorm will now infer the correct type and provide code completion.
Earlier, to move a class and update its namespace, your only option was to use the move refactoring (F6) in the code editor. In PhpStorm 2021.3, you can use drag and drop in the Project view instead. This way, the project structure would be visible during operation. And what's more, you can move several classes or entire folders using this technique, and PhpStorm will correctly update all the references.
The Inline method refactoring lets you replace the calls to the method with the method’s content and delete the method itself. In PhpStorm 2021.3, the refactoring received multiple improvements and will cover even more cases. For example, non-static methods, method chains, and arrow/anonymous functions passed as parameters are now supported.
To use this refactoring, position the caret at a method and press Ctrl+Alt+N. If there is a private method that is used only once in the code, press Alt+Enter and use the new Inline Method quick-fix.
The Inline variable refactoring lets you replace the references to a variable
with the actual variable’s value. In PhpStorm 2021.3, this refactoring supports
inlining NOWDOC/HEREDOC
variables and includes several usability enhancements.
In addition, PhpStorm will now detect variables with a single usage and provide you with an Inline Variable quick-fix, which you can apply via Alt+Enter.
When using an SFTP server for deployment, you can enable rsync for uploading and downloading files and folders, which will significantly increase transfer speeds. The corresponding Use rsync for download/upload option is located in the Advanced section of the deployment server configuration.
The path to the rsync
executable can be configured under
Settings/Preferences | Tools | Rsync. For macOS and Linux, it should be available
right away. For Windows, it is recommended to use
Cygwin
and
OpenSSH.
PhpStorm 2021.3 lets you retransmit the files that failed to upload. In such cases, click the Retry link in the File Transfer tool window.
You can now specify an HTTP or SOCKS proxy server for your SSH configuration under Settings/Preferences | Tools | SSH Configurations.
It’s also possible to use the global IDE-wide proxy. To do this, enable the Use global IDE proxy settings checkbox and provide the proxy details under Settings/Preferences | Appearance & Behavior | System Settings | HTTP Proxy.
PhpStorm 2021.3 provides a new status bar widget that shows the current default server. When you need to deal with multiple environments, you can switch between them right from there without going to the settings.
The HTTP client now supports binary responses. If a received response is an image, you will see its preview right in the response console.
The HTTP client can now redirect output to a custom file or directory. It supports two operators for force and soft redirects:
>>
operator always creates a new file, adding an -n
suffix
to a file name if such a file already exists.
>>!
operator rewrites the file if it already exists.
In addition to providing explicit file paths, you can now use new predefined variables:
{{$projectRoot}}
points to the project root folder
and {{$historyFolder}}
points to the
requests history folder.
In PhpStorm 2021.3, your HTTP requests can be named! You can provide an identifier
as a comment with @name
as well as text in the requests separator
(###).
You can then invoke a request by its name via the Services tool window, from the Run Configuration, Run Anything, or Search Everywhere dialogs.
In PhpStorm 2021.3, the Favorites and Bookmarks functionality has been merged into one – Bookmarks. You can add a bookmark on a specific line, file, or folder by pressing F11. All the added bookmarks will be available in the unified Bookmarks tool window.
The Show Usages dialog, which you can invoke by Ctrl+clicking a symbol, can now display the source code preview per found usage. To enable the preview, click the toolbar button.
We’ve added the new Align Changes Highlighting option to the Diff screen settings that can be found under the gear icon. This feature makes the Diff easier to read, especially when there are complex modifications, because it aligns the unchanged lines so that they are displayed next to each other. This helps you see the code that has been added or removed more clearly.
The Checkout and Rebase onto Current action lets you check out the selected branch and rebase it on top of a branch that is currently checked out. This was only possible for local branches before, but in PhpStorm 2021.3, this action can be applied to remote branches as well.
If you have several commits that are ready to be shipped while others are still work in progress, then you may want to push only the ones you are sure about. PhpStorm 2021.3 will let you push commits up to the one you have selected in the Log tab of the Git tool window. Select the necessary commit, right-click it and use the new Push All up to Here action.
The Version Control page in Settings/Preferences has been streamlined for better usability and now lists all of the available settings. The Git node is now divided into sections that account for the most important processes: Commit, Push, and Update. A separate node for Directory mappings has been added. Since the background operations are now switched on by default, the Background node has been removed.