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.
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:
CodeSniffer directory path:
CodeSniffer directory path:
- Use below command from command line to use it for individual files:
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:
phpcs --standard=<standard-name> <path-to-your-PHP-source-directory> phpcs --standard=PEAR testproject
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.
- To use it, open the extracted directory WebCodeSniffer from the web browser.
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.
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:
- 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.
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.
phpcs --standard=WordPress file_name.php phpcs --standard=WordPress wordpress_project1
As WebCodeSniffer has Joomla, Drupal, CakePHP standards, those directories can be directly copied to the below path to use them from CLI.
Keep coding clean and beautiful website as per their language-specific coding standard guideline.
Thanks Satej 🙂
Thanks for sharing 🙂
Thanks for commenting 🙂
great Sandeep want to see this at work 😉