PHP Code Review: Ensuring Quality Code

PHP Code Review tools are essential for maintaining coding standards while developing in PHP. These tools scrutinize various aspects of your code, such as:

  • Unnecessary spacing
  • Proper line breaks where necessary
  • Escaping PHP variables when displayed in HTML tags
  • Code indentations
  • Unused variables in functions
  • Proper code commenting

Installation & Usage

1) PHP CodeSniffer

To install CodeSniffer, execute the following command with root privileges:

sudo pear install PHP_CodeSniffer

After successful installation, use the command below to check for coding standards-related issues and warnings in your PHP file:

phpcs /path/to/source_file.php

2) PHP Copy Paste Detector

This tool identifies repeated code in your PHP file or project. Install Copy Paste Detector using the commands provided:

sudo pear config-set auto_discover 1
sudo pear install

To check for repeated code in your entire project, use the following command:

phpcpd /path/to/<project_folder>

Similarly, to check for code repetition in a single file, use:

phpcpd /path/to/source_file.php

3) PHP Mess Detector

PHP Mess Detector is a powerful tool that helps detect:

  • Cyclomatic complexity of functions
  • Unused code blocks or variables
  • Naming conventions for classes, functions, and variables

To install PHP Mess Detector, execute the following commands:

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

Once PHP MD is installed, you can check for the three points mentioned above using the following command:

phpmd /path/to/source_file.php text codesize,unusedcode,naming

PHP MD accepts three parameters:

  • The first is the path to the source file.
  • The second is the desired output format (text, XML, or HTML).
  • The third is the ruleset on which you want to test your code. You can even create your own ruleset file and pass it as the third parameter for a custom ruleset.
phpmd /path/to/source_file.php text /path/to/ruleset_file.xml

Integrating with Netbeans

All the above tools can be easily integrated into Netbeans using a plugin called phpcsmd. To install it, navigate to “Tools” -> “Plugins” and search for phpcsmd. Install the plugin and restart your IDE.

See also  Sending USD to GCash: Everything You Need to Know

phpcsmd installation

After successful installation, configure the plugin from “Tools” -> “Options” -> “PHP” -> “PHPCSMD” tab.

Two basic configurations are required:

  1. Script location: This will be /usr/bin/phpcs for PHP CodeSniffer, /usr/bin/phpmd for PHP Mess Detector, and /usr/bin/phpcpd for PHP Copy Paste Detector.
  2. Standard/Rules: Select either PEAR or PHPCS for CodeSniffer, and select the desired ruleset for PHP Mess Detector, as discussed above.

Install WordPress Coding Standard

If you want to follow WordPress Coding Standards, run the following command to install it:

git clone git:// $(pear config-get php_dir)/PHP/CodeSniffer/Standards/WordPress

Once the installation is complete, you will see WordPress added to the list, alongside PEAR and PHPCS.

Configuration PHPCSMD

Once you have completed the above settings, open any file or project you wish to scan. Go to “Tools” and select “Show PHPCSMD Annotations”. Then, click on any project folder from the tree view and select “Scan for Violations” from the “Tools” menu. After the scanning process is complete, you will notice that lines with coding standard issues are marked with different colors, highlighting the specific issues.


Similarly, you can view metrics for a particular code using a tool named PHP Depend. Navigate to “Tools” -> “Scan with Pdepend”. A new tab will open, showing metrics such as Lines of Code, Cyclomatic Complexity, Commented Lines of Code, and more.

Happy Coding!

[Update – 7th Feb 2014]

This update pertains to Netbeans 7.4. Unlike the previous version, Netbeans 7.4 includes an inbuilt Code Analysis feature, eliminating the need to install additional plugins for code analysis (although you still need to have phpcs, phpmd, and other required tools installed on your system).

See also  PHP Microservices Framework


Navigate to “Tools” -> “Options” -> “PHP” and you will see the “Code Analysis” tab, as shown below.

Code Analysis Netbeans 7dot4

The rest of the configuration remains the same as in Netbeans 7.3.


Once properly configured, you can run inspections on a complete PHP project or individual files. Select the desired project (source) and click on “Source” -> “Inspect”, as shown below.

Inspect Netbeans 7dot4

A dialog will prompt you to select which tools you want to run on your project. You can run all the tools at once or select them one by one. The image below shows that we are only running the CodeSniffer tool for the project.

Inspect Click

Once the inspection is complete, the output window will display all the errors and warnings in a tabular format.

Output Window CodeSniffer Netbeans 7dot4

Netbeans 7.4 provides a much-improved and clean code review compared to previous versions, making it highly recommended.


To obtain proper error report formatting in NetBeans, use the following command:

sudo phpcs -config-set report_format full

For more information, visit ProgramMatek.