/** * Run the task. * * @param array &$options Additional options. * * @return integer Number of errors. */ public function run(&$options) { $lib = realpath($this->_config->getPath() . '/lib'); $renderer = new PHP_PMD_Renderer_TextRenderer(); $renderer->setWriter(new PHP_PMD_Writer_Stream(STDOUT)); $ruleSetFactory = new PHP_PMD_RuleSetFactory(); $ruleSetFactory->setMinimumPriority(PHP_PMD_AbstractRule::LOWEST_PRIORITY); $phpmd = new PHP_PMD(); $phpmd->processFiles($lib, Components_Constants::getDataDirectory() . '/qc_standards/phpmd.xml', array($renderer), $ruleSetFactory); return $phpmd->hasViolations(); }
/** * This method will process all files that can be found in the given input * path. It will apply rules defined in the comma-separated <b>$ruleSets</b> * argument. The result will be passed to all given renderer instances. * * @param string $inputPath File or directory * @param string $ruleSets Rule-sets to apply * @param array(PHP_PMD_AbstractRenderer) $renderers Report renderers * @param PHP_PMD_RuleSetFactory $ruleSetFactory The factory to use * * @return void */ public function processFiles($inputPath, $ruleSets, array $renderers, PHP_PMD_RuleSetFactory $ruleSetFactory) { $this->_input = $inputPath; $report = new PHP_PMD_Report(); $factory = new PHP_PMD_ParserFactory(); $parser = $factory->create($this); foreach ($ruleSetFactory->createRuleSets($ruleSets) as $ruleSet) { $parser->addRuleSet($ruleSet); } $report->start(); $parser->parse($report); $report->end(); foreach ($renderers as $renderer) { $renderer->start(); } foreach ($renderers as $renderer) { $renderer->renderReport($report); } foreach ($renderers as $renderer) { $renderer->end(); } $this->_violations = !$report->isEmpty(); }
/** * Executes PHPMD against PhingFile or a FileSet * * @throws BuildException - if the phpmd classes can't be loaded. */ public function main() { /** * Find PHPMD */ if (false === stream_resolve_include_path("PHP/PMD.php")) { @(include_once 'PHPMD/PHPMD.php'); $class_name = '\\PHPMD\\PHPMD'; $new = true; } else { @(include_once 'PHP/PMD.php'); $class_name = "PHP_PMD"; $new = false; } if (!class_exists($class_name)) { throw new BuildException('PHPMDTask depends on PHPMD being installed and on include_path.', $this->getLocation()); } if ($new) { require_once 'PHPMD/AbstractRule.php'; //weird syntax to allow 5.2 parser compatability $minPriority = constant('\\PHPMD\\AbstractRule::LOWEST_PRIORITY'); } else { require_once 'PHP/PMD/AbstractRule.php'; $minPriority = PHP_PMD_AbstractRule::LOWEST_PRIORITY; } if (!$this->minimumPriority) { $this->minimumPriority = $minPriority; } if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException('Missing either a nested fileset or attribute "file" set'); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PHPMDFormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } $reportRenderers = array(); foreach ($this->formatters as $fe) { if ($fe->getType() == '') { throw new BuildException('Formatter missing required "type" attribute.'); } if ($fe->getUsefile() && $fe->getOutfile() === null) { throw new BuildException('Formatter requires "outfile" attribute when "useFile" is true.'); } $reportRenderers[] = $fe->getRenderer(); } // Create a rule set factory if ($new) { @(require_once "PHPMD/RuleSetFactory.php"); $ruleSetClass = '\\PHPMD\\RuleSetFactory'; $ruleSetFactory = new $ruleSetClass(); //php 5.2 parser compatability } else { $ruleSetFactory = new PHP_PMD_RuleSetFactory(); } $ruleSetFactory->setMinimumPriority($this->minimumPriority); $phpmd = new $class_name(); $phpmd->setFileExtensions($this->allowedFileExtensions); $phpmd->setIgnorePattern($this->ignorePatterns); $filesToParse = array(); if ($this->file instanceof PhingFile) { $filesToParse[] = $this->file->getPath(); } else { // append any files in filesets foreach ($this->filesets as $fs) { foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) { $f = new PhingFile($fs->getDir($this->project), $filename); $filesToParse[] = $f->getAbsolutePath(); } } } if (count($filesToParse) > 0) { $inputPath = implode(',', $filesToParse); $this->log('Processing files...'); $phpmd->processFiles($inputPath, $this->rulesets, $reportRenderers, $ruleSetFactory); $this->log('Finished processing files'); } else { $this->log('No files to process'); } }
/** * Executes PHPMD against PhingFile or a FileSet * * @throws BuildException - if the phpmd classes can't be loaded. * @return void */ public function main() { /** * Find PHPMD */ @(include_once 'PHP/PMD.php'); if (!class_exists('PHP_PMD')) { throw new BuildException('PHPMDTask depends on PHPMD being installed and on include_path.', $this->getLocation()); } require_once 'PHP/PMD/AbstractRule.php'; if (!$this->minimumPriority) { $this->minimumPriority = PHP_PMD_AbstractRule::LOWEST_PRIORITY; } if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PHPMDFormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } $reportRenderers = array(); foreach ($this->formatters as $fe) { if ($fe->getType() == '') { throw new BuildException("Formatter missing required 'type' attribute."); } if ($fe->getUsefile() && $fe->getOutfile() === null) { throw new BuildException("Formatter requires 'outfile' attribute when 'useFile' is true."); } $reportRenderers[] = $fe->getRenderer(); } // Create a rule set factory $ruleSetFactory = new PHP_PMD_RuleSetFactory(); $ruleSetFactory->setMinimumPriority($this->minimumPriority); $phpmd = new PHP_PMD(); $phpmd->setFileExtensions($this->allowedFileExtensions); $phpmd->setIgnorePattern($this->ignorePatterns); $filesToParse = array(); if ($this->file instanceof PhingFile) { $filesToParse[] = $this->file->getPath(); } else { // append any files in filesets foreach ($this->filesets as $fs) { $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles(); foreach ($files as $filename) { $f = new PhingFile($fs->getDir($this->project), $filename); $filesToParse[] = $f->getAbsolutePath(); } } } if (count($filesToParse) > 0) { $inputPath = implode(',', $filesToParse); $this->log('Processing files...'); $phpmd->processFiles($inputPath, $this->rulesets, $reportRenderers, $ruleSetFactory); $this->log('Finished processing files'); } else { $this->log('No files to process'); } }
/** * Invokes the <b>createRuleSets()</b> of the {@link PHP_PMD_RuleSetFactory} * class. * * @param string $file At least one rule configuration file name. You can * also pass multiple parameters with ruleset configuration files. * * @return array(PHP_PMD_RuleSet) */ private function createRuleSetsFromFiles($file) { $args = func_get_args(); $factory = new PHP_PMD_RuleSetFactory(); return $factory->createRuleSets(join(',', $args)); }
/** * Executes PHPMD against PhingFile or a FileSet * * @throws BuildException - if the phpmd classes can't be loaded. */ public function main() { $className = $this->loadDependencies(); if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException('Missing either a nested fileset or attribute "file" set'); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PHPMDFormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } $reportRenderers = array(); foreach ($this->formatters as $fe) { if ($fe->getType() == '') { throw new BuildException('Formatter missing required "type" attribute.'); } if ($fe->getUsefile() && $fe->getOutfile() === null) { throw new BuildException('Formatter requires "outfile" attribute when "useFile" is true.'); } $reportRenderers[] = $fe->getRenderer(); } // Create a rule set factory if ($this->newVersion) { $ruleSetClass = '\\PHPMD\\RuleSetFactory'; $ruleSetFactory = new $ruleSetClass(); //php 5.2 parser compatability } else { if (!class_exists("PHP_PMD_RuleSetFactory")) { @(include 'PHP/PMD/RuleSetFactory.php'); } $ruleSetFactory = new PHP_PMD_RuleSetFactory(); } $ruleSetFactory->setMinimumPriority($this->minimumPriority); $phpmd = new $className(); $phpmd->setFileExtensions($this->allowedFileExtensions); $phpmd->setIgnorePattern($this->ignorePatterns); $filesToParse = array(); if ($this->file instanceof PhingFile) { $filesToParse[] = $this->file->getPath(); } else { // append any files in filesets foreach ($this->filesets as $fs) { foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) { $f = new PhingFile($fs->getDir($this->project), $filename); $filesToParse[] = $f->getAbsolutePath(); } } } if (count($filesToParse) > 0) { $inputPath = implode(',', $filesToParse); $this->log('Processing files...'); $phpmd->processFiles($inputPath, $this->rulesets, $reportRenderers, $ruleSetFactory); $this->log('Finished processing files'); } else { $this->log('No files to process'); } }
/** * This method creates a PHP_PMD instance and configures this object based * on the user's input, then it starts the source analysis. * * The return value of this method can be used as an exit code. A value * equal to <b>EXIT_SUCCESS</b> means that no violations or errors were * found in the analyzed code. Otherwise this method will return a value * equal to <b>EXIT_VIOLATION</b>. * * @param PHP_PMD_TextUI_CommandLineOptions $opts The prepared command line * arguments. * * @return integer */ public function run(PHP_PMD_TextUI_CommandLineOptions $opts) { if ($opts->hasVersion()) { fwrite(STDOUT, 'PHPMD @package_version@ by Manuel Pichler' . PHP_EOL); return self::EXIT_SUCCESS; } // Create a report stream if ($opts->getReportFile() === null) { $stream = STDOUT; } else { $stream = fopen($opts->getReportFile(), 'wb'); } // Create renderer and configure output $renderer = $opts->createRenderer(); $renderer->setWriter(new PHP_PMD_Writer_Stream($stream)); // Create a rule set factory $ruleSetFactory = new PHP_PMD_RuleSetFactory(); $ruleSetFactory->setMinimumPriority($opts->getMinimumPriority()); if ($opts->hasStrict()) { $ruleSetFactory->setStrict(); } $phpmd = new PHP_PMD(); $extensions = $opts->getExtensions(); if ($extensions !== null) { $phpmd->setFileExtensions(explode(',', $extensions)); } $ignore = $opts->getIgnore(); if ($ignore !== null) { $phpmd->setIgnorePattern(explode(',', $ignore)); } $phpmd->processFiles($opts->getInputPath(), $opts->getRuleSets(), array($renderer), $ruleSetFactory); if ($phpmd->hasViolations()) { return self::EXIT_VIOLATION; } return self::EXIT_SUCCESS; }
/** * This method parses a single rule that was included from a different * rule-set. * * @param PHP_PMD_RuleSet $ruleSet The parent rule-set instance. * @param SimpleXMLElement $ruleNode The unparsed rule xml node. * * @return void */ private function parseRuleReferenceNode(PHP_PMD_RuleSet $ruleSet, SimpleXMLElement $ruleNode) { $ref = (string) $ruleNode['ref']; $fileName = substr($ref, 0, strpos($ref, '.xml/') + 4); $fileName = $this->createRuleSetFileName($fileName); $ruleName = substr($ref, strpos($ref, '.xml/') + 5); $ruleSetFactory = new PHP_PMD_RuleSetFactory(); $ruleSetRef = $ruleSetFactory->createSingleRuleSet($fileName); $rule = $ruleSetRef->getRuleByName($ruleName); if (trim($ruleNode['name']) !== '') { $rule->setName((string) $ruleNode['name']); } if (trim($ruleNode['message']) !== '') { $rule->setMessage((string) $ruleNode['message']); } if (trim($ruleNode['externalInfoUrl']) !== '') { $rule->setExternalInfoUrl((string) $ruleNode['externalInfoUrl']); } foreach ($ruleNode->children() as $node) { if ($node->getName() === 'description') { $rule->setDescription((string) $node); } else { if ($node->getName() === 'example') { $rule->addExample((string) $node); } else { if ($node->getName() === 'priority') { $rule->setPriority((int) $node); } else { if ($node->getName() === 'properties') { $this->parsePropertiesNode($rule, $node); } } } } } if ($rule->getPriority() <= $this->minimumPriority) { $ruleSet->addRule($rule); } }
/** * Executes PHPMD against PhingFile or a FileSet * * @throws BuildException - if the phpmd classes can't be loaded. */ public function main() { $className = $this->loadDependencies(); if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException('Missing either a nested fileset or attribute "file" set'); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PHPMDFormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } $reportRenderers = array(); foreach ($this->formatters as $fe) { if ($fe->getType() == '') { throw new BuildException('Formatter missing required "type" attribute.'); } if ($fe->getUsefile() && $fe->getOutfile() === null) { throw new BuildException('Formatter requires "outfile" attribute when "useFile" is true.'); } $reportRenderers[] = $fe->getRenderer(); } if ($this->newVersion && $this->cache) { $reportRenderers[] = new PHPMDRendererRemoveFromCache($this->cache); } else { $this->cache = null; // cache not compatible to old version } // Create a rule set factory if ($this->newVersion) { $ruleSetClass = '\\PHPMD\\RuleSetFactory'; $ruleSetFactory = new $ruleSetClass(); //php 5.2 parser compatibility } else { if (!class_exists("PHP_PMD_RuleSetFactory")) { @(include 'PHP/PMD/RuleSetFactory.php'); } $ruleSetFactory = new PHP_PMD_RuleSetFactory(); } $ruleSetFactory->setMinimumPriority($this->minimumPriority); /** * @var PHPMD\PHPMD $phpmd */ $phpmd = new $className(); $phpmd->setFileExtensions($this->allowedFileExtensions); $phpmd->setIgnorePattern($this->ignorePatterns); $filesToParse = $this->getFilesToParse(); if (count($filesToParse) > 0) { $inputPath = implode(',', $filesToParse); $this->log('Processing files...'); $phpmd->processFiles($inputPath, $this->rulesets, $reportRenderers, $ruleSetFactory); if ($this->cache) { $this->cache->commit(); } $this->log('Finished processing files'); } else { $this->log('No files to process'); } }
/** * The main method that can be used by a calling shell script, the return * value can be used as exit code. * * @param array $args The raw command line arguments array. * * @return integer */ public static function main(array $args) { try { $ruleSetFactory = new PHP_PMD_RuleSetFactory(); $options = new PHP_PMD_TextUI_CommandLineOptions($args, $ruleSetFactory->listAvailableRuleSets()); $command = new PHP_PMD_TextUI_Command(); $exitCode = $command->run($options, $ruleSetFactory); } catch (Exception $e) { fwrite(STDERR, $e->getMessage()); fwrite(STDERR, PHP_EOL); $exitCode = self::EXIT_EXCEPTION; } return $exitCode; }
/** * This method parses a single rule that was included from a different * rule-set. * * @param PHP_PMD_RuleSet $ruleSet The parent rule-set instance. * @param SimpleXMLElement $ruleNode The unparsed rule xml node. * * @return void */ private function parseRuleReferenceNode(PHP_PMD_RuleSet $ruleSet, SimpleXMLElement $ruleNode) { $ref = (string) $ruleNode['ref']; $fileName = substr($ref, 0, strpos($ref, '.xml/') + 4); $fileName = $this->createRuleSetFileName($fileName); $ruleName = substr($ref, strpos($ref, '.xml/') + 5); $ruleSetFactory = new PHP_PMD_RuleSetFactory(); $ruleSetRef = $ruleSetFactory->createSingleRuleSet($fileName); $rule = $ruleSetRef->getRuleByName($ruleName); if (!$rule instanceof PHP_PMD_Rule) { $availableRules = array_map(function (PHP_PMD_Rule $rule) { return $rule->getName(); }, iterator_to_array($ruleSetRef->getRules())); throw new \RuntimeException(sprintf('Could not find any rule named "%s", available rules: %s', $ruleName, implode(', ', $availableRules))); } if (trim($ruleNode['name']) !== '') { $rule->setName((string) $ruleNode['name']); } if (trim($ruleNode['message']) !== '') { $rule->setMessage((string) $ruleNode['message']); } if (trim($ruleNode['externalInfoUrl']) !== '') { $rule->setExternalInfoUrl((string) $ruleNode['externalInfoUrl']); } foreach ($ruleNode->children() as $node) { if ($node->getName() === 'description') { $rule->setDescription((string) $node); } else { if ($node->getName() === 'example') { $rule->addExample((string) $node); } else { if ($node->getName() === 'priority') { $rule->setPriority((int) $node); } else { if ($node->getName() === 'properties') { $this->parsePropertiesNode($rule, $node); } } } } } if ($rule->getPriority() <= $this->minimumPriority) { $ruleSet->addRule($rule); } }