PHPCS and PHPCBF are two well-know PHP scripts used in the PHP development to (PHPCS) detect violations of a defined coding standard, and (PHPCBF) to automatically correct coding standard violations.
I usually use both with composer in my WordPress projects, adding them as dependency in the composer.json file.
But sometimes I need to check some individual PHP file without adding PHPCS and PHPCBF as dependency. In this post, I am going to explain how I do this.
I follow the instructions from the WordPress Coding Standards for PHP_CodeSniffer project to do a standalone installation.
I am going to install the PHP_CodeSniffer project and the WordPress Coding Standards for PHP_CodeSniffer in the ~/code/utils. To do this, I access the folder, and then I clone both projects:
cd ~/code/utils git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs
Finally, I add a new folder where PHPCS will look for the WordPress standards.
cd phpcs ./bin/phpcs --config-set installed_paths ../wpcs
Now you can see the code standards installed executing:
./bin/phpcs -i The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
I add these two alias to be able to execute in my local path. I add the “wp” prefix, so this alias doesn’t interfere with another PHPCS installed with composer:
alias wpphpcs="~/code/utils/phpcs/bin/phpcs --standard=WordPress " alias wpphpcbf="~/code/utils/phpcs/bin/phpcbf --standard=WordPress "
Now, I access to the folder of the file I want to fix or check:
cd ~/code/wordpress/my-project
And then I can execute the PHPCBF or the PHPCS utility:
wpphpcbf class-stats.php F 1 / 1 (100%) PHPCBF RESULT SUMMARY --------------------------------------------------------------------------------------------------------------------------------------------- FILE FIXED REMAINING --------------------------------------------------------------------------------------------------------------------------------------------- /Users/amieiro/code/wordpress/my-project/class-stats.php 207 47 --------------------------------------------------------------------------------------------------------------------------------------------- A TOTAL OF 207 ERRORS WERE FIXED IN 1 FILE ---------------------------------------------------------------------------------------------------------------------------------------------
To show the warnings and errors, I execute:
wpphpcs class-stats.php
And to show only the errors, I execute:
wpphpcs -n class-stats.php
Thanks for sharing this! I think there is a typo here: `alias wpphpcbf=”~/code/utils/phpcs/bin/phpcs “` should be phpcbf.
Thank you for the feedback, Alex. I had updated the command in my .zshrc file, but not here. Updated!
Thank you for this!
Finally found I want. Thanks
Finally found What I need. Thanks