PHP 脚本输出“T_STRING” (就是这样)

发布于 2024-10-03 02:36:16 字数 3624 浏览 7 评论 0原文

我在这里使用代码 - 如何在 PHP 项目中找到未使用的函数(复制如下),原样 - 只是路径修改为我的位置,其行为如下:

root@server [/var/www]# php see_unused_code.php
T_STRING

使用的代码是:

<?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]);
                }
        }
    }
?>

PHP 版本是:

PHP 5.3.3 (cli) (built: Aug  7 2010 14:49:50)

I'm using the code here - How can I find unused functions in a PHP project (reproduced below) exactly as it is - just the path modified to my location and it behaves as follows:

root@server [/var/www]# php see_unused_code.php
T_STRING

The code used is:

<?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]);
                }
        }
    }
?>

PHP version is:

PHP 5.3.3 (cli) (built: Aug  7 2010 14:49:50)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2024-10-10 02:36:16

你应该查看源代码,阅读它,理解它。不仅仅是复制粘贴运行。看一下define_file()函数定义:if ($token[0] != T_STRING) die("T_STRING");

你可以通过显示文件来调试它-该条件发生的路径。

if ($token[0] != T_STRING) die("T_STRING: " . $path . " : " . $token[0]);

然后你就会知道真正的问题是什么。

You should look at the source code, read it, understand it. Not just coopy-paste-run. Look at the define_file() function definition: if ($token[0] != T_STRING) die("T_STRING");

You can debug it by displaying the file-path where that condition occurs.

if ($token[0] != T_STRING) die("T_STRING: " . $path . " : " . $token[0]);

Then you'll know what the real problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文