Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm 2018 - how to inspect whole project to php 7.2 compatibility?

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?

like image 341
mature Avatar asked Jan 13 '19 11:01

mature


People also ask

How to change PHP language level PhpStorm?

You can set the language level for your project on the PHP page of the Settings/Preferences dialog ( Ctrl+Alt+S ).

How do I know if PHP is compatible?

Install the PHP version you want to test and run php -l file. php to test if the file passes the lint check. If PHP is unable to parse the file, it will tell you. In general, you should be aware of which features became available in which version.

What version of PHP can I use with PhpStorm?

With PhpStorm, you can develop applications in PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, PHP 7, PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, and PHP 8.1. This topic outlines the coding assistance features available for each language version. You can set the language level for your project on the PHP page of the Settings/Preferences dialog ( Ctrl+Alt+S ).

How do I enable code inspections in PhpStorm?

For the complete list of code inspections available in PhpStorm, refer to Code inspection index. In the Settings/Preferences dialog ( Ctrl+Alt+S) , go to Editor | Inspections. You can also press Ctrl+Alt+Shift+H and select Configure Inspections in the popup that opens. Use to filter the inspections list.

How to check if your PHP is compatible with PHP 7?

If you’re looking for a “one-click” compatibility check, the PhpStorm IDE is your best option. 2. The PHP 7 Migration Assistant Report (MAR) The second easiest solution is the GitHub project called PHP 7 MAR. It’s a simple PHP project. Once you download it, you can use your own PHP environment to check any file or folder containing PHP.

Does PhpStorm automatically detect the language version of each interpreter?

Although the language version of each interpreter is detected automatically, you can still tell PhpStorm to provide you with coding assistance that corresponds to a different language level. However, if you attempt to use a code construct that is not supported by the specified language level, PhpStorm suggests a Switch to PHP <version> quick-fix.


2 Answers

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.

like image 158
common sense Avatar answered Oct 18 '22 17:10

common sense


  • 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 :)

like image 38
MyLibary Avatar answered Oct 18 '22 17:10

MyLibary