The other day, I had some problem setting up latest WordPress coding standard for PHP CodeSniffer on a Windows machine. It was throwing some errors/warnings like the below. There were some others, but eventually we had fixed some of those. However the WP coding standards were not getting listed in phpcs. 🙁
1 2 3 |
Warning: file_put_contents(C:\php\pear\data/PHP_CodeSniffer/CodeSniffer.conf): failed to open stream: No such file or directory in C:\xampp\php\pear\PHP\CodeSniffer.php on line 2166 |
Here are the steps to fix the problems and setup PHP CodeSniffer along with WordPress Coding Standards namely WordPress, WordPress-Core, WordPress-Extra and WordPress-VIP. FYI, I am on a Windows 7 box with XAMPP setup for Apache, MySql & PHP.
Install PHP CodeSniffer on Windows through PEAR installer:
Uninstall the old CodeSniffer(if any):
1 2 3 4 |
C:\xampp\php>pear uninstall PHP_CodeSniffer uninstall ok: channel://pear.php.net/PHP_CodeSniffer-1.5.5 |
Install the latest CodeSniffer using PEAR:
1 2 3 4 5 |
C:\xampp\php>pear install —alldeps PHP_CodeSniffer No releases available for package “pear.php.net/PHP_CodeSniffer” install failed |
It failed, that means we need to clear the cache:
1 2 3 4 5 |
C:\xampp\php>pear clear–cache reading directory C:\Users\user\AppData\Local\Temp\pear\cache 10 cache entries cleared |
Again, try installing the latest version of the PHP_CodeSniffer package:
1 2 3 4 5 6 7 8 9 10 |
C:\xampp\php>pear install —alldeps PHP_CodeSniffer WARNING: channel “pear.php.net” has updated its protocols, use “pear channel-update pear.php.net” to update Package “pear.php.net/PHP_CodeSniffer” dependency “pear.phpunit.de/PHP_Timer” has no releases downloading PHP_CodeSniffer–1.5.5.tgz ... Starting to download PHP_CodeSniffer–1.5.5.tgz (412,025 bytes) ................................................................done: 412,025 bytes install ok: channel://pear.php.net/PHP_CodeSniffer-1.5.5 |
Update the channel as asked above:
1 2 3 4 5 |
C:\xampp\php>pear channel–update pear.php.net Updating channel “pear.php.net” Update of Channel “pear.php.net” succeeded |
Upgrade pear too. Incase if any things are old.
1 2 3 |
C:\xampp\php>pear upgrade–all |
Do a cache clear again:
1 2 3 4 5 |
C:\xampp\php>pear clear–cache reading directory C:\Users\user\AppData\Local\Temp\pear\cache 382 cache entries cleared |
See the list of default coding standards in PHP CodeSniffer:
1 2 3 4 |
C:\xampp\php>phpcs –i The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend |
Install WordPress coding standard definitions for the CodeSniffer:
Navigate to the coding standards directory:
1 2 3 |
C:\xampp\php>cd pear\PHP\CodeSniffer\Standards |
Fetch the WordPress coding standards into CodeSniffer’s standards directory. (if git command not working on your command prompt, you may use any gui for git clone)
1 2 3 |
C:\xampp\php\pear\PHP\CodeSniffer\Standards>git clone https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git |
As, all of the individual standards of WordPress namely WordPress, WordPress-Core, WordPress-Extra and WordPress-VIP are in one directory, for phpcs to be able to detect the standards we need to add the path to phpcs’s installed_paths.
1 2 3 |
C:\xampp\php>phpcs —config–set installed_paths c:\xampp\php\pear\PHP\CodeSniffer\Standards\WordPress–Coding–Standards |
Check if all the installed standards are listed now:
1 2 3 4 |
C:\xampp\php>phpcs –i The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress–Core, WordPress–Extra and WordPress–VIP |
How to use PHP_CodeSniffer:
You can use command prompt to check your code, or you can also configure the same with different IDEs like NetBeans, PHPStrom etc.
1 2 3 |
C:\xampp\php>phpcs —standard=WordPress D:\project\wptest\wp–content\plugins\gallerytest\controllers\media\galleryview.php |
You can write the output of code sniffer to a file:
1 2 3 |
C:\xampp\php>phpcs —standard=WordPress D:\project\wptest\wp–content\plugins\gallerytest\controllers\media\galleryview.php > D:\project\wptest\wp–content\plugins\gallerytest\controllers\media\galleryview.txt |
3 responses to “Setup PHP CodeSniffer along with WordPress Coding Standards [ Windows, XAMPP ]”
Hey thanks a lot for this tutorial. I was also facing this issue had no clue about how to solve it. After reading your tutorial it was easy for me to setup php codesniffer along wordpress coding standards.
I just ran through this tutorial and all was good until I run phpcs -i following this line phpcs –config-set installed_paths c:\xampp\php\pear\PHP\CodeSniffer\Standards\WordPress-Coding-Standards. At which point I get this error
Any suggestions?
Nice, you save my day !!! But how integrate with sublime text 3 ??