8

I have a project long time developed in PhpStorm and fully compatible with php 5.6. In any case it work and deployed on server with php 5.6.

How to inspect with PhpStorm 2018 this whole project to php 7.2 compatibility and only highlight those places where php code incompatible with 7.2 only?

mature
  • 701
  • 1
  • 11
  • 20
  • 2
    If it's a specific inspection, use `Code | Run Inspection by Name` and apply it to the whole project/specific folders. If it's more than 1 inspection, create custom Inspection Profile (`Settings/Preferences | Editor | Inspections`) and include only desired inspections (turn off the rest). Then use `Code | Inspect Code` (select that profile there) on desired folders. – LazyOne Jan 13 '19 at 11:43
  • 1
    How can be named inspection to check code on php 7.2 compatibility? I cant find anything similar in `Code | Run Inspection by Name`. – mature Jan 13 '19 at 12:13
  • 4
    It's called "Language Level" ?.. Check at "Settings/Preferences | Editor | Inspections | PHP | General | Language Level`... BTW -- if you have "Php Inspections (EA Extended)" plugin, it provides even more inspections (under "Language level migration" node) – LazyOne Jan 13 '19 at 13:54

2 Answers2

13

You have two options here, which depend on each other.

Install PHP_CodeSniffer

To check your project for 7.2 compatibility I recommend the PHP CodeSniffer. This is a little yet powerful command line program which statically checks your code against predefined coding standards.

Install it via Composer from within your the root level of your project:

$ composer require --dev squizlabs/php_codesniffer

You can also install it globally or as a Phar. Please consult the documentation for alternate installation methods.

Once installed, you can call it via:

$ vendor/bin/phpcs --version // This outputs you the version

As mentioned above, PHPCS comes with ready to use coding standards. Use

$ vendor/bin/phpcs -i to list them.

To check if your code is PSR-2 compatible run:

$ vendor/bin/phpcs --standard=PSR2 .

As you like to check your project for PHP 7.2 compatibility, you have to install this standard: https://github.com/PHPCompatibility/PHPCompatibility

$ composer require --dev phpcompatibility/php-compatibility

Now register the standard in PHPCS. Open your composer.json and add this lines to the scripts section:

"scripts": {
    "post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility",
    "post-update-cmd" : "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility"
}

This will take care if you install/update your dependencies. To register the standard right now, you have to call the script manually:

$ composer run-script post-install-cmd

To check if the new standard is successfully installed run:

$ vendor/bin/phpcs -i

Now you are able to run the check from the cli:

$ vendor/bin/phpcs -p . --standard=PHPCompatibility

Configure PhpStorm to use PHP_CodeSniffer

As you already have configured the PHP Interpreter in PhpStorm, open your Preferences and to to PHP | Quality Tools | CodeSniffer. Click on the ... and type the path to your PHP_CodeSniffer installation. In our case vendor/bin/phpcs and hit Validate. It shows an tooltip with the current version.

PHP_CodeSniffer setting

Now click on OK.

Enable the Inspections

Inside the Preferences go to Editor | Inspections | PHP | Quality tools. Enable the PHP Code Sniffer validation checkbox. Then to the right you find the settings page. You have to select the PHPCompatibility standard from the select field and hit the reload button next to the select. Once done, click OK.

You should now see the errors inside the editor underlined. The severity and the color can be set in the configuration pane we just closed.

Enable PHPCS

Conclusion

Now you have two ways to check your projects code. The CLI-way gives you a better overall overview about the state of your code where the IDE-way can help you while coding to be aware of not using old language constructs.

common sense
  • 3,453
  • 6
  • 20
  • 31
4
  • Open "Settings"
  • Search for "Languages & Frameworks"
  • Under PHP, choose "Composer"
  • Deslect "Synchronize IDE Settings with composer.json"
  • Choose PHP
  • Set "PHP language level" to 7.2

Enjoy :)

MyLibary
  • 1,567
  • 7
  • 15
  • Can you explain: 1. Why I need Deslect "Synchronize IDE Settings with composer.json"? 2. When I set all you suggest, what is next? – mature Jan 13 '19 at 11:52