New inspections in PhpStorm 2018.2

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:

It is important to list PHP extensions your project requires. Not all PHP installations are created equal: some may miss extensions you may consider as standard (such as ext-mysqli which is not installed by default in Fedora/CentOS minimal installation systems). Failure to list required PHP extensions may lead to a bad user experience: Composer will install your package without any errors but it will then fail at run-time. The show --platform command lists all PHP extensions available on your system. You may use it to help you compile the list of extensions you use and require. Alternatively you may use third party tools to analyze your project for the list of extensions used.

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:

Missing extension

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:

Non-canonical order of elements

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:

optional_before_required_inspection

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:

document_root_preferences

Your JetBrains PhpStorm Team
The Drive to Develop

This entry was posted in Cool Feature, Newsletter and tagged , , . Bookmark the permalink.