PHPMDTask.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * $Id: PHPMDTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. /**
  23. * Runs PHP Mess Detector. Checking PHP files for several potential problems
  24. * based on rulesets.
  25. *
  26. * @package phing.tasks.ext.phpmd
  27. * @author Benjamin Schultz <bschultz@proqrent.de>
  28. * @version $Id: PHPMDTask.php 905 2010-10-05 16:28:03Z mrook $
  29. * @since 2.4.1
  30. */
  31. class PHPMDTask extends Task
  32. {
  33. /**
  34. * A php source code filename or directory
  35. *
  36. * @var PhingFile
  37. */
  38. protected $file = null;
  39. /**
  40. * All fileset objects assigned to this task
  41. *
  42. * @var array<FileSet>
  43. */
  44. protected $filesets = array();
  45. /**
  46. * The rule-set filenames or identifier.
  47. *
  48. * @var string
  49. */
  50. protected $rulesets = 'codesize,unusedcode';
  51. /**
  52. * The minimum priority for rules to load.
  53. *
  54. * @var integer
  55. */
  56. protected $minimumPriority = 5;
  57. /**
  58. * List of valid file extensions for analyzed files.
  59. *
  60. * @var array
  61. */
  62. protected $allowedFileExtensions = array('php');
  63. /**
  64. * List of exclude directory patterns.
  65. *
  66. * @var array
  67. */
  68. protected $ignorePatterns = array('.git', '.svn', 'CVS', '.bzr', '.hg');
  69. /**
  70. * The format for the report
  71. *
  72. * @var string
  73. */
  74. protected $format = 'text';
  75. /**
  76. * Formatter elements.
  77. *
  78. * @var array<PHPMDFormatterElement>
  79. */
  80. protected $formatters = array();
  81. /**
  82. * Load the necessary environment for running PHPMD.
  83. *
  84. * @throws BuildException - if the phpmd classes can't be loaded.
  85. */
  86. public function init()
  87. {
  88. /**
  89. * Determine PHPMD version number
  90. */
  91. @include_once 'PHP/PMD.php';
  92. if (! class_exists('PHP_PMD')) {
  93. throw new BuildException(
  94. 'PHPMDTask depends on PHPMD being installed and on include_path.',
  95. $this->getLocation()
  96. );
  97. }
  98. /*$version = PHP_PMD::VERSION;
  99. if (version_compare($version, '0.2.1') < 0) {
  100. throw new BuildException(
  101. "PHPMDTask requires PHPMD version >= 0.2.1",
  102. $this->getLocation()
  103. );
  104. }*/
  105. /**
  106. * Other dependencies that should only be loaded when class is actually used.
  107. */
  108. require_once 'phing/tasks/ext/phpmd/PHPMDFormatterElement.php';
  109. require_once 'PHP/PMD/AbstractRule.php';
  110. $this->minimumPriority = PHP_PMD_AbstractRule::LOWEST_PRIORITY;
  111. }
  112. /**
  113. * Set the input source file or directory.
  114. *
  115. * @param PhingFile $file The input source file or directory.
  116. *
  117. * @return void
  118. */
  119. public function setFile(PhingFile $file)
  120. {
  121. $this->file = $file;
  122. }
  123. /**
  124. * Nested creator, adds a set of files (nested fileset attribute).
  125. *
  126. * @return FileSet The created fileset object
  127. */
  128. public function createFileSet()
  129. {
  130. $num = array_push($this->filesets, new FileSet());
  131. return $this->filesets[$num-1];
  132. }
  133. /**
  134. * Sets the minimum rule priority.
  135. *
  136. * @param integer $minimumPriority Minimum rule priority.
  137. *
  138. * @return void
  139. */
  140. public function setMinimumPriority($minimumPriority)
  141. {
  142. $this->minimumPriority = $minimumPriority;
  143. }
  144. /**
  145. * Sets the rule-sets.
  146. *
  147. * @param string $ruleSetFileNames Comma-separated string of rule-set filenames
  148. * or identifier.
  149. *
  150. * @return void
  151. */
  152. public function setRulesets($ruleSetFileNames)
  153. {
  154. $this->rulesets = $ruleSetFileNames;
  155. }
  156. /**
  157. * Sets a list of filename extensions for valid php source code files.
  158. *
  159. * @param string $fileExtensions List of valid file extensions without leading dot.
  160. *
  161. * @return void
  162. */
  163. public function setAllowedFileExtensions($fileExtensions)
  164. {
  165. $this->allowedFileExtensions = array();
  166. $token = ' ,;';
  167. $ext = strtok($fileExtensions, $token);
  168. while ($ext !== false) {
  169. $this->allowedFileExtensions[] = $ext;
  170. $ext = strtok($token);
  171. }
  172. }
  173. /**
  174. * Sets a list of ignore patterns that is used to exclude directories from
  175. * the source analysis.
  176. *
  177. * @param string $ignorePatterns List of ignore patterns.
  178. *
  179. * @return void
  180. */
  181. public function setIgnorePatterns($ignorePatterns)
  182. {
  183. $this->ignorePatterns = array();
  184. $token = ' ,;';
  185. $pattern = strtok($ignorePatterns, $token);
  186. while ($pattern !== false) {
  187. $this->ignorePatterns[] = $pattern;
  188. $pattern = strtok($token);
  189. }
  190. }
  191. /**
  192. * Create object for nested formatter element.
  193. *
  194. * @return PHPMDFormatterElement
  195. */
  196. public function createFormatter()
  197. {
  198. $num = array_push($this->formatters, new PHPMDFormatterElement($this));
  199. return $this->formatters[$num-1];
  200. }
  201. /**
  202. * Executes PHPMD against PhingFile or a FileSet
  203. *
  204. * @return void
  205. */
  206. public function main()
  207. {
  208. if (!isset($this->file) and count($this->filesets) == 0) {
  209. throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  210. }
  211. if (count($this->formatters) == 0) {
  212. // turn legacy format attribute into formatter
  213. $fmt = new PHPMDFormatterElement();
  214. $fmt->setType($this->format);
  215. $fmt->setUseFile(false);
  216. $this->formatters[] = $fmt;
  217. }
  218. $reportRenderers = array();
  219. foreach ($this->formatters as $fe) {
  220. if ($fe->getType() == '') {
  221. throw new BuildException("Formatter missing required 'type' attribute.");
  222. }
  223. if ($fe->getUsefile() && $fe->getOutfile() === null) {
  224. throw new BuildException("Formatter requires 'outfile' attribute when 'useFile' is true.");
  225. }
  226. $reportRenderers[] = $fe->getRenderer();
  227. }
  228. // Create a rule set factory
  229. $ruleSetFactory = new PHP_PMD_RuleSetFactory();
  230. $ruleSetFactory->setMinimumPriority($this->minimumPriority);
  231. $phpmd = new PHP_PMD();
  232. $phpmd->setFileExtensions($this->allowedFileExtensions);
  233. $phpmd->setIgnorePattern($this->ignorePatterns);
  234. $filesToParse = array();
  235. if ($this->file instanceof PhingFile) {
  236. $filesToParse[] = $this->file->getPath();
  237. } else {
  238. // append any files in filesets
  239. foreach ($this->filesets as $fs) {
  240. $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
  241. foreach ($files as $filename) {
  242. $f = new PhingFile($fs->getDir($this->project), $filename);
  243. $filesToParse[] = $f->getAbsolutePath();
  244. }
  245. }
  246. }
  247. $inputPath = implode(',', $filesToParse);
  248. $this->log('Processing files...');
  249. $phpmd->processFiles(
  250. $inputPath,
  251. $this->rulesets,
  252. $reportRenderers,
  253. $ruleSetFactory
  254. );
  255. $this->log('Finished processing files');
  256. }
  257. }