首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在PHP项目中查找未使用的函数

如何在PHP项目中查找未使用的函数
EN

Stack Overflow用户
提问于 2008-08-14 19:08:22
回答 4查看 30.2K关注 0票数 67

如何在PHP项目中找到未使用的函数?

有没有内置到PHP中的特性或API可以让我分析我的代码库--例如Reflectiontoken_get_all()

这些API的功能是否足够丰富,让我不必依赖第三方工具来执行这种类型的分析?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2008-08-18 13:47:42

感谢Greg和Dave的反馈。并不完全是我想要的,但我决定花点时间来研究它,并想出了这个快速而肮脏的解决方案:

代码语言:javascript
复制
<?php
    $functions = array();
    $path = "/path/to/my/php/project";
    define_dir($path, $functions);
    reference_dir($path, $functions);
    echo
        "<table>" .
            "<tr>" .
                "<th>Name</th>" .
                "<th>Defined</th>" .
                "<th>Referenced</th>" .
            "</tr>";
    foreach ($functions as $name => $value) {
        echo
            "<tr>" . 
                "<td>" . htmlentities($name) . "</td>" .
                "<td>" . (isset($value[0]) ? count($value[0]) : "-") . "</td>" .
                "<td>" . (isset($value[1]) ? count($value[1]) : "-") . "</td>" .
            "</tr>";
    }
    echo "</table>";
    function define_dir($path, &$functions) {
        if ($dir = opendir($path)) {
            while (($file = readdir($dir)) !== false) {
                if (substr($file, 0, 1) == ".") continue;
                if (is_dir($path . "/" . $file)) {
                    define_dir($path . "/" . $file, $functions);
                } else {
                    if (substr($file, - 4, 4) != ".php") continue;
                    define_file($path . "/" . $file, $functions);
                }
            }
        }       
    }
    function define_file($path, &$functions) {
        $tokens = token_get_all(file_get_contents($path));
        for ($i = 0; $i < count($tokens); $i++) {
            $token = $tokens[$i];
            if (is_array($token)) {
                if ($token[0] != T_FUNCTION) continue;
                $i++;
                $token = $tokens[$i];
                if ($token[0] != T_WHITESPACE) die("T_WHITESPACE");
                $i++;
                $token = $tokens[$i];
                if ($token[0] != T_STRING) die("T_STRING");
                $functions[$token[1]][0][] = array($path, $token[2]);
            }
        }
    }
    function reference_dir($path, &$functions) {
        if ($dir = opendir($path)) {
            while (($file = readdir($dir)) !== false) {
                if (substr($file, 0, 1) == ".") continue;
                if (is_dir($path . "/" . $file)) {
                    reference_dir($path . "/" . $file, $functions);
                } else {
                    if (substr($file, - 4, 4) != ".php") continue;
                    reference_file($path . "/" . $file, $functions);
                }
            }
        }       
    }
    function reference_file($path, &$functions) {
        $tokens = token_get_all(file_get_contents($path));
        for ($i = 0; $i < count($tokens); $i++) {
            $token = $tokens[$i];
            if (is_array($token)) {
                if ($token[0] != T_STRING) continue;
                if ($tokens[$i + 1] != "(") continue;
                $functions[$token[1]][1][] = array($path, $token[2]);
            }
        }
    }
?>

我可能会在上面花费更多的时间,这样我就可以快速找到函数定义和引用的文件和行号;这些信息正在收集中,只是没有显示出来。

票数 25
EN

Stack Overflow用户

发布于 2010-06-01 08:38:22

由于PHP函数/方法可以动态调用,因此没有编程方法可以确定某个函数是否永远不会被调用。

唯一确定的方法是通过手动分析。

票数 3
EN

Stack Overflow用户

发布于 2010-06-01 16:59:49

phpxref将识别从何处调用函数,这将有助于分析-但仍涉及一定数量的手动工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档