PHP Code Sniffer (aka PHPCS) is, in my opinion, the most basic tool for professional PHP development. To be fair, I would also add another popular PHP linter, the PHP Mess Detector (PHPMD). If you are not using them, you should start doing it right now.
Linters analyze your code and warn you about bad practices, bad coding styling, and help you to build better and more professional code.
For the PHP Mess Detector, check my tutorial here.
1. Install PHPCS:
Check if phpcs is installed:
phpcs -i (out)Command 'phpcs' not found, but can be installed with: (out)sudo apt install php-codesniffer
Install it globally:
composer global require squizlabs/php_codesniffer
Get the phpcs binary path:
composer global config bin-dir --absolute (out)Changed current directory to /home/daniel/.config/composer (out)/home/daniel/.config/composer/vendor/bin
Add it to your PATH, by editing
~/.profile and adding these lines at the bottom:
# PHPCS export PATH=/home/daniel/.config/composer/vendor/bin:$PATH
Now, you need to do a logout/login. Closing and opening the shell won’t work. You could run a “source ~/.profile”, but it would work just for the opened shell. So, do the logout/login.
2. Check installed coding styles:
After logout/login, check if it is installed:
phpcs -i (out)The installed coding standards are Squiz, PHPCS, Zend, PSR1, PSR2, PEAR and MySource
This package is a life-saver. Without it, you need to manually set up paths, which can lead to accidental misconfigurations and overwriting.
composer global require --dev dealerdirect/phpcodesniffer-composer-installer
4. [Optional] Install extra coding styles system-wide: WordPress, Joomla, etc:
composer global require wp-coding-standards/wpcs:*
It is also recommended to install the
PHPCompatibilityWP package too:
composer global require --dev phpcompatibility/phpcompatibility-wp:*
composer global require --dev automattic/vipwpcs:*
If you get this error: “Could not find a version of package joomla/coding-standards matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.“, decrease your stability requirements to dev.
composer global config minimum-stability dev composer global require --dev joomla/coding-standards:* composer global config minimum-stability stable
Check installed rules:
phpcs -i (out)The installed coding standards are Squiz, Zend, PSR1, PSR2, PEAR, MySource, PSR12, PHPCompatibility, Joomla, Joomla-CMS, Joomla-Stats, PHPCompatibilityParagonieRandomCompat, PHPCompatibilityParagonieSodiumCompat, PHPCompatibilityWP, WordPress-VIP-Go, WordPressVIPMinimum, WordPress-Docs, WordPress, WordPress-Core and WordPress-Extra
4. Get the bin path:
which phpcs (out)/home/daniel/.config/composer/vendor/bin/phpcs
In this example, the bin path is
5. In Atom, install
In Atom, install packages
linter-php by AtomLinter:
Install Packages and Themes >
6. In Atom, install
In Atom, install package linter-phpcs by AtomLinter:
Install Packages and Themes >
Install any dependency (if required):
linter. I know, we have just installed it as a dependency:
Click on Settings to configure linter-phpcs:
Set the bin path and the coding standard you want to use:
And that’s it. Now you will see hints / warnings to improve your code :)