PHP Code Sniffer - ben600324/wiki GitHub Wiki

composer patchPHPCS composer addCodeSniffs

If you go to File > Preferences > Settings

search for "phpcs exec"

vendor/squizlabs/php_codesniffer/bin/phpcs

You can set up PHP code sniffer in the local workspace using composer and vendors.

Adding CodeSniffer to your project

Change to the root folder of your project: cd ~/platforms/[your project]/docroot

Add the requirements via composer for dev only:

composer require --dev squizlabs/php_codesniffer composer require --dev drupal/coder

Add the Drupal coding standards to PHPCS: ./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer

You can check the standards are now installed with: ./vendor/bin/phpcs -i The installed coding standards are PSR12, Zend, MySource, PSR2, Squiz, PEAR, PSR1, Drupal and DrupalPractice

Automating with composer

I have also added the following to composer scripts: "addCodeSniffs": "./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer"

So you can now run the following to add the Drupal standards via composer: composer addCodeSniffs

VSCode Configuration

Install the PHPCS extension. In VSCode, update the workspace or local settings with: "phpcs.executablePath": "./vendor/squizlabs/php_codesniffer/bin/phpcs.bat" "phpcs.standard": "Drupal,DrupalPractice",

Now anyone using VSCode can just run composer addCodeSniffs, load the workspace and PHPCS will work with no config.

Installing PHP Code Sniffer on your Windows work machine Using the above method for composer in a project works, but it can be very slow over a network. We have found installing PHPCS locally and pointing VSCode to the local source runs much faster. To make your CodeSniffer run fast follow the guide below:

Before beginning, ensure you that you have the following installed: PHP 7 Composer (global install) - https://getcomposer.org/download/

Now run the following in a CMD shell:

composer global require drupal/coder composer global require squizlabs/php_codesniffer composer global show -P

Add the composer bin directory to your path like so (again in CMD):

PATH=%PATH%;C:\Users{Your User Name}\AppData\Roaming\composer\vendor\bin

Now open Visual Studio Code. Hit Ctrl+Shift+X to open the extensions window and search for "phpcs". The only result should be an extension by Ioannis Kappas. Install it.

Now go to your user settings.json file and add the following line, quotes included, and make sure to leave a comma after the previous line:

"phpcs.executablePath": "C:\Users\{Your User Name}\AppData\Roaming\Composer\vendor\bin\phpcs.bat"

NB: PHP Code Sniffer only works on remote codebases when you have mapped a network drive to the location. Make sure any remote .php files you have open are using the network drive path (eg. Z:\platforms\schools\docroot\blah\blah\myfile.php). Using regular network paths (eg. \my.remote.site.\codebase) will not work.

Installing PHP Code Sniffer on the aegir development server You can test that your work conforms to Drupal Coding Standards with PHPCS but this may not work for your user initially.

The following information has been taken from https://www.drupal.org/node/1419988 and converted for use with our Aegir development environment.

If you are seeing errors please run the following commands: Note: First make sure your are logged in as the correct user, do not make these changes to any system users. composer global require drupal/coder composer global show -P

Note: The output path of the above command was /home/$USER/.config/composer/vendor/drupal/coder, if you get different results you will need to adjust the following paths to fit.

Update your ~/.profile path to include: export PATH="$PATH:$HOME/.config/composer/vendor/bin"

Note: You may need to exit your SSH session and reconnect for the above change to take effect.

Register the Drupal and DrupalPractice Standard with PHPCS: phpcs --config-set installed_paths ~/.config/composer/vendor/drupal/coder/coder_sniffer

To test it works and the Drupal standards are registered run: phpcs -i

Command line usage Find all commands here: https://www.drupal.org/node/1587138

phpcs --standard=Drupal ~/platforms/drupal8/docroot/profiles/charity/themes/ultimate

Trouble shooting If you are experiencing issues with Code Sniffer check which version of phpcs you are running and try updating your global composer: phpcs --version It should be at 2.7.0 - if not edit your global config: nano ~/.config/composer/composer.json Update so it looks a bit like this (added a php_codesniffer version):

{ "require": { "drupal/coder": "^8.2", "symfony/console": "^3.2", "squizlabs/php_codesniffer": "2.7.0" } } Now run updates like so: composer global update Because the phpcs versions has changed you will need to re-register the Drupal standards: phpcs --config-set installed_paths ~/.config/composer/vendor/drupal/coder/coder_sniffer

Source: https://www.drupal.org/node/2809335