Search each class for function names that match except for the underscore prefix

Bill Kar­win posts a use­ful lit­tle snip­pet that will list and search each class for func­tion names that match except for the under­score pre­fix, pri­vate / pro­tected func­tions.

< ?php
/**
* Find methods that differ only by the underscore prefix.
* by Bill Karwin August 2010
*
* I release this code under the terms of the New BSD License:
* http://framework.zend.com/license/new-bsd
*/

// Change this to suit your envi­ron­ment
define(“LIBRARY_DIR”, “/Users/bill/Library/PHP/ZF/library”);

// Pre-load some files to sat­isfy the autoloader.
// We could also add the local PEAR library dir to the autoloader.
require_once(“PHPUnit/Framework/SelfDescribing.php”);
require_once(“PHPUnit/Framework/AssertionFailedError.php”);
require_once(“PHPUnit/Framework/Assert.php”);
require_once(“PHPUnit/Framework/Test.php”);
require_once(“PHPUnit/Extensions/Database/DataSet/ITable.php”);
require_once(“PHPUnit/Extensions/Database/DataSet/AbstractTable.php”);
require_once(“PHPUnit/Extensions/Database/DataSet/IDataSet.php”);
require_once(“PHPUnit/Extensions/Database/DataSet/AbstractDataSet.php”);
require_once(“PHPUnit/Extensions/Database/ITester.php”);
require_once(“PHPUnit/Extensions/Database/AbstractTester.php”);

require_once(LIBRARY_DIR . “/Zend/Loader/Autoloader.php”);
Zend_Loader_Autoloader::getInstance();

// Find every PHP file under the library dir and slurp them in.
// Yes that’s a lot of files. Deal with it.
$Direc­tory = new RecursiveDirectoryIterator(LIBRARY_DIR);
$Iter­a­tor = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, ‘/^.+\.php$/i’); fore­ach ($Regex as $file­name) {
require_once($filename);
}

// Loop over each class now in the PHP run­time.
// Fil­ter by classes named Zend*.
$classes = get_declared_classes();
$zend­classes = new RegexIterator(new ArrayIterator($classes), ‘/ ^Zend/’); fore­ach ($zend­classes as $classname) {

// Search each class for func­tion names that match except for the under­score pre­fix
// Note this includes dupli­cates and magic meth­ods, so you have to do some sort­ing
// on the out­put. Hint: ‘sort –u‘.
$class = new ReflectionClass($classname);
$meth­ods = $class->getMethods();
fore­ach ($meth­ods as $method) {
if (preg_match(“/^__*(.*)/”, $method->name, $matches)) {
$under­score = $method;
if ($class->hasMethod($matches[1])) {
$nonun­der­score = $class->getMethod($matches[1]);
echo $underscore->getDeclaringClass()->name
. “::” . $underscore->name . “()” . ” => “;
if ($underscore->getDeclaringClass() != $nonunderscore->getDeclaringClass()) {
echo $nonunderscore->getDeclaringClass()->name;
}
echo “::” . $nonunderscore->name . “()” . “\n”;
}
}
}

}


Tags: ,

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes