PhpStorm 2020.2: PHP 8 Union Types, New Control Flow Engine, GitHub Pull Requests, OpenAPI, Extract Class Refactoring

Roman Pronskiy

PhpStorm 2020.2 is now available!

This major release includes support for PHP 8 Union Types, a new control flow engine for PHP, full GitHub pull requests workflow support right inside the IDE, a brand-new Inspection widget, OpenAPI support via a plugin, and more.

Download PhpStorm 2020.2

Read on to check out all the new features and significant updates, along with a ton of GIFs!

PHP 8 Union Types

PHP 8 will have so many new features that to some extent you can almost call it a new language. We’ve started to roll out support for version 8 early, and the first big feature is union types.

Union types T1|T2|... can be used wherever types can currently be specified: arguments, properties, or return values. In this case, a variable can accept any of the listed types.

Union types have long been used in PHPDoc tags, but now they will really be checked in runtime by the interpreter itself.

Switch language level

To see all the features, you need to switch the language version to PHP 8. You can do this manually in settings under Languages & Frameworks | PHP | PHP language level.

You can also use the Alt+Enter quick-fix to switch if there is already some code with new features.

And if the PHP version constraint is specified in composer.json, PhpStorm will switch automatically.

Converting PHPDoc into native union types

You can call the Alt+Enter quick-fix on a PHPDoc tag union type to convert it to a native union type.

Remove unnecessary PHPDocs

If a PHPDoc tag contained only a type declaration, it may now be redundant and can be removed with an Alt+Enter quick-fix.

We are considering adding the ability to run this fix in the clean-up stage before commit. What do you think? Can it always be considered safe to do so?

Type validation

PhpStorm was already able to analyze types and find violations with PHPDoc tags. But with native declarations, these capabilities became much wider.

This is especially useful in complex code when violations are not obvious at first sight.

Pseudotype false

Some legacy functions may return false in the event of an error. For example, strpos(), array_search(), and 310 other standard functions behave this way.

In these cases, the return type could be declared as a union like int|bool, but in fact it would never return true. This is where the new false pseudotype is useful.

The false pseudotype can only be used as a part of union type. Otherwise, PhpStorm will highlight it as invalid code.

Duplicate and redundant types

Many combinations in type associations are forbidden or simply redundant. For example:

  • bool|false — because false is a part of bool.
  • Foo|Foo or int|string|INT— duplications.
  • object|Userobject contains everything else.
  • iterable|array or iterable|Traversableiterable contains array and Traversable.
  • void — can only be used in the return value and only as a standalone type.
  • false or null — only as part of the union type.

All such violations will be highlighted in PhpStorm.

Nullable types

The existing ?Type syntax is now considered to be shorthand for Type|null.

But you cannot use ? in union types because it causes ambiguity.

PhpStorm provides an Alt+Enter quick-fix for this.

Type variance in inheritance

There are two rules for methods:

The type of the parameter is contravariant, which means it can be expanded.

The return type is covariant, which means you can only narrow it down.

Type order does not matter here, so Type1|Type2 is equivalent to Type2|Type1.

Property type cannot be changed
The type of an inherited property has to match the type of the parent property, for example:

New control flow engine

By redesigning the control flow engine we were able to introduce new inspections and fix many old bugs. As a result, PhpStorm now understands code even better than before.

Here and there you may notice warning highlights from new inspections. Some of them may require your attention as potential sources of bugs, while fixing others can just make the code clearer and easier to read.

Here are some of the new inspections.

Variable is always true or false
Example in Parser.php from doctrine/orm.

Condition is always true
Example in FormValidator.php from symfony/form.

Result of instanceof is always true
Example in Logger.php from symfony/monolog-bridge.

Pseudovariable $this will always be of type ResetInterface, because it is higher up in the hierarchy.

Expression is always null
Example in ProcessUtils.php from symfony/process.

New action: Type Info (⌃⇧P / Ctrl+Shift+P)
You can point the caret at any expression and call this action to see how PhpStorm inferred the type. The action is available under View | Type Info or via the ⌃⇧P / Ctrl+Shift+P shortcut.

type_info

Improvements for Composer

Since version 2020.1, it has been possible to manage Composer dependencies directly in the composer.json file in PhpStorm. This release brings a few enhancements.

Support for Satis/Packagist custom repositories
If you specify a custom source in the "repositories" section, all packages in it will be available for autocompletion, and information about packages and versions will be cached in PhpStorm.

composer_custom_repos

Code completion for multiple versions
If there are multiple versions separated by commas or by pipe (||) symbols, PhpStorm will now handle completion correctly.

composer_range-versions.сmproj

Links to homepage and sources
Each package now has links to its websites in the information popup.

composer_pages_links

Quick access to quality tools configuration
If the list of dependencies contains code quality tools that are supported in PhpStorm, there will be a wrench icon near them that will take you directly to the corresponding section of settings.

composer_open_settings

Improvements for quality tools

PhpStorm 2020.2 introduces a number of useful updates for quality tools.

Support for Docker Compose
PHP_CodeSniffer, PHP CS Fixer, and PHP Mess Detector can now be run with docker-compose.

quality_tools_docker-compose

Exclude list for PHP_CodeSniffer
If there is an "exclude-pattern" section in ruleset.xml, it will now be taken into account and the specified files will not be analyzed in the IDE.

Formatting with a remote interpreter
PHP CS Fixer and PHPCBF can be run via SSH, Docker, Docker Compose, Vagrant, and others.

Command Line Tools

All Symfony, Laravel Artisan, Drupal Drush, WP-CLI commands, and Composer scripts can be run very quickly in PhpStorm without opening the terminal.

Do this by adding the tool you want to use in the settings under Tools | Command Line Tool Support.

Then, by pressing Ctrl-Ctrl you will see the Run anything popup, in which all commands are available with autocompletion.

Starting with this release, you don’t even have to configure anything for Symfony, Laravel, and Drush. Just open the project, press Ctrl-Ctrl, and start typing the command.

⚠️ For Laravel on macOS, the artisan file must be executable (chmod +x artisan).

You can now run these tools through any remote interpreter (SSH, Docker, Docker Compose, Vagrant). For example, you can quickly test your application with PHP 8 by adding a Docker Interpreter from the php:rc-cli image.

Extract Class refactoring

Classes always look neat and clean at first. But over time, many methods and properties are often added to them. And at some point, you can find a class that is overgrown with an excess of responsibilities.

Here it can help to extract a bunch of related methods and properties into a new class. This is exactly what the new Extract Class refactoring offers.

Press Ctrl+T on a class member and select Extract class. PhpStorm will prompt you to enter the name of the new class and you can choose more methods and classes to extract.

This refactoring also works for functions.

More for PHP

New inspection: Typed property might be unassigned

If a typed property is defined but not initialized, the TypeError error will appear when trying to read it (if there is no magic __get()). PhpStorm will highlight cases where you are reading from uninitialized properties.

New inspection: Array used only with write access

Example in SchemaTool.php from doctrine/orm.

The $pkColumns array in gatherColumns() method is updated but never read nor returned.

Customizing generated getters and setters

Now you can adjust the naming style by choosing between camelCase() and snake_case().

The order in which getters and setters are generated is also configurable now.

Both options are available in settings under Editor | Code Style | PHP on the Code Generation tab.

Correct resolve when several projects are opened in one window

Previously, if you opened multiple projects in one window, you would often see errors about a class being defined multiple times. If you wanted to go to the definition of a class, you had to choose from multiple implementations.

multiple-projects-resolve-2019

This is no longer the case. Resolve and Go to declaration work as expected in PhpStorm 2020.2.

multiple-projects-resolve-2020.2

Lower priority for symbols from vendor

In the autocompletion list, priority will be given to classes from the project. Entities from a vendor folder and PHAR files will get lower priority.

Full support for GitHub pull requests

Basic support for pull requests was introduced in PhpStorm 2018.3. Since then there have been some updates, but many actions still required switching to a browser.

In PhpStorm 2020.2 you can manage the entire pull request workflow directly in the IDE!

You can go to pull requests from the VCS | Git | View Pull Requests menu or from the toolbar or using the Alt+7 shortcut. The first thing you’ll see will be a list of the available pull requests with search and filtering options.

Clicking on a pull request will open detailed information about it – its reviewers, tags, modified files, and timeline.

You can work through the whole review flow all at once – start and request a review, comment on changes both at line level and in the whole commit, and submit the review.

Under the timeline, you’ll see the results of pre-commit and CI checks.

To merge a pull request, you previously had to create a local branch first. This is no longer required and you can now merge straight away from the UI.

OpenAPI support

The OpenAPI Specifications plugin is available for all JetBrains IDEs and works starting from this 2020.2 release. It provides highlighting, validation, and navigation in OpenAPI specification files (openapi.yaml/openapi.json and swagger.yaml/swagger.json).

It also provides the following features:

  • Swagger UI integration – rendered directly in the IDE window.
  • Code generation.
  • Structural diff for spec files – this can help spot breaking changes.
  • Gutter icons to create HTTP Client requests to endpoints.
  • Autocompletion for endpoints in the HTTP Client.
  • Rename refactoring – if you rename an endpoint in the spec file, it will automatically be renamed in the HTTP client request.

Add caret per selected line action

The new Add Caret Per Selected Line action creates carets at the end of each selected line, and deselects the lines. Invoke it using the shortcut Alt+Shift+G on Windows/Linux or ⌥⇧G on macOS.

add_caret_action

Brand-new Inspection widget

The top right corner of the editor now displays a widget with the number of problems in the current file.

You can quickly jump to the next/previous problem by clicking the arrow icons. This was previously achieved with the shortcuts F2 / Shift+F2.

In the Inspection widget, you can choose which problems are displayed, for example only syntax errors or all problems.

Clicking on the widget will open a new Problems tool window with a list of all problems detected in the file.

Version control

Git from WSL 2

PhpStorm can now use the Git binary from WSL. Moreover, the IDE itself determines whether the WSL is installed and whether Git is available in it.

Settings are available under Preferences | Version Control | Git.

⚠️ Git will only work with WSL 2. Git from WSL 1 is unstable and may cause incorrect results for Git commands, therefore we do not support it.

Improved UI to compare branches

PhpStorm allows you to compare any branches and see which commits belong to which branch. To do this, select any branch available in the VCS | Branches popup and choose Compare with Current from the context menu.

In PhpStorm 2020.2, information about logs and commits is displayed directly in the editor. This allows more information to be placed on the screen.

vcs_compare_branches

Updated command dialogs

Dialogs for the merge, pull, and rebase Git commands have been redesigned and unified. Some missing options have been added and you can now see the exact Git command that will be executed.

All commands are available in the VCS | Git menu.

git-pull

New action: Drop commit

Unnecessary or temporary local commits can now be removed directly from the PhpStorm log. To do this, call the context menu on a commit and select Drop Commit.

git-drop-commit

New action: Squash commits into one

It is also possible to combine several commits into one. It’s a good idea to do this to clear intermediate results from the history before creating a pull request or pushing changes.

Select multiple commits in the log and choose Squash Commits… from the context menu.

vcs_squash

Database Tools

PhpStorm includes almost all of DataGrip’s features as standard. You can check out What’s new in Database tools for an overview from our colleagues about its new features.

Web

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

 

A full list of all the changes in this release is available in the really long release notes.

That’s all for today. Thanks for reading to the end! We’ll be happy to respond to your questions, suggestions, bug reports, and any thoughts you’d like to share in the comments.

Your JetBrains PhpStorm team
The Drive to Develop

Subscribe

Subscribe for updates