Code Analysis and Formatting Tools for PHP and Magento

Code analysis and formatting tools for PHP and Magento: PHP CodeSniffer, PHP Mess Detector and PHP Copy Paste Detector
created by on 2013-02-26

PHP CodeSniffer

PHP CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

Installing PHP CodeSniffer

Use “pear” to install php_codesniffer:

sudo pear install php_codesniffer

Screenshot of the php_codesniffer installation with pear

After PHP CodeSniffer has been installed you have to install the rules. For example for Magento you can install PHPCS Magento Rules:

Get the CodeSniffer installation directory:
<!– language: lang-sh –>

pear list-files PHP_CodeSniffer

Result of pear list-files for PHP_CodeSniffer

In my case the the installation folder of PHP-CodeSniffer is:


Once you know the installation folder you can clone “phpcs-magento-rules” from github into the “Standards/Made” folder of your CodeSniffer installation:

sudo git clone /usr/share/php/PHP/CodeSniffer/Standards/Made

Cloning Made into your CodeSniffer folder

You can check if your installation was successfull with the “phpcs -i” command. If the command lists “Made” your installation was successful:

phpcs -i

Checking the Made installation with phpcs -i

Integrating PHP-CodeSniffer with Netbeans

Integrate PHP-CodeSniffer with Netbeans IDE using Florian Wolter’s phpcsmd plugin for Netbeans:

  1. Goto Tools → Plugins → Available Plugins
  2. Type in “phpcsmd”
  3. Check “install” for the plugin with the name “phpcsmd” from Normal Specht
  4. Click “install”

Integrating PHP-CodeSniffer with Netbeans IDE

After you have installed the “phpcsmd” plugin you can display an “Action Items” window:

Menu → Window → Action Items

Integrating PHP-CodeSniffer with Netbeans IDE

PHP Mess Detector

It is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.

Installing PHP Mess Detector

Use “pear” to install [(]PHP MD - PHP Mess Detector](

sudo pear channel-discover
sudo pear channel-discover
sudo pear install --alldeps phpmd/PHP_PMD

Now you have to download the [Magento ruleset)( for PHP Mess Detector to your rulesets folder “/usr/share/php/data/PHP_PMD/resources/rulesets”.

sudo curl -s -o /usr/share/php/data/PHP_PMD/resources/rulesets/magento-ruleset.xml

Downloading the a Magento ruleset for PHP Mess Detector from github using curl

PHP Copy Paste Detector (PHPCPD)

A Copy/Paste Detector (CPD) for PHP code:

Installing PHP Copy/Paste Detector

You can install PHPCPD using pear:

sudo pear config-set auto_discover 1
sudo pear install

Installing PHPCPD with pear

Netbeans Configuration

After you have installed PHP CodeSniffer (PHPCS), PHP Mess Detector (PHPMD) and PHP Copy/Paste Detector (PHPCPD) you should check your Netbeans configuration under

Menu → Tools → Options → PHPCSMD

PHP CodeSniffer (PHPCS)

Netbeans Configuration for PHP-CodeSniffer

PHP Mess Detector (PHPMD)

Netbeans Configuration for PHP Mess Detector

PHP Copy/Paste Detector (PHPCPD)

Netbeans Configuration for PHP Copy/Paste Detector

Code-Sniffer, Mess-Detector and Copy-Paste Detector in Action

Using PHP-CodeSniffer, PHP-Mess-Detector and PHP-Copy-Paste-Detector in Netbeans:

Fork allmark on GitHub