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

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

https://gitlab.com/yousafsyed/easternglamor
PHP | 306 lines | 147 code | 19 blank | 140 comment | 3 complexity | 48d0212ecf2def7ecec1acc8cc769575 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PHPMD.
  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 PHPMD\Node\ASTNode;
  43. /**
  44. * This is an abstract base class for PHPMD code nodes, it is just a wrapper
  45. * around PDepend's object model.
  46. *
  47. * @author Manuel Pichler <mapi@phpmd.org>
  48. * @copyright 2008-2014 Manuel Pichler. All rights reserved.
  49. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  50. */
  51. abstract class AbstractNode
  52. {
  53. /**
  54. *
  55. * @var \PDepend\Source\AST\ASTArtifact|\PDepend\Source\AST\ASTNode $node
  56. */
  57. private $node = null;
  58. /**
  59. * The collected metrics for this node.
  60. *
  61. * @var array(string=>mixed) $_metrics
  62. */
  63. private $metrics = null;
  64. /**
  65. * Constructs a new PHPMD node.
  66. *
  67. * @param \PDepend\Source\AST\ASTArtifact|\PDepend\Source\AST\ASTNode $node
  68. */
  69. public function __construct($node)
  70. {
  71. $this->node = $node;
  72. }
  73. /**
  74. * The magic call method is used to pipe requests from rules direct
  75. * to the underlying PDepend ast node.
  76. *
  77. * @param string $name
  78. * @param array $args
  79. * @return mixed
  80. * @throws \BadMethodCallException When the underlying PDepend node
  81. * does not contain a method named <b>$name</b>.
  82. */
  83. public function __call($name, array $args)
  84. {
  85. if (method_exists($this->getNode(), $name)) {
  86. return call_user_func_array(array($this->getNode(), $name), $args);
  87. }
  88. throw new \BadMethodCallException(
  89. sprintf('Invalid method %s() called.', $name)
  90. );
  91. }
  92. /**
  93. * Returns the parent of this node or <b>null</b> when no parent node
  94. * exists.
  95. *
  96. * @return \PHPMD\AbstractNode
  97. */
  98. public function getParent()
  99. {
  100. if (($node = $this->node->getParent()) === null) {
  101. return null;
  102. }
  103. return new ASTNode($node, $this->getFileName());
  104. }
  105. /**
  106. * Returns a child node at the given index.
  107. *
  108. * @param integer $index The child offset.
  109. *
  110. * @return \PHPMD\Node\ASTNode
  111. */
  112. public function getChild($index)
  113. {
  114. return new ASTNode(
  115. $this->node->getChild($index),
  116. $this->getFileName()
  117. );
  118. }
  119. /**
  120. * Returns the first child of the given type or <b>null</b> when this node
  121. * has no child of the given type.
  122. *
  123. * @param string $type The searched child type.
  124. * @return \PHPMD\AbstractNode
  125. */
  126. public function getFirstChildOfType($type)
  127. {
  128. $node = $this->node->getFirstChildOfType('PDepend\Source\AST\AST' . $type);
  129. if ($node === null) {
  130. return null;
  131. }
  132. return new ASTNode($node, $this->getFileName());
  133. }
  134. /**
  135. * Searches recursive for all children of this node that are of the given
  136. * type.
  137. *
  138. * @param string $type The searched child type.
  139. * @return \PHPMD\AbstractNode[]
  140. */
  141. public function findChildrenOfType($type)
  142. {
  143. $children = $this->node->findChildrenOfType('PDepend\Source\AST\AST' . $type);
  144. $nodes = array();
  145. foreach ($children as $child) {
  146. $nodes[] = new ASTNode($child, $this->getFileName());
  147. }
  148. return $nodes;
  149. }
  150. /**
  151. * Tests if this node represents the the given type.
  152. *
  153. * @param string $type The expected node type.
  154. * @return boolean
  155. */
  156. public function isInstanceOf($type)
  157. {
  158. $class = 'PDepend\Source\AST\AST' . $type;
  159. return ($this->node instanceof $class);
  160. }
  161. /**
  162. * Returns the image of the underlying node.
  163. *
  164. * @return string
  165. */
  166. public function getImage()
  167. {
  168. return $this->node->getName();
  169. }
  170. /**
  171. * Returns the source name for this node, maybe a class or interface name,
  172. * or a package, method, function name.
  173. *
  174. * @return string
  175. */
  176. public function getName()
  177. {
  178. return $this->node->getName();
  179. }
  180. /**
  181. * Returns the begin line for this node in the php source code file.
  182. *
  183. * @return integer
  184. */
  185. public function getBeginLine()
  186. {
  187. return $this->node->getStartLine();
  188. }
  189. /**
  190. * Returns the end line for this node in the php source code file.
  191. *
  192. * @return integer
  193. */
  194. public function getEndLine()
  195. {
  196. return $this->node->getEndLine();
  197. }
  198. /**
  199. * Returns the name of the declaring source file.
  200. *
  201. * @return string
  202. */
  203. public function getFileName()
  204. {
  205. return (string) $this->node->getCompilationUnit()->getFileName();
  206. }
  207. /**
  208. * Returns the wrapped PDepend node instance.
  209. *
  210. * @return \PDepend\Source\AST\ASTArtifact
  211. */
  212. public function getNode()
  213. {
  214. return $this->node;
  215. }
  216. /**
  217. * Returns a textual representation/name for the concrete node type.
  218. *
  219. * @return string
  220. */
  221. public function getType()
  222. {
  223. $type = explode('\\', get_class($this));
  224. return preg_replace('(node$)', '', strtolower(array_pop($type)));
  225. }
  226. /**
  227. * This method will return the metric value for the given identifier or
  228. * <b>null</b> when no such metric exists.
  229. *
  230. * @param string $name The metric name or abbreviation.
  231. *
  232. * @return mixed
  233. */
  234. public function getMetric($name)
  235. {
  236. if (isset($this->metrics[$name])) {
  237. return $this->metrics[$name];
  238. }
  239. return null;
  240. }
  241. /**
  242. * This method will set the metrics for this node.
  243. *
  244. * @param array(string=>mixed) $metrics The collected node metrics.
  245. * @return void
  246. */
  247. public function setMetrics(array $metrics)
  248. {
  249. if ($this->metrics === null) {
  250. $this->metrics = $metrics;
  251. }
  252. }
  253. /**
  254. * Checks if this node has a suppressed annotation for the given rule
  255. * instance.
  256. *
  257. * @param \PHPMD\Rule $rule
  258. * @return boolean
  259. */
  260. abstract public function hasSuppressWarningsAnnotationFor(Rule $rule);
  261. /**
  262. * Returns the full qualified name of a class, an interface, a method or
  263. * a function.
  264. *
  265. * @return string
  266. */
  267. abstract public function getQName();
  268. /**
  269. * Returns the name of the parent type or <b>null</b> when this node has no
  270. * parent type.
  271. *
  272. * @return string
  273. */
  274. abstract public function getParentName();
  275. /**
  276. * Returns the name of the parent package.
  277. *
  278. * @return string
  279. */
  280. abstract public function getNamespaceName();
  281. }