123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- require_once 'phing/Task.php';
- class PHPMDTask extends Task
- {
-
- protected $file = null;
-
- protected $filesets = array();
-
- protected $rulesets = 'codesize,unusedcode';
-
- protected $minimumPriority = 5;
-
- protected $allowedFileExtensions = array('php');
-
- protected $ignorePatterns = array('.git', '.svn', 'CVS', '.bzr', '.hg');
-
- protected $format = 'text';
-
- protected $formatters = array();
-
- public function init()
- {
-
- @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 'phing/tasks/ext/phpmd/PHPMDFormatterElement.php';
- require_once 'PHP/PMD/AbstractRule.php';
- $this->minimumPriority = PHP_PMD_AbstractRule::LOWEST_PRIORITY;
- }
-
- public function setFile(PhingFile $file)
- {
- $this->file = $file;
- }
-
- public function createFileSet()
- {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
- public function setMinimumPriority($minimumPriority)
- {
- $this->minimumPriority = $minimumPriority;
- }
-
- public function setRulesets($ruleSetFileNames)
- {
- $this->rulesets = $ruleSetFileNames;
- }
-
- public function setAllowedFileExtensions($fileExtensions)
- {
- $this->allowedFileExtensions = array();
- $token = ' ,;';
- $ext = strtok($fileExtensions, $token);
- while ($ext !== false) {
- $this->allowedFileExtensions[] = $ext;
- $ext = strtok($token);
- }
- }
-
- public function setIgnorePatterns($ignorePatterns)
- {
- $this->ignorePatterns = array();
- $token = ' ,;';
- $pattern = strtok($ignorePatterns, $token);
- while ($pattern !== false) {
- $this->ignorePatterns[] = $pattern;
- $pattern = strtok($token);
- }
- }
-
- public function createFormatter()
- {
- $num = array_push($this->formatters, new PHPMDFormatterElement($this));
- return $this->formatters[$num-1];
- }
-
- public function main()
- {
- 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) {
-
- $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();
- }
-
- $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 {
-
- 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();
- }
- }
- }
- $inputPath = implode(',', $filesToParse);
- $this->log('Processing files...');
- $phpmd->processFiles(
- $inputPath,
- $this->rulesets,
- $reportRenderers,
- $ruleSetFactory
- );
- $this->log('Finished processing files');
- }
- }
|