Every PhpStorm release brings new inspections among other features, and PhpStorm 2018.2 is no exception.
The new inspections will detect extensions that are not listed in composer.json, highlight the non-canonical order of elements in PHPDoc annotations, and warn you when an optional parameter is used in a method declaration before the required one.
Let’s take look at them in more detail.
Missing PHP extensions in composer.json
The new Extension is missing in composer.json inspection will detect the usages of functions, constants, and classes that rely on PHP extensions not currently listed in composer.json.
The best explanation why listing required extensions is a good idea comes from Composer itself:
Now, as soon as an unlisted usage is detected in your code, PhpStorm will display a warning. You can press Alt+Enter and use the Open composer.json in editor intention action to address the issue right away:
Non-canonical order of PHPDoc elements
When adding the @property , @param , or @var tags to a PHPDoc block, a widely accepted standard is to provide the element’s type before its name. While doing the opposite won’t produce an error, keeping docblocks in your code consistent is certainly a good thing.
The new Non-canonical order of elements inspection and the corresponding quick-fix will aid with that. Simply press Alt+Enter and invoke the quick-fix to flip the elements’ order:
Note that the inspection is disabled by default. To enable it, locate it under Preferences | Editor | Inspections | PHP | PHPDoc and select the checkbox next to it.
Using optional parameters before the required ones
A function in PHP can be declared with some of its arguments having default values. When such a function is called, providing these arguments is optional: in this case, their default values are used.
There’s a catch though: you only need to declare such optional arguments after the required ones. Doing the opposite will eventually lead to problems.
PhpStorm 2018.2 includes the new Optional before required parameter inspection, which will highlight such problematic function declarations for you:
One more thing
PhpStorm provides paths completion for functions using file paths as arguments (for example, require or include ). And in situations when such a path cannot be resolved, the Unresolved include inspection has got you covered.
In the previous PhpStorm versions, however, the commonly used $_SERVER[‘DOCUMENT_ROOT’] expression has always resolved to the project root, which in many cases didn’t reflect the actual project structure and resulted in false positive errors. PhpStorm 2018.2 handles this issue: navigate to Preferences | Preferences | Languages & Frameworks | PHP. Then, switch to the Analysis tab and set the document root to any folder of your choice:
Your JetBrains PhpStorm Team
The Drive to Develop