PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 309 lines | 122 code | 29 blank | 158 comment | 4 complexity | 9c3ecd3d07af3ab4f9b91c5121f288d2 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PHP Mess Detector.
  4. *
  5. * Copyright (c) 2008-2012, Manuel Pichler <mapi@phpmd.org>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Manuel Pichler nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @author Manuel Pichler <mapi@phpmd.org>
  38. * @copyright 2008-2014 Manuel Pichler. All rights reserved.
  39. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  40. */
  41. namespace PHPMD;
  42. use PDepend\Engine;
  43. use PDepend\Report\CodeAwareGenerator;
  44. use PDepend\Source\ASTVisitor\AbstractASTVisitor;
  45. use PDepend\Metrics\Analyzer;
  46. use PDepend\Source\AST\ASTClass;
  47. use PDepend\Source\AST\ASTMethod;
  48. use PDepend\Source\AST\ASTInterface;
  49. use PDepend\Source\AST\ASTFunction;
  50. use PDepend\Source\AST\ASTArtifactList;
  51. use PHPMD\Node\ClassNode;
  52. use PHPMD\Node\FunctionNode;
  53. use PHPMD\Node\InterfaceNode;
  54. use PHPMD\Node\MethodNode;
  55. /**
  56. * Simple wrapper around the php depend engine.
  57. *
  58. * @author Manuel Pichler <mapi@phpmd.org>
  59. * @copyright 2008-2014 Manuel Pichler. All rights reserved.
  60. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  61. */
  62. class Parser extends AbstractASTVisitor implements CodeAwareGenerator
  63. {
  64. /**
  65. * The analysing rule-set instance.
  66. *
  67. * @var \PHPMD\RuleSet[]
  68. */
  69. private $ruleSets = array();
  70. /**
  71. * The metric containing analyzer instances.
  72. *
  73. * @var \PDepend\Metrics\AnalyzerNodeAware[]
  74. */
  75. private $analyzers = array();
  76. /**
  77. * The raw PDepend code nodes.
  78. *
  79. * @var \PDepend\Source\AST\ASTArtifactList
  80. */
  81. private $artifacts = null;
  82. /**
  83. * The violation report used by this PDepend adapter.
  84. *
  85. * @var \PHPMD\Report
  86. */
  87. private $report = null;
  88. /**
  89. * The wrapped PDepend Engine instance.
  90. *
  91. * @var \PDepend\Engine
  92. */
  93. private $pdepend = null;
  94. /**
  95. * Constructs a new parser adapter instance.
  96. *
  97. * @param \PDepend\Engine $pdepend The context php depend instance.
  98. */
  99. public function __construct(Engine $pdepend)
  100. {
  101. $this->pdepend = $pdepend;
  102. }
  103. /**
  104. * Parses the projects source and reports all detected errors and violations.
  105. *
  106. * @param \PHPMD\Report $report
  107. * @return void
  108. */
  109. public function parse(Report $report)
  110. {
  111. $this->setReport($report);
  112. $this->pdepend->addReportGenerator($this);
  113. $this->pdepend->analyze();
  114. foreach ($this->pdepend->getExceptions() as $exception) {
  115. $report->addError(new ProcessingError($exception->getMessage()));
  116. }
  117. }
  118. /**
  119. * Adds a new analysis rule-set to this adapter.
  120. *
  121. * @param \PHPMD\RuleSet $ruleSet
  122. * @return void
  123. */
  124. public function addRuleSet(RuleSet $ruleSet)
  125. {
  126. $this->ruleSets[] = $ruleSet;
  127. }
  128. /**
  129. * Sets the violation report used by the rule-set.
  130. *
  131. * @param \PHPMD\Report $report
  132. * @return void
  133. */
  134. public function setReport(Report $report)
  135. {
  136. $this->report = $report;
  137. }
  138. /**
  139. * Adds an analyzer to log. If this logger accepts the given analyzer it
  140. * with return <b>true</b>, otherwise the return value is <b>false</b>.
  141. *
  142. * @param \PDepend\Metrics\Analyzer $analyzer The analyzer to log.
  143. *
  144. * @return boolean
  145. */
  146. public function log(Analyzer $analyzer)
  147. {
  148. $this->analyzers[] = $analyzer;
  149. }
  150. /**
  151. * Closes the logger process and writes the output file.
  152. *
  153. * @return void
  154. * @throws \PDepend\Report\NoLogOutputException If the no log target exists.
  155. */
  156. public function close()
  157. {
  158. // Set max nesting level, because we may get really deep data structures
  159. ini_set('xdebug.max_nesting_level', 8192);
  160. foreach ($this->artifacts as $node) {
  161. $node->accept($this);
  162. }
  163. }
  164. /**
  165. * Returns an <b>array</b> with accepted analyzer types. These types can be
  166. * concrete analyzer classes or one of the descriptive analyzer interfaces.
  167. *
  168. * @return array(string)
  169. */
  170. public function getAcceptedAnalyzers()
  171. {
  172. return array(
  173. 'pdepend.analyzer.cyclomatic_complexity',
  174. 'pdepend.analyzer.node_loc',
  175. 'pdepend.analyzer.npath_complexity',
  176. 'pdepend.analyzer.inheritance',
  177. 'pdepend.analyzer.node_count',
  178. 'pdepend.analyzer.hierarchy',
  179. 'pdepend.analyzer.crap_index',
  180. 'pdepend.analyzer.code_rank',
  181. 'pdepend.analyzer.coupling',
  182. 'pdepend.analyzer.class_level',
  183. 'pdepend.analyzer.cohesion',
  184. );
  185. }
  186. /**
  187. * Visits a class node.
  188. *
  189. * @param \PDepend\Source\AST\ASTClass $node
  190. * @return void
  191. */
  192. public function visitClass(ASTClass $node)
  193. {
  194. if (!$node->isUserDefined()) {
  195. return;
  196. }
  197. $this->apply(new ClassNode($node));
  198. parent::visitClass($node);
  199. }
  200. /**
  201. * Visits a function node.
  202. *
  203. * @param \PDepend\Source\AST\ASTFunction $node
  204. * @return void
  205. */
  206. public function visitFunction(ASTFunction $node)
  207. {
  208. if ($node->getCompilationUnit()->getFileName() === null) {
  209. return;
  210. }
  211. $this->apply(new FunctionNode($node));
  212. }
  213. /**
  214. * Visits an interface node.
  215. *
  216. * @param \PDepend\Source\AST\ASTInterface $node
  217. * @return void
  218. */
  219. public function visitInterface(ASTInterface $node)
  220. {
  221. if (!$node->isUserDefined()) {
  222. return;
  223. }
  224. $this->apply(new InterfaceNode($node));
  225. parent::visitInterface($node);
  226. }
  227. /**
  228. * Visits a method node.
  229. *
  230. * @param \PDepend\Source\AST\ASTMethod $node
  231. * @return void
  232. */
  233. public function visitMethod(ASTMethod $node)
  234. {
  235. if ($node->getCompilationUnit()->getFileName() === null) {
  236. return;
  237. }
  238. $this->apply(new MethodNode($node));
  239. }
  240. /**
  241. * Sets the context code nodes.
  242. *
  243. * @param \PDepend\Source\AST\ASTArtifactList $artifacts
  244. * @return void
  245. */
  246. public function setArtifacts(ASTArtifactList $artifacts)
  247. {
  248. $this->artifacts = $artifacts;
  249. }
  250. /**
  251. * Applies all rule-sets to the given <b>$node</b> instance.
  252. *
  253. * @param \PHPMD\AbstractNode $node
  254. * @return void
  255. */
  256. private function apply(AbstractNode $node)
  257. {
  258. $this->collectMetrics($node);
  259. foreach ($this->ruleSets as $ruleSet) {
  260. $ruleSet->setReport($this->report);
  261. $ruleSet->apply($node);
  262. }
  263. }
  264. /**
  265. * Collects the collected metrics for the given node and adds them to the
  266. * <b>$node</b>.
  267. *
  268. * @param \PHPMD\AbstractNode $node
  269. * @return void
  270. */
  271. private function collectMetrics(AbstractNode $node)
  272. {
  273. $metrics = array();
  274. $pdepend = $node->getNode();
  275. foreach ($this->analyzers as $analyzer) {
  276. $metrics = array_merge($metrics, $analyzer->getNodeMetrics($pdepend));
  277. }
  278. $node->setMetrics($metrics);
  279. }
  280. }