Phpstorm Codesniffer

Posted : admin On 1/3/2022

File Settings PHP Quality Tools for Windows and Linux
PhpStorm Preferences PHP Quality Tools for macOS
Ctrl+Alt+S

  1. Phpstorm Code Sniffer Not Working
  2. Phpstorm Codesniffer Xml
  3. Phpstorm Codesniffer Code Style
  4. Phpstorm Code Sniffer Docker

In PHPStorm, go to your Settings screen (File-Settings or CTRL+ALT+S) and navigate to Languages and Frameworks - PHP - Code Sniffer. Next to the Local Configuration click the browse button and enter the path to phpcs in the PHP Code Sniffer (phpcs) path field. Hit Validate to make sure PHPStorm. The CodeSniffer itself should reside in d:programphpPEARPHPCodeSniffer - there will be a script autoload.php and a subfolder src. Then in Settings - Languages & Frameworks - PHP - Code Sniffer you specify the path to phpcs.bat and Validate it. PHP Code Sniffer is a PHP5 script that tokenises PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

The page is available only when the PHP plugin is enabled. The PHP plugin is bundled with PhpStorm and activated by default. If the plugin is disabled, enable it on the Settings/Preferences Plugins page as described in Managing plugins.

PHP_CodeSniffer

In this section, choose the PHP_CodeSniffer script to use.

ItemDescription
Configuration Choose the code quality tool script:
  • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.

  • To use a local script, choose Local. In this case the local code quality tool will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for code quality tool because PhpStorm runs a script (phpcs.bat for Windows or phpcs for Linux and macOS) that contains a path to a PHP engine.

  • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings/Preferences dialog, choose By default project interpreter.

Show ignored files

Click to open the PHP_CodeSniffer Ignored Files dialog, which lists the files that PHP_CodeSniffer skips. PhpStorm suggests adding a new file to the list during inspection when waiting for response from the PHP_CodeSniffer exceeds the limit specified in the Tool process timeout field. This is done to prevent slowing down processing. For each file, PhpStorm displays its name and location.

  • To add a file, click and locate the desired file in the dialog that opens.

  • To delete a file from the list and have PHP_CodeSniffer process it again, select the file and click the Remove file button .

  • To remove all the files from the list, click .

Phpstorm Codesniffer

PHP_CodeSniffer Dialog

The dialog opens when you click next to the Configuration list on the PHP_CodeSniffer page.

Use this dialog to configure local PHP_CodeSniffer scripts or scripts associated with remote PHP interpreters as well as configure PHP_CodeSniffer's behaviour. See PHP_CodeSniffer for details.

Code
ItemDescription
PHP_CodeSniffer path

In this field, specify the location of the PHP_CodeSniffer script: phpcs.bat for Windows or phpcs for Linux and macOS.

If the script is associated with a PHP interpreter, PhpStorm detects the path to it and fills in the field automatically, but you can edit it if necessary. Type the path manually or click and select the path in the dialog that opens.

To check that the specified path to phpcs.bat or phpcs ensures interaction between PhpStorm and PHP_CodeSniffer, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the phpcs --version command. If validation passes successfully, PhpStorm displays the information on the detected PHP_CodeSniffer version.

The directory containing the PHP engine executable must be added to the system path. This allows code quality tool scripts execute calls to the system-wide PHP engine.

InterpreterThe field shows the chosen PHP interpreter to use the PHP_CodeSniffer from.
Tool process timeoutIn this field, specify how long you want PhpStorm to wait for a result from PHP_CodeSniffer. If the timeout is exceeded, the process is terminated to prevent excessive CPU and memory usage. This lets you fine tune the PHP_CodeSniffer process behavior depending on the configuration of your computer and the rule sets used.
PHP Code Beautifier and Fixer SettingsIn this area, provide the path to the PHP Code Beautifier and Fixer tool in the Path to phpcbf field. Type the path manually or click and select the path in the dialog that opens.

PHP Mess Detector

In this section, choose the Mess Detector script to use.

ItemDescription
Configuration Choose the Mess Detector script:
  • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.

  • To use a local script, choose Local. In this case the local Mess Detector will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for Mess Detector because PhpStorm runs a script (phpmd.bat for Windows or phpmd for Linux and macOS) that contains a path to a PHP engine.

  • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings/Preferences dialog, choose By default project interpreter.

Show ignored files

Click to open the Mess Detector Ignored Files dialog, which lists the files that Mess Detector skips. PhpStorm suggests adding a new file to the list during inspection when waiting for response from the Mess Detector exceeds the limit specified in the Tool process timeout field. This is done to prevent slowing down processing. For each file, PhpStorm displays its name and location.

  • To add a file, click and locate the desired file in the dialog that opens.

  • To delete a file from the list and have Mess Detector process it again, select the file and click the Remove file button .

  • To remove all the files from the list, click .

Mess Detector Dialog

The dialog opens when you click next to the Configuration list on the Mess Detector page.

Use this dialog to configure local Mess Detector scripts or scripts associated with remote PHP interpreters as well as configure Mess Detector's behaviour. See PHP Mess Detector for details.

ItemDescription
Mess Detector path

In this field, specify the location of the Mess Detector script: phpmd.bat for Windows or phpmd for Linux and macOS.

If the script is associated with a PHP interpreter, PhpStorm detects the path to it and fills in the field automatically, but you can edit it if necessary. Type the path manually or click and select the path in the dialog that opens.

To check that the specified path to phpmd.bat or phpmd ensures interaction between PhpStorm and Mess Detector, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the phpmd --version command. If validation passes successfully, PhpStorm displays the information on the detected Mess Detector version.

The directory containing the PHP engine executable must be added to the system path. This allows code quality tool scripts execute calls to the system-wide PHP engine.

InterpreterThe field shows the chosen PHP interpreter to use the Mess Detector from.
Tool process timeoutIn this field, specify how long you want PhpStorm to wait for a result from Mess Detector. If the timeout is exceeded, the process is terminated to prevent excessive CPU and memory usage. This lets you fine tune the Mess Detector process behavior depending on the configuration of your computer and the rule sets used.

PHP CS Fixer

In this section, choose the PHP CS Fixer script to use.

ItemDescription
Configuration Choose the PHP CS Fixer script:
  • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.

  • To use a local script, choose Local. In this case the local PHP CS Fixer will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for PHP CS Fixer because PhpStorm runs a script (php-cs-fixer.bat for Windows or php-cs-fixer for Linux and macOS) that contains a path to a PHP engine.

  • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings/Preferences dialog, choose By default project interpreter.

Show ignored files

Click to open the PHP CS Fixer Ignored Files dialog, which lists the files that PHP CS Fixer skips. PhpStorm suggests adding a new file to the list during inspection when waiting for response from the PHP CS Fixer exceeds the limit specified in the Tool process timeout field. This is done to prevent slowing down processing. For each file, PhpStorm displays its name and location.

  • To add a file, click and locate the desired file in the dialog that opens.

  • To delete a file from the list and have PHP CS Fixer process it again, select the file and click the Remove file button .

  • To remove all the files from the list, click .

PHP CS Fixer Dialog

The dialog opens when you click next to the Configuration list on the PHP CS Fixer page.

Use this dialog to configure local PHP CS Fixer scripts or scripts associated with remote PHP interpreters as well as configure PHP CS Fixer's behaviour. See PHP CS Fixer for details.

ItemDescription
PHP CS Fixer path

In this field, specify the location of the PHP CS Fixer script: php-cs-fixer.bat for Windows or php-cs-fixer for Linux and macOS.

If the script is associated with a PHP interpreter, PhpStorm detects the path to it and fills in the field automatically, but you can edit it if necessary. Type the path manually or click and select the path in the dialog that opens.

To check that the specified path to php-cs-fixer.bat or php-cs-fixer ensures interaction between PhpStorm and PHP CS Fixer, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the php-cs-fixer --version command. If validation passes successfully, PhpStorm displays the information on the detected PHP CS Fixer version.

The directory containing the PHP engine executable must be added to the system path. This allows code quality tool scripts execute calls to the system-wide PHP engine.

InterpreterThe field shows the chosen PHP interpreter to use the PHP CS Fixer from.
Tool process timeoutIn this field, specify how long you want PhpStorm to wait for a result from PHP CS Fixer. If the timeout is exceeded, the process is terminated to prevent excessive CPU and memory usage. This lets you fine tune the PHP CS Fixer process behavior depending on the configuration of your computer and the rule sets used.

Psalm

In this section, choose the Psalm script to use.

ItemDescription
Configuration Choose the Psalm script:
  • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.

  • To use a local script, choose Local. In this case the local Psalm will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for Psalm because PhpStorm runs a script (psalm.bat for Windows or psalm for Linux and macOS) that contains a path to a PHP engine.

  • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings/Preferences dialog, choose By default project interpreter.

Show ignored files

Click to open the Psalm Ignored Files dialog, which lists the files that Psalm skips. PhpStorm suggests adding a new file to the list during inspection when waiting for response from the Psalm exceeds the limit specified in the Tool process timeout field. This is done to prevent slowing down processing. For each file, PhpStorm displays its name and location.

  • To add a file, click and locate the desired file in the dialog that opens.

  • To delete a file from the list and have Psalm process it again, select the file and click the Remove file button .

  • To remove all the files from the list, click .

Phpstorm Code Sniffer Not Working

Psalm Dialog

The dialog opens when you click next to the Configuration list on the Psalm page.

Phpstorm code sniffer

Use this dialog to configure local Psalm scripts or scripts associated with remote PHP interpreters as well as configure Psalm's behaviour. See Psalm for details.

ItemDescription
Psalm path

In this field, specify the location of the Psalm script: psalm.bat for Windows or psalm for Linux and macOS.

If the script is associated with a PHP interpreter, PhpStorm detects the path to it and fills in the field automatically, but you can edit it if necessary. Type the path manually or click and select the path in the dialog that opens.

To check that the specified path to psalm.bat or psalm ensures interaction between PhpStorm and Psalm, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the psalm --version command. If validation passes successfully, PhpStorm displays the information on the detected Psalm version.

The directory containing the PHP engine executable must be added to the system path. This allows code quality tool scripts execute calls to the system-wide PHP engine.

InterpreterThe field shows the chosen PHP interpreter to use the Psalm from.
Tool process timeoutIn this field, specify how long you want PhpStorm to wait for a result from Psalm. If the timeout is exceeded, the process is terminated to prevent excessive CPU and memory usage. This lets you fine tune the Psalm process behavior depending on the configuration of your computer and the rule sets used.

PHPStan

In this section, choose the PHPStan script to use.

ItemDescription
Configuration Choose the PHPStan script:
  • To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.

  • To use a local script, choose Local. In this case the local PHPStan will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for PHPStan because PhpStorm runs a script (phpstan.bat for Windows or phpstan for Linux and macOS) that contains a path to a PHP engine.

  • To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings/Preferences dialog, choose By default project interpreter.

Show ignored files

Click to open the PHPStan Ignored Files dialog, which lists the files that PHPStan skips. PhpStorm suggests adding a new file to the list during inspection when waiting for response from the PHPStan exceeds the limit specified in the Tool process timeout field. This is done to prevent slowing down processing. For each file, PhpStorm displays its name and location.

  • To add a file, click and locate the desired file in the dialog that opens.

  • To delete a file from the list and have PHPStan process it again, select the file and click the Remove file button .

  • To remove all the files from the list, click .

PHPStan Dialog

The dialog opens when you click next to the Configuration list on the PHPStan page.

Use this dialog to configure local PHPStan scripts or scripts associated with remote PHP interpreters as well as configure PHPStan's behaviour. See PHPStan for details.

ItemDescription
PHPStan path

In this field, specify the location of the PHPStan script: phpstan.bat for Windows or phpstan for Linux and macOS.

If the script is associated with a PHP interpreter, PhpStorm detects the path to it and fills in the field automatically, but you can edit it if necessary. Type the path manually or click and select the path in the dialog that opens.

To check that the specified path to phpstan.bat or phpstan ensures interaction between PhpStorm and PHPStan, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the phpstan --version command. If validation passes successfully, PhpStorm displays the information on the detected PHPStan version.

The directory containing the PHP engine executable must be added to the system path. This allows code quality tool scripts execute calls to the system-wide PHP engine.

InterpreterThe field shows the chosen PHP interpreter to use the PHPStan from.
Tool process timeoutIn this field, specify how long you want PhpStorm to wait for a result from PHPStan. If the timeout is exceeded, the process is terminated to prevent excessive CPU and memory usage. This lets you fine tune the PHPStan process behavior depending on the configuration of your computer and the rule sets used.

CodeSniffer is a tool to detect coding standard violation for PHP, CSS and Javascript. It is really useful to check if the codes you made violate the coding standard you defined. When you work in a team, this tool can help a lot. Today, I'm going to show how to setup CodeSniffer on PHPStorm.

Installation

There are two ways to install CodeSniffer, using PEAR or using Composer. I prefer use Composer for this one. You need to execute command below to install CodeSniffer

[php] composer global require 'squizlabs/php_codesniffer=*' [/php]

We install CodeSniffer as global command so PHPStorm can always make reference to it. This command will install CodeSniffer in this path /Users/masbugan/.composer/vendor/bin/phpcs

Setup on PHPStorm

Phpstorm Codesniffer Xml

Here is how to insert reference for CodeSniffer

  1. Go to menu Preferences
  2. Choose PHP -> Code Sniffer
  3. Insert the CodeSniffer path to the input text and click Validate
  4. Click Apply and you are done

Change CodeSniffer Setting

To make CodeSniffer run, you need to set PHPStorm to use it when inspect codes. Here is the step

Phpstorm Codesniffer Code Style

  1. Go to Preferences
  2. Go to Inspections -> PHP
  3. Check the PHP Code Sniffer Validation
  4. You can also define coding standard you want there.

Test CodeSniffer

CodeSniffer can be executed via menu Code -> Inspect Code. You can choose to inspect whole project or just current file. Then PHPStorm will list the inspection result.

Summary

Phpstorm

Phpstorm Code Sniffer Docker

That is it how to setup CodeSniffer on PHPStorm. You will be really helped with this tool to make your codes always match with defined coding standard.