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.
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
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?
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. 👇
Some functions may return a certain type as a result or
false in case of an error. For example, this is how
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 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
Cases when an object and some other class meet in the declaration, such as
object|User, or if there is a combination of
Traversable, will all be highlighted in PhpStorm as invalid.
Invalid types in Unions
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.
null, and their combinations are not allowed to be used as union types.
?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.
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
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.
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