PHP Code Comparator

PHP Code Comparator is Command Line Script allowing to compare different versions of a library, framework or package and detect code changes in terms of class existence or method definitions.

If you ever wanted to have a way to quickly  identify what classes have been altered or methods have been changed between two different versions of the same package, PHP Code Comparator will help you achieve that.

Instead of using a diff tool, you can simply use this PHP command line script. It will basically parse a folder recursively, detect any defined classes and extract the various methods and parameters. This will then be performed on the second version of your library. Then, a summary will be displayed showing the differences that have been detected.

This tool will detect if a class has been removed, if new classes have been added, if methods have been removed or added and also if the signature of a method has changed.

Where can I download it ?

You can browse the code and download it from github.com.

http://github.com/alfallouji/PHP-Code-Compare/tree/master/

You can also directly get the source code from the git repository.

git clone git://github.com/alfallouji/PHP-Code-Comparator.git

How can I use it ?

This tool is a simple Command Line Script. Therefore, it can be used by just using the php interpreter.

Usage: php compare.php oldFolder newFolder [-c] [-m] [-a] [-h]

-c, –classes Compare classes
-m, –methods Compare methods
-a, –all Compare all
-h, –help print a summary of the options

Example: php compare.php /var/www/cms1.0/api /var/www/cms2.0/api -a

Comparing the classes of two different version of a project
The following command will compare the classes of version 1.6 and 1.7 of Wordpress.

php compare.php ../compare/wordpress1.6/ ../compare/wordpress1.7/ -c

Here is the result of the comparison.

Code Comparator ver1.0 by Bashar Al-Fallouji

Classes that dont exist anymore…
None…

New classes …
Class OPML_Import @ ../compare/wordpress1.7/wp-admin/import/opml.php
Class WP_Filesystem_SSH2 @ ../compare/wordpress1.7/wp-admin/includes/class-wp-filesystem-ssh2.php
Class WP_Http @ ../compare/wordpress1.7/wp-includes/http.php
Class WP_Http_Fsockopen @ ../compare/wordpress1.7/wp-includes/http.php
Class WP_Http_Fopen @ ../compare/wordpress1.7/wp-includes/http.php
Class WP_Http_Streams @ ../compare/wordpress1.7/wp-includes/http.php
Class WP_Http_ExtHTTP @ ../compare/wordpress1.7/wp-includes/http.php
Class WP_Http_Curl @ ../compare/wordpress1.7/wp-includes/http.php
Class EnchantSpell @ ../compare/wordpress1.7/wp-includes/js/tinymce/plugins/spellchecker/classes/EnchantSpell.php
Class Walker_Comment @ ../compare/wordpress1.7/wp-includes/comment-template.php

Comparing the methods of two different version of a project
The following command will compare the methods of version 1.6 and 1.7 of Wordpress.

php compare.php /var/www/perso/compare/wordpress1.6/ /var/www/perso/compare/wordpress1.7/ -m

Here is the result of the comparison.

Code Comparator ver1.0 by Bashar Al-Fallouji
Comparing methods between V1:”/var/www/perso/compare/wordpress1.6/” and V2:”/var/www/perso/compare/wordpress1.7/”

Class Walker_Category_Checklist @ /var/www/perso/compare/wordpress1.6/wp-admin/includes/template.php
Parameters for method _wp_get_comment_list may have changed
V1: $status, $s, $start, $num
V2: $status, $s, $start, $num, $post, $type

Parameters for method _wp_comment_row may have changed
V1: $comment_id, $mode, $comment_status, $checkbox
V2: $comment_id, $mode, $comment_status, $checkbox, $from_ajax

Parameters for method touch_time may have changed
V1: $edit, $for_post, $tab_index
V2: $edit, $for_post, $tab_index, $multi

Class PHPMailer @ /var/www/perso/compare/wordpress1.6/wp-includes/class-phpmailer.php
Parameters for method EncodeQP may have changed
V1: $str
V2: $input, $line_max, $space_conv

Class Walker_Page @ /var/www/perso/compare/wordpress1.6/wp-includes/classes.php
Parameters for method start_el may have changed
V1: $output, $page, $depth, $current_page, $args
V2: $output, $page, $depth, $args, $current_page

Can I use this code?

Yes, here is a copy of the license.

This Code is released under the GNU LGPL

Please do not change the header of the file(s).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Lesser General Public License for more details.
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

1 Response to PHP Code Comparator

Avatar

Bashar’s Blog » PHP Code Comparator - Detect Code Changes

May 29th, 2009 at 3:36 am

[...] PHP Code Comparator [...]

Comment Form

Who am I?

My name is Bashar Al-Fallouji, I am a Solution Architect at Sense.

I am particularly interested in Web applications, Open Source Development, Software Engineering, Information Architecture, Unit Testing, XP/Agile development, etc.

On this blog, you will find mostly technical articles and thoughts around PHP, OOP, OOD, Unit Testing, etc. I am also sharing a few open source tools and scripts.

  • Bashar: Glad that you liked it ! [...]
  • Angel S. Moreno: well, there goes wasting a couple of hours of development and a couple of days of testing. I owe you [...]
  • Bashar: Thats right, the setSaveFile create a files containing an associative array of classname => filen [...]
  • Loggy: Jim's clarification in particular was pretty useful although I did have to dig down into the tree to [...]
  • Jim Osborne: Got it! It did work 'out of the box' when I extended the test code to instantiate a class within [...]