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 :)

Tuesday, November 11, 2008

Matching Ruby Regular Expressions Across Lines

Just a quick note: x.matches(/something.*/) will match all of one line.
To match all lines x.matches(/something.*/m) you need to put the 'm'.

Nice!

I found it here.

Wednesday, October 29, 2008

Transform a Twiki Table

I needed to change a TWiki table to make the rows become columns and columns become rows.

Ruby + 5 mins =

table = <<-TABLE
...paste table here...
TABLE

rows = (table.split "\n").collect { |line| line.split("|") }
puts (0.. rows[1].length).each { |col| puts "|#{rows.collect{ |row| row[col] }.join("|")}|" }

Ahhhh.. That could have been painful.

I love being a programmer. How do people that don't code handle this sort of problem?

Document-oriented Data Storage

There seems to be new interest in the concept of storing data as documents instead of tables. This is the approach that Lotus Notes took. Here is what I ahave found out so far...

A 'document' is basically a hashtable. You can store values with keys. Solutions then seem to take the Google "Map Reduce" approach to querying the data.

CouchDB allows you to define views. A view contains the results of executing a specified javascript function against each document. This is equivolent to the Google 'Map' step.

RDDB (a ruby implementation) provides for both steps...
  # First create an database object
database = Rddb::Database.new

# Put some documents into it
database << {:name => 'John', :income => 35000}
database << {:name => 'Bob', :income => 40000}
database << {:name => 'Jim', :income => 37000}

# Create a view that will return the names
database.create_view('names') do |document, args|
document.name
end

# The result of querying will return an array of names
assert_equal ['John','Bob','Jim'], database.query('names')


"Map Reduce" scales really well across machines, so I am keen to see where this goes.

I'll let you know what I find out.

Anyone got any pointers?

Tuesday, October 28, 2008

Dos Prompt for Long Paths

If you have to work with DOS at some point you find the display muddled due to deep directory trees cluttering your display.

A simple solution is setting the environment variable PROMPT=$p$_$+$g

This just adds a newline to the end of the prompt.. but makes all the difference.

Thanks to Florin Lazar for this tip

GitHub Projects