如何在 PHP 项目中找到任何未使用的函数?
PHP 中是否有内置功能或 API 可以让我分析我的代码库 - 例如 Reflection , token_get_all()
?
这些 API 的功能是否足够丰富,让我不必依赖第三方工具来执行此类分析?
最佳答案
你可以试试 Sebastian Bergmann 的死码检测器:
phpdcd
is a Dead Code Detector (DCD) for PHP code. It scans a PHP project for all declared functions and methods and reports those as being "dead code" that are not called at least once.
来源:https://github.com/sebastianbergmann/phpdcd
请注意,它是一个静态代码分析器,因此它可能会为仅动态调用的方法提供误报,例如它无法检测到 $foo = 'fn'; $foo();
您可以通过 PEAR 安装它:
pear install phpunit/phpdcd-beta
之后,您可以使用以下选项:
Usage: phpdcd [switches] <directory|file> ...
--recursive Report code as dead if it is only called by dead code.
--exclude <dir> Exclude <dir> from code analysis.
--suffixes <suffix> A comma-separated list of file suffixes to check.
--help Prints this usage information.
--version Prints the version and exits.
--verbose Print progress bar.
更多工具:
注意:根据存储库通知,此项目不再维护,其存储库仅用于存档目的。所以你的里程可能会有所不同。
关于php - 如何在 PHP 项目中找到未使用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532/