A good program doesn't have only good logic and structure, but nice and readable codes as well. Of course, you can refactor the look of your codes manually, but there are tools to take care of that for you, like Code Sniffer.

First, let us help you with the installation of Code Sniffer.

There are many ways to install Code Sniffer, but the most preferable way, that we recommend, is using PEAR.

Open your terminal then run these commands in order to install PEAR

 

sudo apt-get update

sudo apt-get install php-pear

 

Then install Code Sniffer with this command

 

pear install PHP_CodeSniffer

 

All done! Now you have Code Sniffer available system-wide.

 

There are many coding standards for PHP such as PSR1, PSR2, … and some of the most common standards have been installed by default.

Go to /usr/share/php/PHP/CodeSniffer/Standards if you want to have a look at these installed standards.

 

 

To check for code standard flaws, for example, key in the following command line

 

phpcs -n  --standard=PSR2  --extensions="php" /path/to/code

 

This will check for codes with severity of warning to 0 (-n), standard of PSR2 and files with .php extension. The command will produce something like this

 

 

There are errors that can be fixed automatically using code beautifier or phpbcs in Code Sniffer. For example:

 

phpcs --standard=EcgM2 -s  --report=summary --warning-severity=3 /path/to/code

 

If you want to uncover advanced Code sniffer usage, visit the following link for the full guide:

https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage

Happy coding!