Source Code Signatures in PHP


This post is based on Ward Cunningham’s Signature Survey: A Method for Browsing Unfamiliar Code article. Ward suggests that when studying new and unfamiliar source codes, it is vital that a programmer get a intuitive sense of the code structure under consideration. One method he suggests is that of summarizing on a single line select punctuation characters from each source file. He calls this the file’s “signature”. This will help the developer gauge the complexity of the code base.

So for example, if we only consider using braces ‘{}’, and semicolon ‘;’ – one of the common punctuation elements in a PHP program, a programmer can get a feel for the structure of a source file. Usually braces delimit blocks and functions in PHP, and many other languages, so this can give a programmer a sense of the length of code blocks or function sizes.

A sample source file taken from a WordPress system, and its signature using select punctuation characters ( ‘{}’ and ‘;’ ) is given below.

Sample WordPress source file – wp-blog-header.php:

<?php
/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
 
if ( !isset($wp_did_header) ) {
	$wp_did_header = true;
	require_once( dirname(__FILE__) . '/wp-load.php' );
	wp();
	require_once( ABSPATH . WPINC . '/template-loader.php' );
}
?>

Signature of the above file:

wp-blog-header.php : {;;;;}

This shows that the file has one block section (function, class or other), containing four statements. Of course to get a quick intuitive feel for the signature, you will have to work with at least a few diverse code bases. This will prime you to estimate the complexity of the code by just looking at the signature.

If we also include parenthesis ‘()’ in the signature, we get the following. But this looks a little complex and less intuitive than the previous one.

wp-blog-header.php : (()){;(());();();}

Some more signatures of various WordPress root files is given below.

index.php : ;;
wp-atom.php : ;;;
wp-blog-header.php : {;;;;}
wp-comments-post.php : {;;;;};;;;{;;};;{;;}{;;}{;;}{;;}{;};;;;;{;;;;
{{;;}}}{;};{;;};;;;;{;;;;};;;;
wp-commentsrss2.php : ;;;
wp-config-sample.php : {};;;;;;{};;;;;;;;;;;;;
wp-config.php : ;{};;;;;;{}}{{{;{};;{;;{;{};{{{;{;}{;;;;;;
wp-cron.php : ;;;{;};;;;{;{{;{;;};;}}};
wp-feed.php : ;;;
wp-links-opml.php : ;;;;{;;};;;;;;;;;;;;;;;;
xmlrpc.php : ;;{;};;{;;;;};;;;;;{;{;;;;;};};;;;;
wp-load.php : ;;{;}{;}{;;;;;;;;;;;;}
wp-pass.php : ;;;;;
wp-rdf.php : ;;;
wp-register.php : ;;;
wp-rss.php : ;;;
wp-rss2.php : ;;;
wp-login.php : ;{{;;}{;;}}{;;;;;;;;;;;;;;;;{;;;;{;}{;}}{{;}};;{;;;}{;;
;};;{;;}{;;{;{;;}};;}}{;;;{;;}{};;;}{;;{;{;}{;{;;}}};{;}{;}{;;{{;};}{{
;;}{}}}{;;;;;};}{;;;{;;}{;;};;{;;};;;;;;;;{;;;};;;;;;;;;;;;;}{;;;;;;;}
{;;;}{;;;{;}{;;}{;}{;}{;;;}{;};;;;;{;;;};;;};;;;;;;{;;;};;;;;{;;;;;;{;
{;;;}};;;;;;;;;;;;;{;;};{;}{;;;;};;;;;;;;;;;{;;}{;;};;{;;;{;;;}};;;;;;
;;;;;{;{{;;}}}{;;}{;};;;;{{;;{};;;}{;;;};;};;;;;;;;;;;;;;;;;;{}{;}{;}{
{{{;;}{;{;}};;}{}};}{;};;;}
wp-mail.php : ;;;;;;;;;;;;;;{;;;};;{;;;;;;;;;;{;{;}{{;;;;{;;};}{;;;;;}
{;;;}{;;{;}{;};;}{;;;{;;{;}{;;}}{;}}{;;{;};;;;;;;;;;{{;}};;;;;}}}{;;}{
;};{;;{;;};};;{;}{;};;;;;;;;;;;;;;;{;;;}{;}};
wp-settings.php : ;;;;;;;;;;;;;;;;;;;;;;;;;;;;{;;}{;};;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;{;;;};{;};{{;};};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;{;
;};;;;;{{;;};};
wp-signup.php : ;;;{;;}{;};{;}{;;}{;;};{{;;}{;}{;;;;;}{;;;}{;;}{;;;;}{;
}{;;;;;}};;;{;;;{};;{;;;}{};;{};{};;}{;;;}{;{;};;;{}{;};}{;}{;;{;};;;;;
{;};;{{;;}};;;}{;;;;;{;;};;;;;;}{{}{}{};;;}{;;;;;;;;;;;{}{}{}}{;;{;;}{;
;};;;}{;;}{;;;;;;;;;;}{;;{;;};;{;;};;;;;;}{{}{}{};;;;;};;;;;;;;;;;;{;}{
;;;;}{;{;;;;;;;;;;;;;;{;;;};}};;
wp-trackback.php : {;;}{;;{;;;;;;}{;;;;}};{;;};;;;;;;;{;;;};;;;;{;;}{;;
;;;;;;;;;;;;;;;}
wp-activate.php : ;;;{;;};;{;};{{;}{;;}{;}{;}{;;;;;}};;{;}{;;{{;;;{;;
;}{;;;};}{;;}}{;;;;;;;;;;}};;;

To generate the signatures yourself, you can use the following shell script or a PHP program. The shell script is shown below.

for i in *.php; do
    echo -n "$i: "
    sed 's/[^{};]//g' $i | tr -d '\n'
    echo 
done

And below is a PHP version to do the same.

<?php
 
/* Get all PHP files in the current directory */
$dir = './*.php';
 
/* Use the SPL Iterator class */
$files = new GlobIterator($dir);
 
/* Get the signature of each file */
foreach ($files as $file) {
    $data = file_get_contents($file);
    preg_match_all('/[{};]/',$data,$out);
    $signature = '';
 
    foreach($out[0] as $line) {
        $signature .= $line;
    }
 
    echo $file->getFilename() . " : " . $signature . "\n";
}

Furthermore, we can make the output more attractive and user friendly, by outputting it to HTML, with links to each source file.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

4 Responses

1

Ryan

December 4th, 2011 at 7:57 pm

Very technical, but really informative. Thanks for the info.

2

BriffErag

November 10th, 2012 at 11:12 am

I’m really enjoying the theme/design of your site. Do you ever run into any internet browser compatibility issues? A couple of my blog visitors have complained about my blog not working correctly in Explorer but looks great in Chrome. Do you have any solutions to help fix this problem?

3

free signature create

November 24th, 2012 at 7:53 am

Every ωeekend i useԁ to рay a quick νisit thiѕ web
site, as i want enϳoyment, as thіs this web pagе conаtions actually
pleasant funnу mateгial too.

4

Programowanie w PHP » Blog Archive » Sameer Borate’s Blog: Source Code Signatures in PHP

May 13th, 2014 at 4:51 pm

[...] this new post to his blog Sameer Borate applies an interesting method of discovery of structure in unfamiliar [...]

Your thoughts