Releases

PhpStorm 2021.3: PHP 8.1, Generics, Remote Development, Refactorings, and More

Read this post in other languages:

PhpStorm 2021.3 is now available!
This major release introduces full support for PHP 8.1, better handling of generics in PHP, remote development, improvements to deployment, an HTTP client, refactorings, and much more.

Download PhpStorm 2021.3

Here are the highlights of the release:

Full support for PHP 8.1

Generics in PHP

Improved refactorings

Remote Development

Faster and better deployment

HTTP Client

UI Updates

VCS

Read this blog post for details about all the significant updates, demonstrated through a ton of GIFs!


PHP 8.1

PHP 8.1 has already been released! We would like to express our thanks to all the contributors and release managers!

PhpStorm 2021.3 supports all of the latest language changes. Here’s what’s available and how you can use it in the IDE.

As usual, you can try new features by setting the project language level to PHP 8.1. You can switch from the toolbar or by specifying the requirement in composer.json.

Enums

Enumerations can be used instead of a set of constants, and they provide type validation out of the box.

PhpStorm has supported PHP 8.1 Eums since the 2021.2 release. Learn more about enums support in PhpStorm.

Readonly properties

Readonly properties are properties that cannot be changed after initialization, meaning after a value is assigned to them.

It was possible to achieve similar behavior in previous versions of PHP with a private setter, but the readonly keyword makes classes cleaner and constraints stricter.

A readonly property can only be initialized in the same scope where it’s declared. This means you can assign a value either in the declaration itself or in a class method.

Properties with the readonly flag must be typed, but if you don’t know the specific type, then you can use the mixed type.

If a readonly property is initialized by an object, it does not become immutable. That is, its internal state can still be changed.
A readonly property cannot have a default value, but you can initialize it in the constructor as a promoted property. PhpStorm has an Alt+Enter quick-fix for this:

Pure intersection types

Union types were added in PHP 8.0, and now PHP 8.1 is introducing intersection types.

The syntax is TypeA&TypeB, and it means that the variable must be both instanceof TypeA and instanceof TypeB.

In PhpStorm 2021.3, code completion for this is mostly the same as it is for union types, because a union can contain members of all involved types, and an intersection must contain members of all types.

This feature is called pure intersection types because combinations with union types are not supported and are being left for future consideration. PhpStorm 2021.3 will check the syntax:

In older PHP versions, it was quite common to use intersection types in PHPDoc annotations. Those are also supported in PhpStorm 2021.3, and when you decide to upgrade to PHP 8.1, you can add a native type declaration:

New return type: `never`

PHP 8.1 features a new type for return values: never.

A function or method declared with the never type indicates that it will not return a value and will either throw an exception or end the script’s execution by calling die(), exit(), trigger_error(), or something similar.

This type improves code analysis and does pretty much the same thing that the #[NoReturn] attribute has been doing in PhpStorm. It’s great that this is a language feature now!

Final constants

PHP 8.1 allows you to declare final class constants so that they cannot be overridden in child classes.

`new` operator in initializers

In PHP 8.1, objects can be used as default parameter values, static variables, and global constants, as well as in attribute arguments.

As a cool side effect, this change makes nested attributes possible.

However, you can’t use the new operator with properties or class constants because of problems with the initialization order.

PhpStorm validates all of the cases to help you use these new language features:

First-class callables

In PHP 8.1 it is possible to get a reference to any function. This is called first-class callable syntax.

This is what it used to look like:

$fn = Closure::fromCallable('strlen');

Now you will be able to do the following instead:

$fn = strlen(...);

The advantage is that the new syntax is accessible to static analysis, and it respects the scope at the point where the callable is created.

In PhpStorm, convert fromCallable calls to the new syntax by pressing Alt+Enter and choosing a quick-fix from the dropdown list:

Deprecations

PHP has been known for its quirky behavior in some cases, but each new version cleans up many of these issues. Version 8.1 is no exception, as it deprecates a lot of problematic legacy features.

PhpStorm will help you find deprecated code in your projects and make the upgrade process easier.

Let’s take a closer look at some of the significant deprecations.

Deprecated usage of Serializable interfaces

Instead of broken Serializable interfaces, it is recommended to use __serialize() and __unserialize() magic methods, which were introduced in PHP 7.4.

In PHP 8.1 you can still use Serializable, but only if the implementing class has __serialize() and __unserialize() as well.

PhpStorm will highlight incorrect code and offer a quick-fix to add the magic methods to a class:

Restricted GLOBALS usage

Usage of the GLOBALS superglobal variable will be restricted to disallow problematic cases, while allowing most code to continue working as-is.

To learn more about all deprecations, see the Migration Guide.

More for generics

In PhpStorm 2021.2, we added long-awaited support for generics. It covers basic use cases, but there’s still work to be done. Thank you for your feedback and tickets in the tracker!

Here’s what’s in the upcoming batch of enhancements for generics:

Improved support for Doctrine Collections

We’ve added support for the IteratorAggregate interface in @template annotations.

As a result, iterating over Doctrine Collections now works out of the box!

Support for future Laravel collections

In the upcoming Laravel release, the illuminate/collections package is getting generics annotations. With PhpStorm 2021.3, you’ll get better completion for them too.

Infer generic types from constructor

Let’s say there is a generic @template parameter in a constructor and you want to specify the type by passing an object as an argument. This would not have worked in previous versions because the type would be missing and you would not get code completion. In this version it works correctly.

Support for the phpstan-* annotation for templates

PhpStorm now supports any annotation for generics. You can use a @psalm-* or @phpstan-* prefix or use pure tags without prefixes, like @template.

Improved Refactorings

Move Class and Move Namespace refactorings with drag and drop 🎉

Previously, if you moved a file or a folder in the Project view tree, that would not change namespaces in the classes under those folders.

In PhpStorm 2021.3, you can simply drag files or even whole folders to a new destination, and PhpStorm will take care of updating the namespaces of the classes and all the class usages too!

This change makes it much easier to restructure a project. And you can undo moves with Ctrl-Z!
You would need a properly configured PSR-0 / PRS-4 projects to use this refactoring.


The existing refactorings Inline method, Inline variable, Extract variable, and Extract method received multiple updates and fixes. They are now much more reliable and predictable. Give them a try!

Inline function or method

When a method body is more obvious than the method itself, it makes sense to replace calls to the method with the method’s content and delete the method itself.

To use this refactoring, position the caret at the redundant method and press Ctrl+Alt+N (⌥⌘N on macOS).

There is also a new quick-fix available. When you have a private method that is only used once in the code, you can press Alt+Enter on it and choose Inline method:

Inline variable

Sometimes you may have redundant variables in the code that just add clutter and can be removed.

To replace the references to the variable with the expression itself, position the caret at the redundant variable and press Ctrl+Alt+N (⌘⌥N on macOS).

If a variable has only been used once, then you can inline it with a quick-fix by pressing Alt+Enter:

If you don’t remember the refactoring name or shortcut, you can also use Ctrl+T (⌃T) and choose one from the list.

Remote Development

PhpStorm 2021.3 comes with bundled remote development capabilities to help you code, run, and debug your projects from any machine.

Here’s how it works:

  • On the client side, you run JetBrains Client, which is a lightweight application that enables you to work with your remote project.
  • On the server side, there is a headless build of PhpStorm, potentially the same executable as the one you are currently running.
  • You can connect to a remote server from the PhpStorm Welcome screen or by using JetBrains Gateway, which is a lightweight application that is used as an entry point and connects everything via SSH.

With remote development workflows, you can:

  • Feel like you are running your IDE locally with lag-free typing and code assistance.
  • Develop on any machine without being limited by the power of your local devices.
  • Keep source code off your laptop. With JetBrains Gateway, source code is kept on the server side.
  • Work securely from anywhere. Remote development functionality supports working from home and hybrid schedules. Your projects are securely located on the remote machine and can be accessed from anywhere.

Please note that this is an early access version of the remote development functionality and we’re still actively working to improve it.

Faster and Better Deployment

PhpStorm allows you to upload files to a remote server. In version 2021.3, we’re making this process faster and adding some highly anticipated options.

All deployment settings are available at Settings/Preferences | Build, Execution, Deployment | Deployment.

Support for rsync

When using an SFTP server for deployment, you’ll see a new rsync option under the Advanced section.
Using rsync will result in significantly faster deployments!
Here are benchmark figures for uploading Drupal 8 (12,757 files, 120 MB, SFTP on local Docker):
Rsync option disabled: 6.26 minutes
Rsync option enabled: 38.192 seconds – 9x faster!

To activate rsync, you’ll need to tick the `Use rsync for download/upload` checkbox for the SFTP server as shown below:

The path to the rsync executable is configured under Settings/Preferences | Tools | Rsync:

For macOS and Linux, it should be available right away. For Windows, we recommend using Cygwin and OpenSSH.
When configured correctly, rsync will be used to upload and download files and folders, including when you use auto-uploading and drag and drop.
SFTP will still be used to provide navigation in the Remote Host tool window and for some operations like Delete, Create Folder, Move, Change Permissions, etc.
Also, the Sync command still uses SFTP to get the list of differences and to upload or download.

Retransmit files that failed to upload

When uploading to a server, file uploads may fail for any number of reasons. PhpStorm 2021.3 makes it easy for you to retry uploading.
You’ll see a Retry link in the File Transfer tool window if anything failed during the upload.

Speedup for SFTP/FTP/FTPS with multithreaded connection

We’ve prepared a highly anticipated implementation for multithreaded deployment. The following operations are currently supported: Upload, Download, and Drag and Drop. Sync and auto-upload are out of the scope and are not yet supported.
A new option, Number of connections, is located in the Advanced section of the SFTP/FTP/FTPS server configuration area available at Settings/Preferences | Build, Execution, Deployment | Deployment. It is set to 5 by default:

Upload all opened files

If you open multiple files in different tabs, you will be able to upload all of them with a single command, Upload All Open Files to…. It’s available under Tools | Deployment, via Search for action (Cmd/Ctrl+Shift+A), or from the context menu of any file.

Quickly switch default server for project

In PhpStorm 2021.3, the status bar will have a new widget that shows the current default server. You will be able to switch it right from the status bar, as well. This can come in handy when you need to deal with multiple environments.

Proxy support for SSH connections

You can now specify an HTTP or SOCKS proxy server for your SSH configuration in Preferences / Settings | Tools | SSH Configurations.

Under the new HTTP/SOCKS Proxy section, you can select the proxy type, enter a hostname and a port, and, if necessary, apply authentication details.

It’s also possible to sync your SSH proxy settings with the global IDE ones. To do so, select the Use global IDE proxy settings checkbox. If you haven’t yet configured the global settings, you can easily do so by clicking the corresponding link under the checkbox.

HTTP client enhancements

Support for text and JSON streams in the HTTP Client

It is now easier to test streaming applications in your favorite IDE, as the upcoming v2021.3 supports the following content types: text/event-stream, application/stream+json, and application/x-ndjson. This means the HTTP client will no longer wait for a stream to end and will instead display the output on the fly.

The HTTP client will also listen for the end of a stream and will show a message when the process is finished.

Binary responses in the HTTP client

The HTTP client now shows an image preview in the response console if a response is an image.

Output redirection to a custom file or directory

The HTTP client can now redirect output to a custom file or directory. It supports two operators for force and soft redirects:

  • The >> operator always creates a new file, adding an -n suffix to a file name if the requested file name already exists.
  • The > operator only rewrites the file if it already exists.

Support for HTTP request identifiers

The upcoming PhpStorm 2021.3 now understands identifiers (or names) for requests. The IDE understands identifiers both that were declared as a comment with @name and that were declared as text in the first separator (###).
You can invoke a request through its identifier from the Services tool window or from the Run Configuration, Run Anything, or Search Everywhere dialogs.

VCS

Align changes highlighting

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.

Checkout and Rebase onto Current for remote branches

The Checkout and Rebase onto Current action allows you to check out the selected branch and rebase it on top of a branch that is currently checked out. Until now, it has only been available for local branches. Starting from this EAP, we’ve added the ability to apply this action to remote branches as well.

Reorganized VCS settings

To improve the user experience and speed up the process of configuring your version control systems, we’ve made the VCS settings more discoverable and more clearly structured. Here are some of the changes you’ll find in Preferences/Settings | Version Control.
The Version Control node now lists all the available settings. You can easily navigate to the settings by clicking on them.

We’ve improved the layout inside the Git node. We divided all of the settings into sections that account for the most important processes: Commit, Push, and Update. Additionally, the parameters inside these sections are now organized more logically.

We’ve added a separate node for Directory mappings, and the background operations are now switched on by default. The Background node no longer exists.

New ‘Push All up to Here’ action

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.

User Experience

New Bookmarks tool window

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 F3 (macOS) or F11 (Windows/Linux). All the added bookmarks will be available in the unified Bookmarks tool window.

Source preview for Show Usages

The Show Usages dialog, which you can invoke by ⌘+clicking (macOS) or Ctrl+clicking (Windows/Linux) a symbol, can now display the source code preview per found usage. To enable the preview, click the [img] toolbar button.

Improved Markdown support

In PhpStorm 2021.3, tables can be created with ease. Simply invoke the context menu and select Insert | Table. That’s it! You can select the preferred size using the mouse hover action or the keyboard.

The cell width in the table will be adjusted as you type. You can edit the table content using the floating toolbars.

To create a new row you can use Shift+Enter, and Tab to navigate to the next cell.

Database Tools

PhpStorm includes almost all of DataGrip’s features as standard. Check out What’s New in DataGrip 2021.3 for an overview of the new features we’ve added for working with databases.

Web

As usual, all of the updates for WebStorm 2021.3 have also been incorporated into PhpStorm.


The full list of changes in PhpStorm 2021.3 is available on the lengthy release notes page.

That’s all for today. Thanks for keeping up with the changes! We hope they improve your PhpStorm experience.

Your JetBrains PhpStorm team
The Drive to Develop

image description