Early Access Program

PhpStorm 2020.2 EAP #6: Union Types Are Here

We’ve just updated the PhpStorm 2020.2 EAP.

The release of PHP 8 is scheduled for November 26, 2020. PhpStorm 2020.3 will be released around the same time with full support for the new language version.

Meanwhile, the feature freeze for PHP 8 is about a month away, and some new features are still being discussed. There is one major feature in PHP 8 that is well established – Union Types. And we plan to release support for it in PhpStorm 2020.2.

Download PhpStorm 2020.2 EAP

Union Types

In PHP 8.0, it will be possible to declare a set of arbitrary types for properties, arguments, and return types. A value will be able to take any of these specified types.

In fact, union types already exist in PHP and have been actively used both in popular packages and in the standard library.

It has long been possible to specify a union type in PHPDoc. But the problem was that it was never validated by the PHP engine, even when adding declare(strict_types=1).

Starting with PHP 8, and PhpStorm 2020.2, everything will get a lot more interesting.

Switch language level

To see all the features of the IDE in action, let’s first switch the Language Level to PHP 8. This can be done manually in Preferences | Languages & Frameworks | PHP | PHP language level.

Or, if you specify the corresponding requirement in composer.json, PhpStorm will pick it up automatically.

Convert PHPDoc to native Union Types

If you have already used Unions in PHPDoc, the first thing you will notice is highlighted type declarations.

With the help of the Alt+Enter quick-fix, they can be easily converted into real Union Types.

This works everywhere including function arguments, return types, and properties. For properties, it takes into account default values, too.

Remove redundant PHPDoc

After adding a type declaration, your PHPDoc may become excessive as it does not provide any extra information.

Luckily, you can now remove it with a quick-fix (Alt+Enter).

We are considering making this quick-fix available at the clean-up stage so that it is possible to remove redundant PHPDoc blocks all over the project, in one go. What do you think?

Type validation

Now, when union types are declared natively, PhpStorm will be able to analyze calls and highlight problem areas that involve Unions.

The checks work on all levels: properties, arguments, and return values.

It’s exceptionally useful when you’re in more complex code bases where you can’t see the problems at first sight. PhpStorm will highlight places that require attention even before the code is run.


✋ These are the key things you need to know about the Union Types support in the upcoming PhpStorm 2020.2. If you’d like to learn more about all the tricky parts, read on. 👇


Pseudotype false

Some functions may return a certain type as a result or false in case of an error. For example, this is how strpos(), array_search(), and 310 other standard functions behave.

In such cases, you could specify the return type as int|bool, but in reality, the functions will never return true. This is where the new pseudotype false comes.

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

Duplicate and redundant types

Some unions of types are meaningless or redundant, for example, bool|false or some trivial repetitions of Foo|Foo.

Cases when an object and some other class meet in the declaration, such as object|User, or if there is a combination of Iterable with array or Traversable, will all be highlighted in PhpStorm as invalid.

Invalid types in Unions

void

The void type in PHP can only be used as a return type and is not allowed to be combined with any other type. PhpStorm will always highlight this violation.

false|null

Standalone false, standalone null, and their combinations are not allowed to be used as union types.

Nullable types

The existing ?Type syntax is now considered a shorthand for Type|null, which in PHP 8 is also valid.

But when there is more than one type combined with null, you cannot use the short syntax because the meaning will be ambiguous.

PhpStorm will warn you about this and allow you to fix it with the Alt+Enter quick-fix.

Variance

During inheritance, PHP allows you to modify method signatures by following two basic rules.

Parameter types are contravariant

It means that the type can be extended, that is, the child must be a supertype.

Return types are covariant

In the case of return values, you can only narrow the type down, i.e. the child must be a subtype.

Note that the order of types in unions does not matter, which means Type1|Type2 is equivalent to Type2|Type1.

Property types cannot change

The type of the inherited property should evaluate to the same type. See the examples:

Existing functionality affected

All existing features of PhpStorm will support Union Types, including refactorings and code generation features.

https://wiki.php.net/rfc/union_types_v2
https://php.watch/versions/8.0/union-types
https://www.slideshare.net/nikita_ppv/whats-new-in-php-80
https://stitcher.io/blog/new-in-php-8


The full list of changes in this specific EAP build is available in the release notes.

That’s all for today. We have at least one more EAP build planned, so now is the best time to submit your feedback via our Issue Tracker or in the comments section.

  • Important! PhpStorm EAP builds are not fully tested and may be unstable.
  • You can install an EAP build side by side with a stable PhpStorm version to try out the latest features.
  • EAP builds are free to use but expire 30 days after the build date.

Your JetBrains PhpStorm team
The Drive to Develop

image description