Finding PHP dependencies

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 02:44:13

问题


Are there any tools that can list the names of classes used by a PHP file?

For example, if I ran it on this file:

<?
class Test {
  public function __construct(Obj1 $x) {
    $y = new Obj2();
    $str = "Obj3";
    $z = new $str();
  }
}
?>

it would report "Obj1" and "Obj2". If it were really smart it might report "Obj3" as well, but that's not essential.

I'm trying to package up some code, and I want some help making sure that I didn't miss any dependencies.

There's something called PHP_Depend, which can graph the number of dependencies, but can't report what they are.

UPDATE: I didn't find a real solution, but I figured out something close enough for my purposes. You can tokenize a file, and search for all T_STRING tokens. This will give you all class names mentioned in the file. It will also give you other things, like function names and constants. But if your class names are easy to distinguish (e.g. they have initial caps), then this shouldn't be a problem.

$contents = file_get_contents($path);
$tokens = token_get_all($contents);
foreach ($tokens as $token) {
  if (is_array($token) && $token[0] === T_STRING) {
    echo $token[1]."\n";
  }
}

回答1:


There's PHPXref, which is a PHP cross referencing document generator.



来源:https://stackoverflow.com/questions/2709843/finding-php-dependencies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!