1

ここでコードを使用しています-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)
4

1 に答える 1

6

ソースコードを見て、読んで、理解する必要があります。コピペランだけじゃない。define_file()関数定義を見てください。if ($token[0] != T_STRING) die("T_STRING");

その状態が発生するファイルパスを表示することでデバッグできます。

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

そうすれば、本当の問題が何であるかがわかります。

于 2010-11-17T10:53:51.890 に答える