
If you're not using PHP Code Sniffer, you're missing out on an essential tool that keeps your code clean, consistent, and compliant with industry standards. Whether you're working on a personal project or contributing to open-source platforms like Drupal, maintaining proper coding standards ensures better readability, fewer errors, and smoother collaboration.
In this guide, we'll walk you through installing PHP Code Sniffer, using it to enforce coding standards, and even automating code cleanup to fix common issues like spacing inconsistencies and unnecessary line breaks.
What is PHP Code Sniffer?
Great question! According to its official GitHub repository, PHP Code Sniffer is…
PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.
An example output when phpcs is run and configured to follow PEAR code standard on a php file;
phpcs --standard=PEAR /path/to/code/myfile.php
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
51 | ERROR | Missing function doc comment
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------
You can also check a whole directory!
phpcs --standard=PEAR /path/to/code
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
51 | ERROR | Missing function doc comment
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------
FILE: /path/to/code/yourfile.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
21 | ERROR | PHP keywords must be lowercase; expected "false" but found
| | "FALSE"
21 | WARNING | Equals sign not aligned with surrounding assignments
--------------------------------------------------------------------------------
Pretty exciting! Now you see the issues with the code, you can manually correct it! Why, hell NO! There is a command to do that!
I want to clean up my code after creating a new module for Drupal. Notice how changing the "standard" flag will tell phpcbf what standard to follow. You can change this flag to any supported coding standard.
phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /path/to/drupal/example_module
As you can see, PHP Code Sniffer is a must have tool in your box. It can create an efficient workflow. Cutting down on development time.
So, let's get to installing PHP Code Sniffer.
How to install
1. Installation.
Use composer to install the package.
composer require "squizlabs/php_codesniffer=*"
In Drupal 11+, install the Coder module. When you install this module it will include PHP Code Sniffer as a dependancy.
composer require drupal/coder
2. Test Installation.
Test the installation by running the command
phpcs -i
There will be a list of standards that you are able to use, showing PHP Code Sniffer works as it should.
Shorten commands
Now that everything is working, let's create some aliases to shorten our commands. Below are example alias commands for Drupal. We’ll use sed to insert these aliases into the appropriate shell profile, making them available for use. It's important to select the correct profile file. To determine which shell you're using, run: echo $SHELL
sed -i '$a\
alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md,yml'"\
alias drupalcsp="phpcs --standard=DrupalPractice --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md,yml'"\
alias drupalcbf="phpcbf --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md,yml'"' .example
Now that you have created the aliases, refresh your shell source .yourshellfile
.
If everything was done correctly, you should be able to run the full commands with a simple entry.
Conclusion
PHP Code Sniffer is an invaluable tool for maintaining clean, standardized code—whether you're working on a personal project or contributing to open-source platforms like Drupal. By automating code quality checks and enforcing best practices, it helps prevent inconsistencies, improves readability, and streamlines collaboration.
With the ability to both detect and fix coding standard violations, PHP Code Sniffer not only saves time but also enhances your development workflow. By setting up convenient command aliases, you can make the process even more efficient, reducing repetitive typing and ensuring compliance with Drupal’s coding standards effortlessly.
Now that you’ve installed and configured PHP Code Sniffer, it's time to put it into action! How do you use PHP Code Sniffer in your projects? Do you have any favorite aliases or workflow improvements? Let us know in the comments!