PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/pdepend/pdepend/src/main/php/PDepend/Source/AST/ASTNode.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 540 lines | 187 code | 40 blank | 313 comment | 6 complexity | 0e50dea73b0b71c3ea4ea86d6fd0bf31 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PDepend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2015, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @copyright 2008-2015 Manuel Pichler. All rights reserved.
  40. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  41. * @since 0.9.6
  42. */
  43. namespace PDepend\Source\AST;
  44. /**
  45. * This is an abstract base implementation of the ast node interface.
  46. *
  47. * @copyright 2008-2015 Manuel Pichler. All rights reserved.
  48. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  49. * @since 0.9.6
  50. */
  51. abstract class ASTNode
  52. {
  53. /**
  54. * Parsed child nodes of this node.
  55. *
  56. * @var \PDepend\Source\AST\ASTNode[]
  57. */
  58. protected $nodes = array();
  59. /**
  60. * The parent node of this node or <b>null</b> when this node is the root
  61. * of a node tree.
  62. *
  63. * @var \PDepend\Source\AST\ASTNode
  64. */
  65. protected $parent = null;
  66. /**
  67. * An optional doc comment for this node.
  68. *
  69. * @var string
  70. */
  71. protected $comment = null;
  72. /**
  73. * Metadata for this node instance, serialized in a string. This string
  74. * contains the start, end line, and the start, end column and the node
  75. * image in a colon seperated string.
  76. *
  77. * @var string
  78. * @since 0.10.4
  79. */
  80. protected $metadata = '::::';
  81. /**
  82. * Constructs a new ast node instance.
  83. *
  84. * @param string $image The source image for this node.
  85. */
  86. public function __construct($image = null)
  87. {
  88. $this->metadata = str_repeat(':', $this->getMetadataSize() - 1);
  89. $this->setImage($image);
  90. }
  91. /**
  92. * Sets the image for this ast node.
  93. *
  94. * @param string $image The image for this node.
  95. *
  96. * @return void
  97. * @since 0.10.4
  98. */
  99. public function setImage($image)
  100. {
  101. $this->setMetadata(4, $image);
  102. }
  103. /**
  104. * Returns the source image of this ast node.
  105. *
  106. * @return string
  107. */
  108. public function getImage()
  109. {
  110. return $this->getMetadata(4);
  111. }
  112. /**
  113. * Sets the start line for this ast node.
  114. *
  115. * @param integer $startLine The node start line.
  116. *
  117. * @return void
  118. * @since 0.9.12
  119. */
  120. public function setStartLine($startLine)
  121. {
  122. $this->setMetadataInteger(0, $startLine);
  123. }
  124. /**
  125. * Returns the start line for this ast node.
  126. *
  127. * @return integer
  128. */
  129. public function getStartLine()
  130. {
  131. return $this->getMetadataInteger(0);
  132. }
  133. /**
  134. * Sets the start column for this ast node.
  135. *
  136. * @param integer $startColumn The node start column.
  137. *
  138. * @return void
  139. * @since 0.9.12
  140. */
  141. public function setStartColumn($startColumn)
  142. {
  143. $this->setMetadataInteger(2, $startColumn);
  144. }
  145. /**
  146. * Returns the start column for this ast node.
  147. *
  148. * @return integer
  149. */
  150. public function getStartColumn()
  151. {
  152. return $this->getMetadataInteger(2);
  153. }
  154. /**
  155. * Sets the node's end line.
  156. *
  157. * @param integer $endLine The node's end line.
  158. *
  159. * @return void
  160. * @since 0.9.12
  161. */
  162. public function setEndLine($endLine)
  163. {
  164. $this->setMetadataInteger(1, $endLine);
  165. }
  166. /**
  167. * Returns the end line for this ast node.
  168. *
  169. * @return integer
  170. */
  171. public function getEndLine()
  172. {
  173. return $this->getMetadataInteger(1);
  174. }
  175. /**
  176. * Sets the node's end column.
  177. *
  178. * @param integer $endColumn The node's end column.
  179. *
  180. * @return void
  181. * @since 0.9.12
  182. */
  183. public function setEndColumn($endColumn)
  184. {
  185. $this->setMetadataInteger(3, $endColumn);
  186. }
  187. /**
  188. * Returns the end column for this ast node.
  189. *
  190. * @return integer
  191. */
  192. public function getEndColumn()
  193. {
  194. return $this->getMetadataInteger(3);
  195. }
  196. /**
  197. * For better performance we have moved the single setter methods for the
  198. * node columns and lines into this configure method.
  199. *
  200. * @param integer $startLine The node's start line.
  201. * @param integer $endLine The node's end line.
  202. * @param integer $startColumn The node's start column.
  203. * @param integer $endColumn The node's end column.
  204. *
  205. * @return void
  206. * @since 0.9.10
  207. */
  208. public function configureLinesAndColumns(
  209. $startLine,
  210. $endLine,
  211. $startColumn,
  212. $endColumn
  213. ) {
  214. $this->setMetadataInteger(0, $startLine);
  215. $this->setMetadataInteger(1, $endLine);
  216. $this->setMetadataInteger(2, $startColumn);
  217. $this->setMetadataInteger(3, $endColumn);
  218. }
  219. /**
  220. * Returns an integer value that was stored under the given index.
  221. *
  222. * @param integer $index The property instance.
  223. *
  224. * @return integer
  225. * @since 0.10.4
  226. */
  227. protected function getMetadataInteger($index)
  228. {
  229. return (int) $this->getMetadata($index);
  230. }
  231. /**
  232. * Stores an integer value under the given index in the internally used data
  233. * string.
  234. *
  235. * @param integer $index The property instance.
  236. * @param integer $value The property value.
  237. *
  238. * @return void
  239. * @since 0.10.4
  240. */
  241. protected function setMetadataInteger($index, $value)
  242. {
  243. $this->setMetadata($index, $value);
  244. }
  245. /**
  246. * Returns a boolean value that was stored under the given index.
  247. *
  248. * @param integer $index The property instance.
  249. *
  250. * @return boolean
  251. * @since 0.10.4
  252. */
  253. protected function getMetadataBoolean($index)
  254. {
  255. return (bool) $this->getMetadata($index);
  256. }
  257. /**
  258. * Stores a boolean value under the given index in the internally used data
  259. * string.
  260. *
  261. * @param integer $index The property instance.
  262. * @param boolean $value The property value.
  263. *
  264. * @return void
  265. * @since 0.10.4
  266. */
  267. protected function setMetadataBoolean($index, $value)
  268. {
  269. $this->setMetadata($index, $value ? 1 : 0);
  270. }
  271. /**
  272. * Returns the value that was stored under the given index.
  273. *
  274. * @param integer $index The property instance.
  275. *
  276. * @return mixed
  277. * @since 0.10.4
  278. */
  279. protected function getMetadata($index)
  280. {
  281. $metadata = explode(':', $this->metadata, $this->getMetadataSize());
  282. return $metadata[$index];
  283. }
  284. /**
  285. * Stores the given value under the given index in an internal storage
  286. * container.
  287. *
  288. * @param integer $index The property index.
  289. * @param mixed $value The property value.
  290. *
  291. * @return void
  292. * @since 0.10.4
  293. */
  294. protected function setMetadata($index, $value)
  295. {
  296. $metadata = explode(':', $this->metadata, $this->getMetadataSize());
  297. $metadata[$index] = $value;
  298. $this->metadata = join(':', $metadata);
  299. }
  300. /**
  301. * Returns the total number of the used property bag.
  302. *
  303. * @return integer
  304. * @since 0.10.4
  305. */
  306. protected function getMetadataSize()
  307. {
  308. return 5;
  309. }
  310. /**
  311. * Returns the node instance for the given index or throws an exception.
  312. *
  313. * @param integer $index Index of the requested node.
  314. *
  315. * @return \PDepend\Source\AST\ASTNode
  316. * @throws \OutOfBoundsException When no node exists at the given index.
  317. */
  318. public function getChild($index)
  319. {
  320. if (isset($this->nodes[$index])) {
  321. return $this->nodes[$index];
  322. }
  323. throw new \OutOfBoundsException(
  324. sprintf(
  325. 'No node found at index %d in node of type: %s',
  326. $index,
  327. get_class($this)
  328. )
  329. );
  330. }
  331. /**
  332. * This method returns all direct children of the actual node.
  333. *
  334. * @return \PDepend\Source\AST\ASTNode[]
  335. */
  336. public function getChildren()
  337. {
  338. return $this->nodes;
  339. }
  340. /**
  341. * This method will search recursive for the first child node that is an
  342. * instance of the given <b>$targetType</b>. The returned value will be
  343. * <b>null</b> if no child exists for that.
  344. *
  345. * @param string $targetType Searched class or interface type.
  346. *
  347. * @return \PDepend\Source\AST\ASTNode
  348. */
  349. public function getFirstChildOfType($targetType)
  350. {
  351. foreach ($this->nodes as $node) {
  352. if ($node instanceof $targetType) {
  353. return $node;
  354. }
  355. if (($child = $node->getFirstChildOfType($targetType)) !== null) {
  356. return $child;
  357. }
  358. }
  359. return null;
  360. }
  361. /**
  362. * This method will search recursive for all child nodes that are an
  363. * instance of the given <b>$targetType</b>. The returned value will be
  364. * an empty <b>array</b> if no child exists for that.
  365. *
  366. * @param string $targetType Searched class or interface type.
  367. * @param array &$results Already found node instances. This parameter
  368. * is only for internal usage.
  369. *
  370. * @return \PDepend\Source\AST\ASTNode[]
  371. */
  372. public function findChildrenOfType($targetType, array &$results = array())
  373. {
  374. foreach ($this->nodes as $node) {
  375. if ($node instanceof $targetType) {
  376. $results[] = $node;
  377. }
  378. $node->findChildrenOfType($targetType, $results);
  379. }
  380. return $results;
  381. }
  382. /**
  383. * This method adds a new child node at the first position of the children.
  384. *
  385. * @param \PDepend\Source\AST\ASTNode $node The new child node.
  386. *
  387. * @return void
  388. * @since 0.10.2
  389. */
  390. public function prependChild(\PDepend\Source\AST\ASTNode $node)
  391. {
  392. array_unshift($this->nodes, $node);
  393. $node->setParent($this);
  394. }
  395. /**
  396. * This method adds a new child node to this node instance.
  397. *
  398. * @param \PDepend\Source\AST\ASTNode $node The new child node.
  399. *
  400. * @return void
  401. */
  402. public function addChild(\PDepend\Source\AST\ASTNode $node)
  403. {
  404. $this->nodes[] = $node;
  405. $node->setParent($this);
  406. }
  407. /**
  408. * Returns the parent node of this node or <b>null</b> when this node is
  409. * the root of a node tree.
  410. *
  411. * @return \PDepend\Source\AST\ASTNode
  412. */
  413. public function getParent()
  414. {
  415. return $this->parent;
  416. }
  417. /**
  418. * Traverses up the node tree and finds all parent nodes that are instances
  419. * of <b>$parentType</b>.
  420. *
  421. * @param string $parentType Class/interface type you are looking for,
  422. *
  423. * @return \PDepend\Source\AST\ASTNode[]
  424. */
  425. public function getParentsOfType($parentType)
  426. {
  427. $parents = array();
  428. $parentNode = $this->parent;
  429. while (is_object($parentNode)) {
  430. if ($parentNode instanceof $parentType) {
  431. array_unshift($parents, $parentNode);
  432. }
  433. $parentNode = $parentNode->getParent();
  434. }
  435. return $parents;
  436. }
  437. /**
  438. * Sets the parent node of this node.
  439. *
  440. * @param \PDepend\Source\AST\ASTNode $node The parent node of this node.
  441. *
  442. * @return void
  443. */
  444. public function setParent(\PDepend\Source\AST\ASTNode $node)
  445. {
  446. $this->parent = $node;
  447. }
  448. /**
  449. * Returns a doc comment for this node or <b>null</b> when no comment was
  450. * found.
  451. *
  452. * @return string
  453. */
  454. public function getComment()
  455. {
  456. return $this->comment;
  457. }
  458. /**
  459. * Sets the raw doc comment for this node.
  460. *
  461. * @param string $comment The doc comment block for this node.
  462. *
  463. * @return void
  464. */
  465. public function setComment($comment)
  466. {
  467. $this->comment = $comment;
  468. }
  469. /**
  470. * The magic sleep method will be called by PHP's runtime environment right
  471. * before an instance of this class gets serialized. It should return an
  472. * array with those property names that should be serialized for this class.
  473. *
  474. * @return array
  475. * @since 0.10.0
  476. */
  477. public function __sleep()
  478. {
  479. return array(
  480. 'comment',
  481. 'metadata',
  482. 'nodes'
  483. );
  484. }
  485. /**
  486. * The magic wakeup method will be called by PHP's runtime environment when
  487. * a previously serialized object gets unserialized. This implementation of
  488. * the wakeup method restores the dependencies between an ast node and the
  489. * node's children.
  490. *
  491. * @return void
  492. * @since 0.10.0
  493. */
  494. public function __wakeup()
  495. {
  496. foreach ($this->nodes as $node) {
  497. $node->parent = $this;
  498. }
  499. }
  500. }