PhpStorm 2020.2: PHP 8 Concatenated Types, New Control Flow Engine, GitHub Pull Requests, OpenAPI





Hello, Habr! We are glad to present the second major release of PhpStorm this year!



Below the cut is a detailed analysis of all the notable changes and new features. Careful - a lot of pictures.



Union types in PHP 8



PHP 8 will have so many new features that it is even a new language to some extent. We started rolling out support for the eight early and the first big feature was the merged types.



Concatenated types T1|T2|...can be used wherever types can be specified now: in arguments, properties, and return values. In this case, the variable can take one of the listed types.



In fact, concatenated types have been used in PHPDoc for a long time, but now the interpreter will actually check them at runtime.



Switching the language version



To see all the possibilities, you can manually switch the language version to PHP 8 in the Languages ​​& Frameworks | PHP | PHP language level .



Or if the code already contains the use of new features, then you can switch using a quick fix (Alt + Enter).



And if the PHP version is specified in composer.json, then PhpStorm will pull it up automatically.







Converting PHPDoc to Native Combined Types



You can call a quick fix (Alt + Enter) on a PHPDoc tag that has a combined type.







Removing unnecessary PHPDoc



If PHPDoc contained only type declarations, now it can be removed as redundant quick fix (Alt + Enter).







Can such a deletion always be considered safe and done automatically before committing? We are considering adding such a feature.


Type validation



PhpStorm used to be able to analyze types and find violations based on information from PHPDoc tags. With native declarations, these possibilities have expanded.



Checks are done for everything: properties, arguments, return values.







This is especially useful in complex code where violations are not immediately obvious.







The pseudo type false



Some legacy functions can return falseon error. For example, this is how they behave strpos(), array_search()and 310 other standard functions.



In this case, it would be possible to specify the return type as concatenated, for example int|bool, but in fact trueit will never be there.



For such cases, a pseudotype was introduced false. It can only be used in joins. Otherwise PhpStorm will highlight the invalid code.







Duplicate and redundant types



Many combinations in type unions are forbidden or simply unnecessary. For instance:



  • bool|false- because false is included in bool;
  • Foo|Fooor int|string|INT- repetition;
  • object|User- objectknowingly contains everything else;
  • iterable|arrayor iterable|Traversable- iterableincludes array and Traversable;
  • void - can only be used in the return value and only by itself;
  • falseor null- only as part of an association.




PhpStorm will highlight all such violations.







Nullable types



The old syntax is ?Typenow considered a shorthand for Type|null.



But the shorthand c ?cannot be used in joins because there is an unsolvable ambiguity.



For such cases, PhpStorm has a quick fix (Alt + Enter).







Changing types with inheritance



There are two rules for methods:



The parameter type is contravariant , that is, it can be extended.







The return type is covariant , that is, it can only be narrowed.







In this case, the order of the types does not matter, and is Type1|Type2equivalent Type2|Type1.



The type of properties cannot be changed The

type of an inherited property must be reduced to the parent, for example:







New control flow engine



An update to the control flow engine made it possible to add new inspections and fix many old bugs. As a result, PhpStorm understands the code even better.



In some places it will be possible to notice highlighting and warnings from new inspections. Some of them may require your attention as potential sources of bugs. Fixing others will just make the code cleaner.



Here are some examples of inspections.



Variable is always true or false

Example in Parser.php file from doctrine / orm.







Condition is always true

Example in the FormValidator.php file from symfony / form. Instanceof







result is always true

Example in Logger.php file from symfony / monolog-bridge.







$thiswill always be of typeResetInterfacebecause that one is higher in the hierarchy.







Expression is always null

Example in ProcessUtils.php file from symfony / process.







New Action: Type Info (⌃⇧P / Ctrl + Shift + P)

You can hover the caret over any expression and call that action to see what type PhpStorm has inferred for the expression. Available from the View menu | Type Info or by hotkey ⌃⇧P / Ctrl + Shift + P.







Composer improvements



Since version 2020.1, all actions with Composer dependencies can be performed directly in PhpStorm in the composer.json file. There are several improvements in this release.



Support for custom repositories Satis / Packagist

If you specify a custom source in the “repositories” section, then code completion will be available for all packages from it. Package and version information will be cached in PhpStorm.







Addition, when several versions are specified.

If versions are listed separated by commas or pipe ( ||), then PhpStorm will now work correctly and show the available options.







Link to the code and site

Each package in the popup with information has links to sites.







Code quality tools settings

If the list of dependencies contains code quality tools that are supported in PhpStorm, then opposite them there will be a wrench icon to go to the settings.







Improvements to code quality tools



By the way, about quality tools. PhpStorm 2020.2 has some useful additions.



Docker Compose

support PHP_CodeSniffer, PHP CS Fixer and PHP Mess Detector can be run via docker compose.







Exception lists for PHP_CodeSniffer

If there is a custom ruleset.xmlsection "exclude-pattern", now it will be taken into account and files on the specified paths will not be analyzed in the IDE.



Formatting via a remote interpreter

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



Command line tools



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



To do this, add the appropriate tool in the Tools | Command Line Tool Support .



Then, by clicking, the Run anythingCtrl-Ctrl line appears , in which all the commands with auto-completion are available. Starting with this release, you don't even need to configure anything for Symfony, Laravel and Drush. It is enough to open the project, click and start entering the command. ️ For Laravel on macOS, the file must be executable ( ).



Ctrl-Ctrl







artisanchmod +x artisan



In addition, tools can now be run through any remote interpreters (SSH, Docker, Docker Compose, Vagrant). For example, you can quickly test your PHP 8 application by adding a Docker interpreter from an image php:rc-cli.



New refactoring: class extraction



Classes look neat and clean at first. Then more and more new methods and properties are added to them, and sooner or later the class is already "swollen" and overgrown with responsibilities from all sides.



Allocating a bunch of related methods and properties into a new class can help here. This is exactly what the new Extract Class refactoring offers.



To try it, you need to click inside the class Ctrl+Tand select Extract Class . PhpStorm will prompt you to enter the name of the new class, and you can immediately select more methods and classes to retrieve.







This refactoring works for regular functions as well.



And more for PHP



New Inspection: Typed property might be unassigned



If a typed property is declared but not initialized, then an attempt to read it will result in an error TypeError(unless a magic property is defined __get()). PhpStorm will highlight reading from uninitialized properties.







New Inspection: Array used only with write access



Example from file SchemaTool.php from doctrine / orm.







The array $pkColumnsin the method is gatherColumns()updated, but never read.



Customizing generated getters and setters



Now you can customize the method naming by choosing between camelCase()and snake_case().



And also customize the order in which they are added to the class.



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







Correct resolution for many projects in one window



If you open several projects in one window, then you could often see errors that the class is defined several times. And when trying to go to the definition of a class, you still had to choose which one.



PhpStorm 2020.2 doesn't have these bugs and the transition works as expected.







Lower priority for classes from vendor



In the auto-completion list, priority will be given to classes directly from the project, and entities from the folder vendorand PHAR files will go with a lower one.



Add cursor to all selected lines



The new action Add Caret Per Selected Line adds a cursor at the end of each selected line and deselects it. Can be invoked using the ⌥⇧G / Alt + Shift + G keys.







Full support for GitHub pull requests



Basic pull request support appeared in PhpStorm 2018.3. There have been updates since then, but many actions still required switching to the browser.



In PhpStorm 2020.2, all pull request work can be done right in the IDE!







You can access pull requests from the VCS | Git | View Pull Requests , from the toolbar or by clicking Alt+7. The first will be a list of available requests with search and filtering options.



Clicking on a pull request will open detailed information about it: reviewers, tags, changed files, timeline.



The entire flow of the review can be done right there. Start and request a review, comment on changes at the line or commit level, submit a review, merge.



The results of checks, including those from the CI, will be displayed under the timeline.



Previously, to merge a pull request, you had to create a local branch. Now it is not required and you can merge by pressing a button.



OpenAPI support



The OpenAPI Specifications plugin is available for all JetBrains IDEs and works starting from version 2020.2. In the Open API spec files ( openapi.yaml/ openapi.jsonand swagger.yaml/ swagger.json), it provides highlighting, augmentation, validation, and navigation.



In addition, the following options are available:



  • Integration with Swagger UI - renders directly in the IDE window.
  • Code generation.
  • Structural diff for OpenAPI specs - allows you to quickly detect significant changes.
  • Gatter icons for quickly creating requests in an HTTP client.
  • Completion of endpoints in the HTTP client.
  • Rename refactoring - if you rename an endpoint in a spec, then it will automatically be renamed in the request of the HTTP client.








New Inspection Widget



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



Using the arrows, you can quickly jump to the next / previous problem (earlier this could be done with the F2/ keys Shift+F2).



In the widget, you can choose what level of problem will be shown, for example, only syntax errors or all problems.



And by clicking on the widget, a new Problems window will open with a list of all detected problems in the file.







Version control



Git from WSL 2



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



The settings are available in Preferences | Version Control | Git .



️ Git will only work with WSL2, because Git from WSL1 is unstable and can lead to incorrect results from Git commands.



Improved UI for comparing branches



PhpStorm allows you to compare any branches and see which commits went into one or the other. To do this, select any branch from those available in the VCS popup | Branches and select Compare with Current from the context menu .



In PhpStorm 2020.2, the logs and commit differences will be displayed directly in the editor. This allows more information to be displayed on the screen.







Updated command dialogs



Dialogues for the Git Merge, Pull and Rebase commands have been redesigned and unified. Added missing options and now you can see the complete Git command that will be executed.



All commands are available in the VCS | Git .







New action: remove commit



An extra or temporary local commit can now be removed directly from the log in PhpStorm. To do this, select Drop Commit from the context menu on the commit .







New action: merge commits into one (squash)



You can also combine multiple commits into one. For example, it can be convenient to clear history before creating a pull request or before pushing changes. Select



several commits in the log and select Squash Commits ... from the context menu.







For both actions, behind the scenes is done git rebase.



DB Tools



PhpStorm out of the box includes almost all DataGrip features that are covered in the DataGrip 2020.2 release overview from our colleagues.



Web



And, as always, all updates from WebStorm 2020.2 are included in PhpStorm too. For example, using a plugin , Prettier can be used as the default formatter for PHP files as well.






You can download PhpStorm 2020.2 on the “ What's newpage .



And here is a video (in English) showing the main features of the release:





That's all this time. Thanks for reading to the end! We will be glad to have any questions, wishes, bug reports and just thoughts in the comments.



Your JetBrains PhpStorm Team



All Articles