Ejemplo n.º 1
0
 /**
  * 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');
     }
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }