Setup PHP CodeSniffer along with WordPress Coding Standards [ Windows, XAMPP ]

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. 🙁

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.

Image: Flicker
Image: Flicker cc

Install PHP CodeSniffer on Windows through PEAR installer:
Uninstall the old CodeSniffer(if any):

C:\xampp\php>pear uninstall PHP_CodeSniffer
uninstall ok: channel://pear.php.net/PHP_CodeSniffer-1.5.5

Install the latest CodeSniffer using PEAR:

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:

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:

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:

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.

C:\xampp\php>pear upgrade-all

Do a cache clear again:

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:

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:

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)

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.

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:

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.

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:

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

Posted

in

,

by

Comments

3 responses to “Setup PHP CodeSniffer along with WordPress Coding Standards [ Windows, XAMPP ]”

  1. Andrew Avatar

    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.

  2. Marios Alexandrou Avatar

    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

    Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(c:\xampp\php\pear\PHP\CodeSniffer\Standards\WordPress-Coding-Standards,c:\xampp\php\pear\PHP\CodeSniffer\Standards\WordPress-Coding-Standards): The system cannot find the path specified. (code: 3)' in C:\Apps\xampp\php\pear\PHP\CodeSniffer.php on line 2159

    Any suggestions?

  3. Nand Lal Avatar

    Nice, you save my day !!! But how integrate with sublime text 3 ??

Leave a Reply

Your email address will not be published. Required fields are marked *