Skip to content

How to use WordPressCS with Eclipse and XAMPP

Juliette edited this page Aug 20, 2023 · 2 revisions

If you are a WordPress theme/plugin developer who uses XAMPP, PHP, MySQL, and Eclipse for development, these are the steps to use the WordPress Coding Standards along with PHP CodeSniffer in Eclipse.

⚠️ Note from the WordPressCS maintainers ⚠️

PEAR is not the officially supported way of installing WordPressCS. PHP_CodeSniffer has already announced it will stop supporting installation via PEAR in version 4.0.

We do recommend local Composer installation of WordPressCS. The following guide will install an older version of PHP_CodeSniffer and WordPressCS, so take it with a grain of salt.

Step 1. Install the latest PHP CodeSniffer in XAMPP

Note: do not install PHP CodeSniffer via composer! Via Composer it will fail because the XAMPP folder after setup already contains an old CodeSniffer sub-folder inside it, so you will end up with two versions of CodeSniffer. Use the steps below instead:

1.1. Install PEAR:

To be sure you are using the latest PEAR version, not the old one included in XAMPP, download this file into C:\xampp\php\ then run it locally via PHP:

C:\xampp\php>php go-pear.phar

When you are asked for installation type ("system wide" or "locally") choose "locally". Further, use the default settings suggested by the installer.

1.2. Uninstall the old CodeSniffer (if any):

C:\xampp\php>pear uninstall PHP_CodeSniffer

uninstall ok: channel://pear.php.net/PHP_CodeSniffer-1.5.5

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

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

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

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

1.7. Upgrade pear too. In case if any things are old:

C:\xampp\php>pear upgrade-all

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

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

1.10. Navigate to the coding standards directory:

C:\xampp\php>cd pear\PHP\CodeSniffer\Standards

1.11. Fetch the WordPress coding standards into CodeSniffer’s standards directory (if the 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

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

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

Step 2. Install the PHP CodeSniffer plugin for Eclipse

Since the most used PHP code sniffer plugin for Eclipse is not updated yet ( http://phpsrc.org/ ) and Eclipse includes an OLD CodeSniffer version, which will not work with WordPress Coding Standards, you'll have to install this Eclipse plugin instead, which is very intuitive to use:

https://github.com/ramonamorea/com.hevada.eclipse.pti.tool.codesniffer/blob/master/plugins.7z

Unpack the package plugins.7z from the link above, into the dropins folder inside your Eclipse installation folder.


Step 3. Setup the PHP CodeSniffer preferences in Eclipse:

In Eclipse. go to Window -> Preferences -> PHP Tools -> PHP CodeSniffer and set up it like this:

setup eclipse plugin


Step 4. View the sniffer output:

To view the output of the sniffer with WordPress Coding Standards, check (enable) the automatic build from Eclipse menu: Project - Build Automatically, then open the Problems view from: Eclipse menu -> Window -> Show view -> Problems. Wait for the build to complete and all the coding problems will appear in the Problems view.