Wednesday, March 17, 2010

Check how much your PHP code meets the standards ?

Generely we easily check for parse errors in a file using the PHP command line interface and the -l (lowercase L) option.
$ php -l /path/to/code/myfile.inc
No syntax errors detected in /path/to/code/myfile.inc

So code is fine and why do I need check code standered ?

Coding standards  make our code easier to read and maintain, especially when multiple people are working on the same application. we want to ensure to follow some set of coding standards,in my previous company , every SVN commits to email all team members so we fulfill the code review process but sometimes still issues are left. we need some more deep level to review code.

PHP_CodeSniffer is a solution. It's quick and easy way to do that. PHP_CodeSniffer is a replacement for the more manual task of checking coding standards in code reviews. With PHP_CodeSniffer, we can reserve code reviews for the checking of code correctness. if we use it in a regular time interval , we ensures some couple of positive things like
1. Team learn the coding standards and make less mistakes in the future.
2. Team can decide if a coding standard doesn't fit a particular piece of code.

Install and How to use:-  it's pear package so easy to install and we can use it as command line.

phpcs -n --report=summary /var/www/html/shiksha/

Another good tool is PHPMD. it scans PHP source code and looks for potential problems such as possible bugs, suboptimal code or overcomplicated expressions. we can get HTML output of Report as well.


Install and How to use:-  it's pear package so easy to install and we can use it as command line.
phpmd ~/shiksha_head/marketing/  html codesize

Third one is PHPCPD. it is a Copy/Paste Detector (CPD) for PHP code. it's funny tool.

fourth and final one is PHP_Depend. it performs static code analysis on a given source base. Static code analysis means that PHP_Depend first takes the source code and parses it into an easily processable internal data structure. This data structure is normally called an AST (Abstract Syntax Tree), that represents the different statements and elements used in the analyzed source base. Then it takes the generated AST and measures several values, the so called software metrics. what are the software metrics ? They are just the sum of some statements or code fragments found in the analyzed source.
it's also provide some graphical charts that tells about percentage of metrics that current code is followed.

Install and How to use:-  it's pear package so easy to install and we can use it as command line.
pdepend --summary-xml=/tmp/summary.xml   --jdepend-chart=/tmp/jdepend.svg    --overview-pyramid=/tmp/pyramid.svg    /home/raviraj/shiksha_head/marketing/

we have seen some good tools that are really helpful for automated quality testing of code. i have run all these tests in my current  project

Happy coding !!!

No comments:

Post a Comment