PHP Code Sniffer (aka PHPCS) is, for me, the most essential tool for professional PHP development.
PHPCS warns you about bad practices, bad coding styling, and help you to build better and more professional code.
PHPCS is a linter, a class of tools to assist you when writing code.
TIP
The second most essential tool for me is another popular PHP linter, the PHP Mess Detector (PHPMD). If you are not using them, start it right now. Check my PHPMD tutorial here
Check if phpcs is installed:
phpcs -i
Command 'phpcs' not found, but can be installed with: sudo apt install php-codesniffer
Install it globally with composer
:
composer global require squizlabs/php_codesniffer
Get the phpcs binary path:
composer global config bin-dir --absolute
Changed current directory to /home/daniel/.config/composer /home/daniel/.config/composer/vendor/bin
In this example, the binary path is /home/daniel/.config/composer/vendor/bin
.
Add it to your PATH environment variable, by editing ~/.profile
and adding these lines at the bottom:
# PHPCS
export PATH=/home/daniel/.config/composer/vendor/bin:$PATH
WARNING
Change /home/daniel/.config/composer/vendor/bin
with your actual phpcs binary path.
Now, do a logout/login. Closing and re-opening the shell won't work. You could run a "source ~/.profile", but it would work just for the opened shell. So, do a logout/login.
After logout/login, check if the command is installed:
phpcs -i
The installed coding standards are Squiz, PHPCS, Zend, PSR1, PSR2, PEAR and MySource
phpcodesniffer-composer-installer
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
composer global require wp-coding-standards/wpcs:*
It is also recommended to install the PHPCompatibilityWP (opens new window) package too:
composer global require --dev phpcompatibility/phpcompatibility-wp:*
composer global require --dev automattic/vipwpcs:*
composer global require --dev joomla/coding-standards:*
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.
Then, 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
phpcs -i
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
which phpcs
/home/daniel/.config/composer/vendor/bin/phpcs
In this example, the bin path is /home/daniel/.config/composer/vendor/bin/phpcs
.
linter
and linter-php
packagesIn Atom, install packages linter
and linter-php
by AtomLinter:
Ctrl+Shift+P
> Install Packages and Themes
> linter-php
linter-phpcs
packageIn Atom, install package linter-phpcs
by AtomLinter:
Ctrl+Shift+P
> Install Packages and Themes
> linter-phpcs
Install suggested dependencies:
Disable 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 😃
Just add phpcs:disable <RULE>
as a comment before your class/method/command:
// phpcs:disable Squiz.Commenting.FunctionComment.Missing
function myFunctionWithNoCommentBlock() {
}
To find the rule name, you can manually run phpcs
on the command line, with the -s
argument. Example:
phpcs -s my-file.php --standard=PSR2