PHPMD(PHP Mess Detector)在Eclipse Mars中的集成


PHPMD (PHP Mess Detector) integration in Eclipse Mars

在Mars发布之前,可以在Eclipse中安装PHPMD支持,尽管有一些注意事项和困难。

现在,PTI的支持似乎已经完全取消,即使PHPMD的开发没有停止,而且PHPMD确实提供了一些其他工具没有的功能:例如,检测未使用的变量。

对于最后一个功能,我发现了一个不太新的CodeSniffer插件,它可以做到这一点。也有一些嗅探器应该起作用,但它们似乎对我不起作用,或者至少在所有情况下都不起作用:我有一个项目需要重构,CodeSniffer发出了11条警告,PHPMD发出了2524条警告

我想我有一种简单而不雅的方法来重新说服PHPMD,但在做之前,我想知道是否有人有同样的问题/需求,以及他是否设法解决了这个问题。

好了,开始吧。

我确实有一个PHPMD二进制文件,它可以在命令行中工作。我计划做的是将其输出注入CodeSniffer插件的输出中,用PHPMD消息"丰富"后者。

为此,我损坏了plugins/org.ppsrc.eclipse.pti.tools.codesniffer_.../php/tools目录中的phpcs.php

(由于CodeSniffer的另一个问题是它经常会重新扫描应该知道的文件,我决定给CodeSniffr一个内存。

首先,我提取调用的最后一个参数,即正在分析的文件(用+++标记的行是我的添加/更改):

// Optionally use PHP_Timer to print time/memory stats for the run.
// Note that the reports are the ones who actually print the data
// as they decide if it is ok to print this data to screen.
@include_once 'PHP/Timer.php';
if (class_exists('PHP_Timer', false) === true) {
    PHP_Timer::start();
}
if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
    include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
} else {
    include_once 'PHP/CodeSniffer/CLI.php';
}
+++ $lastArgument   = array_pop($_SERVER['argv']);

然后我添加了一些CS似乎没有通过的标志,以及我需要的标志,比如忽略一些目录:

+++ $_SERVER['argv'][]  = '--ignore=tests,vendor,cache';
+++ $_SERVER['argv'][]  = $lastArgument;

然后CS调用继续进行,但现在我将其结果保存到缓冲区,而不是直接发送到Eclipse。

$phpcs = new PHP_CodeSniffer_CLI();
$phpcs->checkRequirements();
+++ ob_start();
$numErrors = $phpcs->process();
+++ $dom    = new DOMDocument();
+++ $dom->loadXML(ob_get_clean());
+++ $cs     = $dom->getElementsByTagName('phpcs')->item(0);
+++ $xpath  = new DOMXPath($dom);

现在我已经准备好了PHPCS输出作为XML。

剩下的就是使用自己的语法调用PHPMD。

// Add PHPMD.
$mdCmd  = "C:/PHP/composer/vendor/phpmd/phpmd/src/bin/phpmd '"{$lastArgument}'" xml '"C:/Program Files/eclipse/plugins/org.phpsrc.eclipse.pti.library.pear_1.2.2.R20120127000000/php/library/PEAR/data/PHP_PMD/resources/rulesets/codesize.xml,C:/Program Files/eclipse/plugins/org.phpsrc.eclipse.pti.library.pear_1.2.2.R20120127000000/php/library/PEAR/data/PHP_PMD/resources/rulesets/naming.xml,C:/Program Files/eclipse/plugins/org.phpsrc.eclipse.pti.library.pear_1.2.2.R20120127000000/php/library/PEAR/data/PHP_PMD/resources/rulesets/unusedcode.xml'"";

并将其加载到另一个XML:中

    fprintf(STDERR, $mdCmd . "'n");
    $dompmd = new DOMDocument();
    $dompmd->loadXML($mdxml = shell_exec($mdCmd));

现在,我从PMD对象中获取所有错误,并将其添加到CS对象中:

    $files  = $dompmd->getElementsByTagName('file');
    foreach ($files as $file) {
        $name   = $file->getAttribute('name');
        $list   = $xpath->query("//file[@name='"{$name}'"]");
        if (null === $list) {
            continue;
        }
        $csFile = $list->item(0);
        if (null === $csFile) {
            // No errors from CS.
            $csFile = $dom->createElement('file');
            $csFile->setAttribute('name', $name);
            $csFile->setAttribute('errors', 0);
            $csFile->setAttribute('warnings', 0);
            $csFile->setAttribute('fixable', 0);
            $cs->appendChild($csFile);
        }
        $errs   = 0;
        foreach ($file->childNodes as $violation) {
            if ($violation instanceof 'DOMText) {
                continue;
            }
            $error  = $dom->createElement('warning', trim($violation->nodeValue));

            $error->setAttribute('line', $violation->getAttribute('beginline'));
            $error->setAttribute('column', 1);
            $error->setAttribute('source', 'PHPMD.' . $violation->getAttribute('ruleset'));
            $error->setAttribute('severity', $violation->getAttribute('priority'));
            $error->setAttribute('fixable', 0);
            $csFile->appendChild($error);
            $errs++;
        }
        $csFile->getAttributeNode('errors')->value += $errs;
    }

最后,将数据发送回Eclipse:

print $dom->saveXML();
exit($numErrors ? 1: 0);

缓存CodeSniffer(以及PHPMD)

由于CodeSniffer的另一个问题是它经常会重新扫描应该知道的文件,所以我决定给CodeSniffr一个内存。这很容易:我可以用保存的XML和由原始文件名及其内容的MD5构建的名称存储一个临时文件:

/tmp/68ce1959ef67bcc94e05084e2e20462a.76e55e72f32156a20a183de82fe0b3b6.xml

因此,当PHPCS被要求分析/path/to/file/so-and-so.php时,它将:

  • 创建文件名的MD5
  • 创建其内容的MD5
  • 如果/tmp/md5file.md5content.xml不存在:
    • 删除任何/tmp/md5file.*.xml:它们已经过时了
    • 如上所述运行代码
    • 将结果保存到/tmp/md5file.md5content.xml
  • 输出/tmp/md5file.md5content.xml的内容