Hi Everybody! How’s going?
Today I want to tell you one thing that makes me so proud and my eyes shining: see my codes clean, easier to read and to understand by any human that follow the best practices, and in WordPress ecosystem using: Coding Standards.
Actually exists many ways to learn and grow up more and more with your codes.
Today we are going to talk about PHPCodeSniffer, a very useful tool that check your code looking for issues and suggest improvements, most of time you can do automatic fixes with the tool. When we are developing a plugin or theme we can count with the WordPress Coding Standards where you can choose check your code with some options as bellow:
Extended tools extend to best practices as WP recommends.
- WordPress-Core — WordPress core coding standards
- WordPress-Docs — WordPress inline documentation standards
- WordPress-Extra — Extended tools extend to best practices as WP recommends.
- includes WordPress-Core
- WordPress-VIP — regras extentidas para WordPress VIP coding requirements
- includes WordPress-Core
If you are developing with WP I strongly recommend to you check your code with one of the options above.
Let’s go, bellow you can learn how to use the WP Coding Standards, the first step is to install PHPCodeSniffer.
Linux (Ubuntu 14.04):
sudo pear install --alldeps php_codesniffer
Then:
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
Add the wpcs pack into PEAR, don’t forget to update the directories:
phpcs --config-set installed_paths /DIRECTORY/TO/wpcs
Now, to check if is install run the command bellow that will display all the packs installed inside PEAR pack, if correct you will see WordPress-Core, Docs, VIP, etc…
phpcs -i
If is correctly installed it will returns all installed packs. (Maybe you cannot see all these bellow, but you need to see those with WordPress prefix.)
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP
To check you code just run:
phpcs --standard=WordPress-Core wp-load.php
The –standard parameter define which option will follow to check your code (One of those I describe at the beginning of this post). In this example I’m going to use WordPress-Core.
When issues are found that can be automatically fixed, they will show, and you just need to use phpcbf instead phpcs:
phpcbf --standard=WordPress-Core wp-load.php
Mac
Installing the PEAR:
sudo curl -O http://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar
pear upgrade pear
pear upgrade
Installing PHPCodeSniffer:
sudo pear install --alldeps php_codesniffer
Check if it’s installed:
phpcs -i
If is correctly installed will return this info:
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend
In case of the issue bellow:
Warning: include_once(PHP/CodeSniffer/CLI.php): failed to open stream: No such file or directory in /usr/bin/phpcs on line 22 Warning: include_once(): Failed opening 'PHP/CodeSniffer/CLI.php' for inclusion (include_path='.:') in /usr/bin/phpcs on line 22 Fatal error: Class 'PHP_CodeSniffer_CLI' not found in /usr/bin/phpcs on line 25
It’s pretty easy to fix, just open the php.ini file:
sudo vim /etc/php.ini
And add the PEAR pack:
include_path = ".:/usr/share/pear"
Installl the WPCS (WordPress CodeSniffer):
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
Add the wpcs pack into PEAR, don’t forget to update the directories.
phpcs --config-set installed_paths /DIRECTORY/TO/wpcs
Check again:
phpcs -i
If is correctly installed it will return all installed packs. (Maybe you cannot see all these bellow, but you need to see those with WordPress prefix.)
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP
Then:
phpcs --standard=WordPress-Core wp-load.php
The –standard parameter define which option will follow to check your code (One of those I describe at the beginning of this post). In this example I’m going to use WordPress-Core.
When issues are found that can be automatically fixed, they will show, and you just need to use phpcbf instead phpcs:
phpcbf --standard=WordPress-Core wp-load.php
If you got problems using the phpcs command, try again with:
php phpcs.phar --standard=WordPress-Core wp-load.php
To fix:
php phpcbf.phar --standard=WordPress-Core wp-load.php
Final considerations:
In the codes above we are using the file wp-load as example, you will need add the patch of your files, directory… as you want!
Some useful links with more info and details:
https://www.jetbrains.com/help/phpstorm/2016.3/using-php-code-sniffer-tool.html
https://github.com/squizlabs/PHP_CodeSniffer
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
That’s it folks, one more time I strongly recommend this practice, using it you are going to follow the best practices of WordPress Development, learning, getting better and contributing with awesome lines of code. 😉
If you have any kind of doubts or additional info just comment bellow! It will be a pleasure to help or hear you!
I hope you’ve liked this post! Take care. 😉