Monday, November 17, 2008

Getting Cyclometric Complexity in PHP

I have some legacy code I am analyzing at the moment. I needed a tool to report Cyclomatic complexity for PHP code.

I found Lint PHP which is a web form in which you can post your code to be evaluated. Now I'm not going to post all my code to some 3rd parties page. Luckily you can download the code.

After installing PHP ...

I used [ XAMPP. Just unzip it made that painless] I put the code in the htdocs directory and visit http://localhost/lint_php.php and there you go .. a local copy is running.

Next problem... I have around 200 pages all spread through a directory structure I want to submit to this form..

This is where Ruby and Mechanize come in.

I found a bug with Mechanize at this point that slowed me down.. :(
You are supposed to be able pass blocks to forms, like so...

... but that didn't work for me.

I finally got the following working..

So.. now I can call

phpcc C:\Projects\PHPStuff\src

and get out some figures.

All this reports is the worst methods complexity and a total for the code submitted.
How useful this is I won't get into, but you can change it to report whatever you like from the list generated in the code.

Hope this helps :)


Unknown said...

I improved lint_php for direct command line usage (no browser needed), now you can analyze a single file (or iterate on a folder through a shell script) without using a browser and a web server.

Pasquale Ceres

Nigel Thorne said...

Great! Thanks.

GitHub Projects