Tuesday, 27 September 2011

Want to be a web developer? (part 5)

Improving your code

Now you have your lovely application, can you improve it? Here are some tools that can check your code for fine tuning. Note: Some of the following tools require pear, to install pear use

sudo apt-get install php-pear

phpcpd - duplicate code

phpcpd - this is a command line tool that scans your code for duplicate code. Install using pear
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear install phpunit/phpcpd
Usage :

phpcpd --log-pmd phpcpd.xml yourprojectfolder

This will generate a file called phpcpd.xml with all the duplicate code. There are other output options too, plain text for example.

phpcs - coding standards

phpcs - command line too that will check for coding standards

pear install PHP_CodeSniffer

Usage :

phpcs --report=xml --report-file=phpcs.xml yourprojectfolder

phpmd - mess detector

phpmd - This will look for "messy code", one of the best command line tools

Install using pear

pear channel-discover pear.phpmd.org
pear channel-discover pear.pdepend.org
pear install --alldeps phpmd/PHP_PMD


phpmd yourprojectfolder xml codesize,unusedcode,naming,design --reportfile phpmd.xml


Kcachegrind is a very useful tool for analysing profile results. This will highlight processes that are taking too long or are using too many resources. Install kcachegrind and xdebug if you haven't done so already

sudo apt-get install php5-xdebug
sudo apt-get install kcachegrind

Modify your php.ini file to include the following.

; Profiler Settings for kcachegrind copied from http://www.odino.org/5/profiling-with-xdebug-and-kcachegrind
; and http://xdebug.org/docs/profiler
xdebug.profiler_output_dir = "/var/benchmark/"

Then after running your application, you'll find cachegrind files in /var/benchmark. Open these by going to Applications -> Programming -> Kcachegrind

phpunit - unit tests

PHPUnit tests provide a great way to ensure you are getting the results you should be getting. This can be checked constantly so any code changes can be checked to see if they match the tests. For setting up this, refer to the Netbeans unit test documentation.

No comments:

Post a Comment