comparison vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser; 3 namespace PhpParser;
4
5 use PhpParser\Node;
4 6
5 abstract class NodeAbstract implements Node, \JsonSerializable 7 abstract class NodeAbstract implements Node, \JsonSerializable
6 { 8 {
7 protected $attributes; 9 protected $attributes;
8 10
9 /** 11 /**
10 * Creates a Node. 12 * Creates a Node.
11 * 13 *
12 * @param array $attributes Array of attributes 14 * @param array $attributes Array of attributes
13 */ 15 */
14 public function __construct(array $attributes = array()) { 16 public function __construct(array $attributes = []) {
15 $this->attributes = $attributes; 17 $this->attributes = $attributes;
16 } 18 }
17 19
18 /** 20 /**
19 * Gets the type of the node. 21 * Gets line the node started in (alias of getStartLine).
20 * 22 *
21 * @return string Type of the node 23 * @return int Start line (or -1 if not available)
22 */ 24 */
23 public function getType() { 25 public function getLine() : int {
24 $className = rtrim(get_class($this), '_'); 26 return $this->attributes['startLine'] ?? -1;
25 return strtr(
26 substr(
27 $className,
28 strpos($className, 'PhpParser\Node') + 15
29 ),
30 '\\',
31 '_'
32 );
33 } 27 }
34 28
35 /** 29 /**
36 * Gets line the node started in. 30 * Gets line the node started in.
37 * 31 *
38 * @return int Line 32 * Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
33 *
34 * @return int Start line (or -1 if not available)
39 */ 35 */
40 public function getLine() { 36 public function getStartLine() : int {
41 return $this->getAttribute('startLine', -1); 37 return $this->attributes['startLine'] ?? -1;
42 } 38 }
43 39
44 /** 40 /**
45 * Sets line the node started in. 41 * Gets the line the node ended in.
46 * 42 *
47 * @param int $line Line 43 * Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
48 * 44 *
49 * @deprecated 45 * @return int End line (or -1 if not available)
50 */ 46 */
51 public function setLine($line) { 47 public function getEndLine() : int {
52 $this->setAttribute('startLine', (int) $line); 48 return $this->attributes['endLine'] ?? -1;
49 }
50
51 /**
52 * Gets the token offset of the first token that is part of this node.
53 *
54 * The offset is an index into the array returned by Lexer::getTokens().
55 *
56 * Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
57 *
58 * @return int Token start position (or -1 if not available)
59 */
60 public function getStartTokenPos() : int {
61 return $this->attributes['startTokenPos'] ?? -1;
62 }
63
64 /**
65 * Gets the token offset of the last token that is part of this node.
66 *
67 * The offset is an index into the array returned by Lexer::getTokens().
68 *
69 * Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
70 *
71 * @return int Token end position (or -1 if not available)
72 */
73 public function getEndTokenPos() : int {
74 return $this->attributes['endTokenPos'] ?? -1;
75 }
76
77 /**
78 * Gets the file offset of the first character that is part of this node.
79 *
80 * Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
81 *
82 * @return int File start position (or -1 if not available)
83 */
84 public function getStartFilePos() : int {
85 return $this->attributes['startFilePos'] ?? -1;
86 }
87
88 /**
89 * Gets the file offset of the last character that is part of this node.
90 *
91 * Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
92 *
93 * @return int File end position (or -1 if not available)
94 */
95 public function getEndFilePos() : int {
96 return $this->attributes['endFilePos'] ?? -1;
97 }
98
99 /**
100 * Gets all comments directly preceding this node.
101 *
102 * The comments are also available through the "comments" attribute.
103 *
104 * @return Comment[]
105 */
106 public function getComments() : array {
107 return $this->attributes['comments'] ?? [];
53 } 108 }
54 109
55 /** 110 /**
56 * Gets the doc comment of the node. 111 * Gets the doc comment of the node.
57 * 112 *
58 * The doc comment has to be the last comment associated with the node. 113 * The doc comment has to be the last comment associated with the node.
59 * 114 *
60 * @return null|Comment\Doc Doc comment object or null 115 * @return null|Comment\Doc Doc comment object or null
61 */ 116 */
62 public function getDocComment() { 117 public function getDocComment() {
63 $comments = $this->getAttribute('comments'); 118 $comments = $this->getComments();
64 if (!$comments) { 119 if (!$comments) {
65 return null; 120 return null;
66 } 121 }
67 122
68 $lastComment = $comments[count($comments) - 1]; 123 $lastComment = $comments[count($comments) - 1];
79 * This will either replace an existing doc comment or add it to the comments array. 134 * This will either replace an existing doc comment or add it to the comments array.
80 * 135 *
81 * @param Comment\Doc $docComment Doc comment to set 136 * @param Comment\Doc $docComment Doc comment to set
82 */ 137 */
83 public function setDocComment(Comment\Doc $docComment) { 138 public function setDocComment(Comment\Doc $docComment) {
84 $comments = $this->getAttribute('comments', []); 139 $comments = $this->getComments();
85 140
86 $numComments = count($comments); 141 $numComments = count($comments);
87 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) { 142 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
88 // Replace existing doc comment 143 // Replace existing doc comment
89 $comments[$numComments - 1] = $docComment; 144 $comments[$numComments - 1] = $docComment;
93 } 148 }
94 149
95 $this->setAttribute('comments', $comments); 150 $this->setAttribute('comments', $comments);
96 } 151 }
97 152
98 public function setAttribute($key, $value) { 153 public function setAttribute(string $key, $value) {
99 $this->attributes[$key] = $value; 154 $this->attributes[$key] = $value;
100 } 155 }
101 156
102 public function hasAttribute($key) { 157 public function hasAttribute(string $key) : bool {
103 return array_key_exists($key, $this->attributes); 158 return array_key_exists($key, $this->attributes);
104 } 159 }
105 160
106 public function &getAttribute($key, $default = null) { 161 public function getAttribute(string $key, $default = null) {
107 if (!array_key_exists($key, $this->attributes)) { 162 if (!array_key_exists($key, $this->attributes)) {
108 return $default; 163 return $default;
109 } else { 164 } else {
110 return $this->attributes[$key]; 165 return $this->attributes[$key];
111 } 166 }
112 } 167 }
113 168
114 public function getAttributes() { 169 public function getAttributes() : array {
115 return $this->attributes; 170 return $this->attributes;
116 } 171 }
117 172
118 public function jsonSerialize() { 173 public function setAttributes(array $attributes) {
174 $this->attributes = $attributes;
175 }
176
177 /**
178 * @return array
179 */
180 public function jsonSerialize() : array {
119 return ['nodeType' => $this->getType()] + get_object_vars($this); 181 return ['nodeType' => $this->getType()] + get_object_vars($this);
120 } 182 }
121 } 183 }