Set up PHP Code Sniffer & Mess Detector inspections in PhpStorm

Before we start, you'll need to set up a CLI Interpreter to map to the Docker container. You can do this by going to Preferences > PHP, and then under CLI Interpreter click the triple dot icon:

Next, create a new CLI interpreter, selecting "From Docker..." from the prompt. Then select Docker Compose as the type, select both docker-compose.yml and docker-compose.dev.yml as the configuration files, then select the "phpfpm" service:

This will create an interpreter that looks similar to:

Due to a current bug in PhpStorm, you cannot set Lifecycle to "Connect to existing container", so be sure it is set to "Always start a new container" until this bug is resolved. This leads to many Docker containers created during development, which are not harmful but annoying. You can clear these orphaned containers at any time by running "docker container prune".

Be sure your project path mappings are set by going to Preferences > PHP, then confirm the "Path mappings" value maps from your project root src directory maps to /var/www/html on the container:

Next, ensure the following inspections are enabled by checking their appropriate boxes under PhpStorm > Preferences > Editor > Inspections:

  • PHP CS Fixer validation
  • PHP Mess Detector validation
  • PHP_CodeSniffer validation

Now, go to PHP > Quality Tools, and assign the PHP_CodeSniffer Configuration to the phpfpm interpreter, like so:

Next, click the triple dot icon next to the Configuration dropdown, then create a new setting for the "PHP Code Sniffer by Remote Interpreter", with configuration that matches the following:

The "PHP_CodeSniffer path" should be set to: /var/www/html/vendor/bin/phpcs

The "Path to phpcbf" should be set to: /var/www/html/vendor/bin/phpcbf

You can also click the Validate button to ensure the binaries were located:

You can also set up similar configurations for PHP CS Fixer as well as Mess Detector:

Lastly, we need to set the configuration files for inspections. Let's set up PHP Code Sniffer first.

Select that inspection under Preferences > Editor > Inspections > PHP > Quality tools, and under Options > Coding standard, select Custom:

Then, click the triple dot icon next to Custom to set the path to the Magento default result. You'll want to set the path to the ruleset, referencing dev/tests/static/framework/Magento/ruleset.xml within your local host project directory:

We'll do the same thing for PHP CS Fixer, selecting the "Custom" ruleset, then assign it the path to our local directory's .php_cs.dist file:

And then also for Mess Detector, assigning it to a Custom Coding Standard to the dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml location in our local path:

Complete and Continue