I'm trying to use a tokenizer to scan a file to find all the specific classes, everything that they extend, any instantiated instances, and anytime they were statically called.
<?php $tokens = token_get_all(file_get_contents($file)); $used_classes = array(); $defined_classes = array(); $variable_classes = array(); foreach($tokens as $i => $token) { if(is_array($token)) { if(isset($tokens[$i - 2][0], $tokens[$i - 1][0])) { // new [class] if ($tokens[$i - 2][0] == T_NEW AND $tokens[$i - 1][0] == T_WHITESPACE) { if($tokens[$i][0] == T_STRING) { $used_classes[$token[1]] = TRUE; // new $variable() } elseif($tokens[$i][0] == T_VARIABLE) { // @todo, this is really broken. However, do best to look for the assignment if(preg_match('~\$var\s*=\s*([\'"])((?:(?!\1).)*)\1~', $text, $match)) { if(empty($extension_classes[$match[2]])) { $used_classes[$match[2]] = TRUE; } } elseif($token[1] !== '$this') { $variable_classes[$token[1]] = TRUE; } } } // class [class] if ($tokens[$i - 2][0] == T_CLASS AND $tokens[$i - 1][0] == T_WHITESPACE) { if($tokens[$i][0] == T_STRING) { $defined_classes[$token[1]] = TRUE; } } // @todo: find more classes \/ // class [classname] extends [class] ??? // [class]::method()??? } } }
How can I extend this code to find additional instances of PHP classes as above?
, PHP- - , . , , , , , , .., , .
, PHP-to-Javascript , :
, ClassScope , FunctionScope, , .
,
, , ClassScope β $parentClasses
, .
, 95% , , .
Inclued, , , , , / .
, , .
, - , , , . / ; - . , - . , , , .
, , PHP . . ( , , ).
, , API Reflection (ReflectionClass:: _ construct(), ..) .
, get_declared_classes().
(: , YMMV.)