Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 abstract class NodeAbstract implements Node, \JsonSerializable
|
Chris@0
|
6 {
|
Chris@0
|
7 protected $attributes;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Creates a Node.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @param array $attributes Array of attributes
|
Chris@0
|
13 */
|
Chris@0
|
14 public function __construct(array $attributes = array()) {
|
Chris@0
|
15 $this->attributes = $attributes;
|
Chris@0
|
16 }
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Gets the type of the node.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @return string Type of the node
|
Chris@0
|
22 */
|
Chris@0
|
23 public function getType() {
|
Chris@12
|
24 $className = rtrim(get_class($this), '_');
|
Chris@12
|
25 return strtr(
|
Chris@12
|
26 substr(
|
Chris@12
|
27 $className,
|
Chris@12
|
28 strpos($className, 'PhpParser\Node') + 15
|
Chris@12
|
29 ),
|
Chris@12
|
30 '\\',
|
Chris@12
|
31 '_'
|
Chris@12
|
32 );
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Gets line the node started in.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @return int Line
|
Chris@0
|
39 */
|
Chris@0
|
40 public function getLine() {
|
Chris@0
|
41 return $this->getAttribute('startLine', -1);
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Sets line the node started in.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @param int $line Line
|
Chris@0
|
48 *
|
Chris@0
|
49 * @deprecated
|
Chris@0
|
50 */
|
Chris@0
|
51 public function setLine($line) {
|
Chris@0
|
52 $this->setAttribute('startLine', (int) $line);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Gets the doc comment of the node.
|
Chris@0
|
57 *
|
Chris@0
|
58 * The doc comment has to be the last comment associated with the node.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return null|Comment\Doc Doc comment object or null
|
Chris@0
|
61 */
|
Chris@0
|
62 public function getDocComment() {
|
Chris@0
|
63 $comments = $this->getAttribute('comments');
|
Chris@0
|
64 if (!$comments) {
|
Chris@0
|
65 return null;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 $lastComment = $comments[count($comments) - 1];
|
Chris@0
|
69 if (!$lastComment instanceof Comment\Doc) {
|
Chris@0
|
70 return null;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 return $lastComment;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Sets the doc comment of the node.
|
Chris@0
|
78 *
|
Chris@0
|
79 * This will either replace an existing doc comment or add it to the comments array.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @param Comment\Doc $docComment Doc comment to set
|
Chris@0
|
82 */
|
Chris@0
|
83 public function setDocComment(Comment\Doc $docComment) {
|
Chris@0
|
84 $comments = $this->getAttribute('comments', []);
|
Chris@0
|
85
|
Chris@0
|
86 $numComments = count($comments);
|
Chris@0
|
87 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
|
Chris@0
|
88 // Replace existing doc comment
|
Chris@0
|
89 $comments[$numComments - 1] = $docComment;
|
Chris@0
|
90 } else {
|
Chris@0
|
91 // Append new comment
|
Chris@0
|
92 $comments[] = $docComment;
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 $this->setAttribute('comments', $comments);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 public function setAttribute($key, $value) {
|
Chris@0
|
99 $this->attributes[$key] = $value;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 public function hasAttribute($key) {
|
Chris@0
|
103 return array_key_exists($key, $this->attributes);
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 public function &getAttribute($key, $default = null) {
|
Chris@0
|
107 if (!array_key_exists($key, $this->attributes)) {
|
Chris@0
|
108 return $default;
|
Chris@0
|
109 } else {
|
Chris@0
|
110 return $this->attributes[$key];
|
Chris@0
|
111 }
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 public function getAttributes() {
|
Chris@0
|
115 return $this->attributes;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 public function jsonSerialize() {
|
Chris@0
|
119 return ['nodeType' => $this->getType()] + get_object_vars($this);
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|