PHP Code Review tools help you in following a proper coding standard while you are coding. These tools review certain things in your code such as –
- Unneccessary spacing
- Proper line breaks wherever needed
- Escaping PHP variables whenever they are displayed in html tags
- Code indentations
- Unused variables in functions
- Proper commenting of code
Installation & Usage
1) PHP CodeSniffer
To install CodeSniffer run the below given command ( with root privileges )
sudo pear install PHP_CodeSniffer
Once you have successfully installed it, Run the below given command to check for coding standard related issues and warning in your php file
phpcs /path/to/source_file.php
2) PHP Copy Paste Detector
This tool detects for repeated codes in your php file or project. Run the below commands to install Copy Paste Detector
sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/phpcpd
To check for repeated codes in your whole project, Use the below command
phpcpd /path/to/<project_folder>
To check for code repetion in a single file, you can use
phpcpd /path/to/source_file.php
3) PHP Mess Detector
This is a quite powerful tool as it helps in detecting –
- Cyclomatic complexity of functions
- Unused code blocks or variables
- Naming conventions of class,functions and variables
To install PHP Mess Detector, run the below commands –
sudo pear channel-discover pear.phpmd.org
sudo pear channel-discover pear.pdepend.org
sudo pear install --alldeps phpmd/PHP_PMD
Once you have installed PHP MD, You can check for the above three points all together using the following command
phpmd /path/to/source_file.php text codesize,unusedcode,naming
PHP MD accepts three parameters –
- First is the path to the source file,
- Second is the output format you want ( text, xml or html ) and
- Third is the ruleset on which you want to test your code. You can even create your own ruleset file and pass it instead of the third parameter to have your own 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 with the help of a plugin named phpcsmd. To install it, Go to Tools -> Plugins and search for phpcsmd and install it. Restart your IDE and you are good to go!
After the plugin is successfully installed, You need to configure it from Tools -> Options -> PHP -> PHPCSMD
tab
There are two basic configurations required –
- 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. - Standard / Rules – Select
PEAR
orPHPCS
in case of CodeSniffer & Select rule set as we have discussed above in the case of PHP Mess Detector
Install WordPress Coding Standard
If you want to follow WordPress Coding Standards you can install it by running the below command
git clone git://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git $(pear config-get php_dir)/PHP/CodeSniffer/Standards/WordPress
Once the installation is completed, You will be able to see that WordPress is also added to the list apart from PEAR and PHPCS.
Once you are done with the above settings, Then open any file or project you want to scan. Go to Tools and select show phpcsmd annotations. After this click on any project folder from the tree view and click on scan for violations from the Tools menu. Once the scanning is completed, You will see that all the lines having issues with coding standard will be marked in different color ( depeding on the type of issue ) pointing out the issue as shown in the image below.
Similarly, we can see the metrics for a particular code using a tool named PHP Depend. Go to Tools -> scan with Pdepend. A new tab will open and show all the metrics of the selected file in terms of Lines of codes, Cyclomatic complexity, Commented lines of code etc.
Happy Coding!
[ Update – 7th Feb 2014 ]
This update is regarding Netbeans 7.4 as we have already discussed setting up of code review tools in Netbeans 7.3
Unlike to the older version, Netbeans 7.4 comes with the inbuilt Code Analysis part and there is no need to install any additional plugin(s) to run code analysis ( though you still need phpcs, phpmd and other required tools installed on your system ).
Configuration
Go to Tools -> Options -> PHP, and you will see the Code Analysis tab as shown below.
The rest of the configuration is same as Netbeans 7.3
Usage
Once you have configured it correctly, Now its time to run the inspection on a complete PHP project or a file.
Select any project ( Source ) and then click on Source -> Inspect as shown below
Once you click on Inspect, a dialog will ask on whether which tools you want to run on your project. You can either run all the tools at once or even go for running one at a time. The below image shows that we are running only the CodeSniffer tool for the project.
Once you are done, the inspection process will inspect all your PHP files in the project and then the output window will show all the errors and warnings in a tabular format as shown below.
Netbeans 7.4 provides a much better and clean code review as compared to the previous version, and it is highly recommended.
Updated
Use the following command to get proper error report format in NetBeans.
sudo phpcs --config-set report_format full
By default netbeans 7.4 uses its regular format to show the error report. We can change it to full with following command.
sudo phpcs --config-set report_format full
This will make error reports easy to read as shown in the last image above.
Thanks Harish for pointing out 🙂