2016-01-13 127 views
0

在发布Mars之前,可以在Eclipse中安装PHPMD支持,尽管some caveats and difficulties也支持。在Eclipse中集成PHPMD(PHP Mess Detector)Mars

现在来自PTI的支持似乎已被彻底删除,即使development of PHPMD hasn't stopped和PHPMD确实提供了其他工具不具备的某些功能:例如,detect unused variables

对于这最后一个功能我找到了一个not too recent CodeSniffer plugin这个技巧。也有some sniffs应该做这项工作,但他们似乎并没有为我工作,或至少不是在所有情况下工作:我有一个需要重构的项目,我有11个来自CodeSniffer的警告和2524个来自PHPMD的警告。

我想我已经回到了shoehorning PHPMD的简单和不雅的方式,但这样做之前,我想知道是否有人有这个特定的同样的问题/需要,以及是否他/她设法以某种方式解决这个问题。

回答

1

好的,所以在这里。

我确实有一个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的内容。