好的,所以在这里。
我确实有一个PHPMD二进制文件,它可以从命令行工作。我打算做的是将其输出注入到CodeSniffer插件的输出中,以便用PHPMD消息“丰富”后者。
为此,我捣毁了phpcs.php
,它在我的plugins/org.ppsrc.eclipse.pti.tools.codesniffer_.../php/tools
目录中。
(因为另一个问题,我有CodeSniffer是,它往往会重新扫描文件它应该了解,我决定给CodeSniffer内存。
第一件事,我提取的最后一个参数调用,这是文件被分析(由+++标线是我添加/修改):
// 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是它将经常重新扫描文件它应该了解,我决定给CodeSniffer一个记忆。这是很容易:我可以存储与保存的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的内容。