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
@u01jmg3
Copy link
Author

u01jmg3 commented Oct 20, 2014

  • Test against the following:
    • PHPCS should report 46 errors
    • PHPMD should report multiple errors
<?php
    //Save as index.php
    function test ($x ,$y)  {
        $y=array(1,2,);
        $z=ARRAY (
            0=>1,
        )
        ;

        if  ($x = '' and 1==array(1,2,))
            $x = "x";
        elseif(0==1)  {
            $y=-1 ;
        }

        $y = 'x'.'x';

        echo($y) ;


    }

Once fixed -- with PHPCBF and manually -- it will look like this

<?php
    /**
     * Save as index.php
     *
     * @param  string $x
     * @param  string $y
     * @return void
     */
    function test($x, $y)
    {
        $y = [1, 2];
        $z = [
            0 => 1,
        ];

        if ($x == '' && 1 == [1, 2]) {
            $x = 'x';
        } else if (0 == 1) {
            $y = -1;
        }

        $y = 'x' . 'x';

        echo $y;

    }

@u01jmg3
Copy link
Author

u01jmg3 commented Feb 1, 2016

Checking Code for PHP 5.6 Compatibility

  • Assuming composer is installed run the following from a command prompt
    • This will downgrade php_codesniffer to 2.3.2 for use with php-compatibility
      • (:warning: Any custom sniffs will be lost if you have a version of php_codesniffer different to 2.3.2)
composer global require "squizlabs/php_codesniffer=2.3.2" "wimg/php-compatibility=dev-master"

  • Download PHP 5.6 sniffs to your computer
    • Extract the contents to C:\Users\%USERNAME%\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer\Standards
    • Rename the folder from PHPCompatibility-5.6 to PHPCompatibility (i.e. remove -5.6)

  • Navigate to the root of the folder to be scanned and open a command prompt
    • Check compatibility by running the following command
      • (The final in the command can be replaced by a folder name)
del "phpcs-report.txt" 2>NUL & phpcs -n --extensions=php --report-file="phpcs-report.txt" --standard=PHPCompatibility .

  • Open and review phpcs-report.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment