Skip to content
Home » PHP CodeSniffer – A tool to help writing codes as per coding standard guideline.

PHP CodeSniffer – A tool to help writing codes as per coding standard guideline.

php-codesniffer

What is PHP Code Sniffer:

  • A package for syntax checking.
  • It can check code against defined rules, covering anything from white space through document comments to variable naming conventions and beyond.
  • Available from PEAR.

Installation and use (Command line interface):

  • Use below command from command line to install it.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    pear install PHP_CodeSniffer
    // For Mac use below:
    sudo brew install php-code-sniffer
    // If above command gives error as "No releases available for package "pear.php.net/PHP_CodeSniffer" then run "pear clear-cache" to clear cache and again run above command.
    pear install PHP_CodeSniffer // For Mac use below: sudo brew install php-code-sniffer // If above command gives error as "No releases available for package "pear.php.net/PHP_CodeSniffer" then run "pear clear-cache" to clear cache and again run above command.
    pear install PHP_CodeSniffer
    
    // For Mac use below: 
    sudo brew install php-code-sniffer
    
    // If above command gives error as "No releases available for package "pear.php.net/PHP_CodeSniffer" then run "pear clear-cache" to clear cache and again run above command.

    Installation path should be:
    For Mac:
    CodeSniffer directory path: 

    /usr/local/php5/lib/php/PHP/CodeSniffer
    /usr/local/php5/lib/php/PHP/CodeSniffer
        phpcs path: 
    /usr/local/bin/phpcs
    /usr/local/bin/phpcs
    For Ubuntu:
        CodeSniffer directory path: 
    usr/share/php/PHP/CodeSniffer/Standards/
    usr/share/php/PHP/CodeSniffer/Standards/
        phpcs path: 
    /usr/bin/phpcs
    /usr/bin/phpcs

  • Use below command from command line to use it for individual files:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    phpcs --standard=<standard-name> <path-to-your-PHP-source-file>
    phpcs --standard=Zend file_name.php
    phpcs --standard=PEAR file_name.php
    phpcs --standard=<standard-name> <path-to-your-PHP-source-file> phpcs --standard=Zend file_name.php phpcs --standard=PEAR file_name.php
    phpcs --standard=<standard-name> <path-to-your-PHP-source-file>
    phpcs --standard=Zend file_name.php
    phpcs --standard=PEAR file_name.php
  • Use below command from command line to use it for whole directory:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    phpcs --standard=<standard-name> <path-to-your-PHP-source-directory>
    phpcs --standard=PEAR testproject
    phpcs --standard=<standard-name> <path-to-your-PHP-source-directory> phpcs --standard=PEAR testproject
    phpcs --standard=<standard-name> <path-to-your-PHP-source-directory>
    phpcs --standard=PEAR testproject

    NOTE:
    CLI has limited standard selection by default which is MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend.
    Other standards can be used from the command line by downloading the required standard for it which is explained below in the section of ‘Installation and use of other standards manually’.

Installation and use (WebCodeSniffer – a web interface for PHP Code Sniffer):

  • Use below link to download the WebCodeSniffer and extract it to the projects directory that you want to sniff.
    http://sourceforge.net/projects/quickeasyphp/files/WebCodeSniffer/
  • To use it, open the extracted directory WebCodeSniffer from the web browser.
    Like: http://localhost/webcodesniffer/
    Which lists all file and directory where WebCodeSniffer directory is present. A select drop down appears on the right side for each file to select the coding standard for the file to sniff it.

    NOTE:
    UI has some more options by default like Joomla, Drupal, and CakePHP which is not present in the CLI version.
    Check this online version: http://www.webcodesniffer.net/#online_version

Installation and use of other standards manually (like WordPress):

  • Use the below link to download the zip file of WordPress coding standard:
    https://github.com/mrchrisadams/Wordpress-Coding-Standards
  • Extract the zip to the directory where another coding standard exists in pear.
  • For XAMMP the path will be: xamppphpPEARPHPCodeSnifferStandards
  • If the Standard directory does not exist then first you need to install it using below code from command line.
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    pear install PHP Code Sniffer
    pear install PHP Code Sniffer
    pear install PHP Code Sniffer
  • Rename the directory to WordPress and all its files like Sniffs, ruleset.xml etc should directly be present inside it.
  • Use the below command from command line to sniff your WordPress file or directory.
    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    phpcs --standard=WordPress file_name.php
    phpcs --standard=WordPress wordpress_project1
    phpcs --standard=WordPress file_name.php phpcs --standard=WordPress wordpress_project1
    phpcs --standard=WordPress file_name.php
    phpcs --standard=WordPress wordpress_project1

    NOTE:
    As WebCodeSniffer has Joomla, Drupal, CakePHP standards, those directories can be directly copied to the below path to use them from CLI.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    C:xampp/php/PEAR/PHPCodeSniffer/Standards
    C:xampp/php/PEAR/PHPCodeSniffer/Standards
    C:xampp/php/PEAR/PHPCodeSniffer/Standards

Keep coding clean and beautiful website as per their language-specific coding standard guideline.

6 thoughts on “PHP CodeSniffer – A tool to help writing codes as per coding standard guideline.”

Leave a Reply

Your email address will not be published. Required fields are marked *