Quantcast
Channel: code-diesel » software
Viewing all articles
Browse latest Browse all 8

Source Code Signatures in PHP

$
0
0

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.


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images