Skip to content

Instantly share code, notes, and snippets.

@u01jmg3
Last active December 20, 2020 16:44
Show Gist options
  • Save u01jmg3/d1336edb9681b3c0341a to your computer and use it in GitHub Desktop.
Save u01jmg3/d1336edb9681b3c0341a to your computer and use it in GitHub Desktop.
PHP Codesniffer, Code Beautifier & Mess Detector Setup

Composer Installation

  • Install Composer - https://getcomposer.org/Composer-Setup.exe
    • ℹ️ You may need to enable OpenSSL in your PHP installation by going directly to ./wamp/bin/php/php-5.x.x/php.ini and enabling the OpenSSL extension
      • These instructions assume you have WAMP installed already in order to provide your technology stack
    • To install globally the necessary Composer packages paste this command into a terminal and hit Enter ↵
      • ℹ️ The openssl extension must be enabled within php.ini of the PHP build you are using with the command line
      • (Start → Run → Type: cmd to open a command prompt)
    • Add C:\Users\%USERNAME%\AppData\Roaming\Composer\vendor\bin to the PATH System Variable
      • Start → Run → control sysdm.cpl,,3 → Environment Variables
    • To check your installation is configured properly, open a terminal and type in composer --version
      • This should return something similar to Composer version 1.6.5 2018-05-04 11:44:59
    • Download and extract coding-standards/PHP/CodeSniffer/CustomPHPCS and place at C:\Users\%USERNAME%\Documents\
      • ℹ️ Remember to unzip the zipped content!

Running PHPCS (≥ 3.0.0)

  • Go to the root of the project you want to sniff and open a command prompt (not Git Bash)
    • (Start → Run → Type: cmd to open a command prompt)
  • Optionally, change the period (at the end) to scan against whatever folder/file name you like
    • e.g. includes, index.php
del "phpcs-report.txt" 2>NUL & phpcs -s --colors -d memory_limit=1G --ignore=*/vendor/* --extensions=php --report-file="phpcs-report.txt" --standard="C:/Users/%USERNAME%/Documents/CustomPHPCS" .

Running PHPCBF (≥ 3.0.0)

  • PHP Code Beautifier (PHPCBF) can be run to automatically fix the errors detailed in the report. In the report an [x] will denote which errors it can fix.
phpcbf -n -d memory_limit=1G --ignore=*/vendor/* --extensions=php --standard="C:/Users/%USERNAME%/Documents/CustomPHPCS" .
  • To fix only a specific sniff use the following command.
phpcbf -n -d memory_limit=1G --ignore=*/vendor/* --extensions=php --standard="C:/Users/%USERNAME%/Documents/CustomPHPCS" --sniffs="CustomPHPCS.Strings.ConcatenationSpacing" .

Running PHPMD

  • Go to the root of the project you want to mess detect and open a command prompt (not Git Bash)
    • (Start → Run → Type: cmd to open a command prompt)
  • Optionally, change the period to scan against whatever folder/file name you like
    • e.g. includes, index.php
phpmd . html "C:/Users/%USERNAME%/Documents/phpmd-ruleset.xml" > phpmd-report.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment