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 –
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
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
This is a quite powerful tool as it helps in detecting –
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 –
phpmd /path/to/source_file.php text /path/to/ruleset_file.xml
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 –
/usr/bin/phpcs
for PHP CodeSniffer, /usr/bin/phpmd
for PHP Mess Detector and /usr/bin/phpcpd
for PHP Copy Paste Detector.PEAR
or PHPCS
in case of CodeSniffer & Select rule set as we have discussed above in the case of PHP Mess DetectorIf 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 ).
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
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.
Use the following command to get proper error report format in NetBeans.
sudo phpcs --config-set report_format full
Comments
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 🙂