开发者

Get functions and classes defined in file?

开发者 https://www.devze.com 2023-04-10 03:28 出处:网络
I\'m looping over all the files in a directory. Now I want to get all the functions and classes defined in each of them. From there, I can examine them further using the ReflectionClass. I can\'t figu

I'm looping over all the files in a directory. Now I want to get all the functions and classes defined in each of them. From there, I can examine them further using the ReflectionClass. I can't figure out how开发者_高级运维 to get all the functions and classes defined in a file though.

ReflectionExtension looks the closest to what I want, except my files aren't part of an extension. Is there some class or function I'm overlooking?


Great question. get_declared_classes and get_defined_functions could be a good starting point. You would have to take note of what classes / functions are already defined when trying to determine what's in a given file.

Also, not sure what your end goal is here, but tools such as PHP Depend or PHP Mess Detector may do something similar to what you want. I'd recommend checking them out as well.


This is the best I could come up with (courtesy):

function trimds($s) {
    return rtrim($s,DIRECTORY_SEPARATOR);
}

function joinpaths() {
    return implode(DIRECTORY_SEPARATOR, array_map('trimds', func_get_args()));
}

$project_dir = '/path/to/project/';

$ds = array($project_dir);

$classes = array();

while(!empty($ds)) {
    $dir = array_pop($ds);
    if(($dh=opendir($dir))!==false) {
        while(($file=readdir($dh))!==false) {
            if($file[0]==='.') continue;
            $path = joinpaths($dir,$file);
            if(is_dir($path)) {
                $ds[] = $path;
            } else {
                $contents = file_get_contents($path);
                $tokens = token_get_all($contents);
                for($i=0; $i<count($tokens); ++$i) {
                    if(is_array($tokens[$i]) && $tokens[$i][0] === T_CLASS) {
                        $i += 2;
                        $classes[] = $tokens[$i][1];
                    }
                }
            }
        }
    } else {
        echo "ERROR: Could not open directory '$dir'\n";
    }
}

print_r($classes);

Wish I didn't have to parse out the files and loop over all the tokens like this.


Forgot the former solutions prevents me from using reflection as I wanted. New solution:

$project_dir = '/path/to/project/';

$ds = array($project_dir);

while(!empty($ds)) {
    $dir = array_pop($ds);
    if(($dh=opendir($dir))!==false) {
        while(($file=readdir($dh))!==false) {
            if($file[0]==='.') continue;
            $path = joinpaths($dir,$file);
            if(is_dir($path)) {
                $ds[] = $path;
            } else {
                try{
                    include_once $path;
                }catch(Exception $e) {
                    echo 'EXCEPTION: '.$e->getMessage().PHP_EOL;
                }
            }
        }
    } else {
        echo "ERROR: Could not open directory '$dir'\n";
    }
}

foreach(get_declared_classes() as $c) {
    $class = new ReflectionClass($c);
    $methods = $class->getMethods();
    foreach($methods as $m) {
        $dc = $m->getDocComment();
        if($dc !== false) {
            echo $class->getName().'::'.$m->getName().PHP_EOL;
            echo $dc.PHP_EOL;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号