comparison vendor/nikic/php-parser/lib/PhpParser/Node/Name.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 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser\Node; 3 namespace PhpParser\Node;
4 4
5 use PhpParser\NodeAbstract; 5 use PhpParser\NodeAbstract;
6 6
9 /** 9 /**
10 * @var string[] Parts of the name 10 * @var string[] Parts of the name
11 */ 11 */
12 public $parts; 12 public $parts;
13 13
14 private static $specialClassNames = [
15 'self' => true,
16 'parent' => true,
17 'static' => true,
18 ];
19
14 /** 20 /**
15 * Constructs a name node. 21 * Constructs a name node.
16 * 22 *
17 * @param string|array|self $name Name as string, part array or Name instance (copy ctor) 23 * @param string|string[]|self $name Name as string, part array or Name instance (copy ctor)
18 * @param array $attributes Additional attributes 24 * @param array $attributes Additional attributes
19 */ 25 */
20 public function __construct($name, array $attributes = array()) { 26 public function __construct($name, array $attributes = []) {
21 parent::__construct($attributes); 27 parent::__construct($attributes);
22 $this->parts = self::prepareName($name); 28 $this->parts = self::prepareName($name);
23 } 29 }
24 30
25 public function getSubNodeNames() { 31 public function getSubNodeNames() : array {
26 return array('parts'); 32 return ['parts'];
27 } 33 }
28 34
29 /** 35 /**
30 * Gets the first part of the name, i.e. everything before the first namespace separator. 36 * Gets the first part of the name, i.e. everything before the first namespace separator.
31 * 37 *
32 * @return string First part of the name 38 * @return string First part of the name
33 */ 39 */
34 public function getFirst() { 40 public function getFirst() : string {
35 return $this->parts[0]; 41 return $this->parts[0];
36 } 42 }
37 43
38 /** 44 /**
39 * Gets the last part of the name, i.e. everything after the last namespace separator. 45 * Gets the last part of the name, i.e. everything after the last namespace separator.
40 * 46 *
41 * @return string Last part of the name 47 * @return string Last part of the name
42 */ 48 */
43 public function getLast() { 49 public function getLast() : string {
44 return $this->parts[count($this->parts) - 1]; 50 return $this->parts[count($this->parts) - 1];
45 } 51 }
46 52
47 /** 53 /**
48 * Checks whether the name is unqualified. (E.g. Name) 54 * Checks whether the name is unqualified. (E.g. Name)
49 * 55 *
50 * @return bool Whether the name is unqualified 56 * @return bool Whether the name is unqualified
51 */ 57 */
52 public function isUnqualified() { 58 public function isUnqualified() : bool {
53 return 1 == count($this->parts); 59 return 1 === count($this->parts);
54 } 60 }
55 61
56 /** 62 /**
57 * Checks whether the name is qualified. (E.g. Name\Name) 63 * Checks whether the name is qualified. (E.g. Name\Name)
58 * 64 *
59 * @return bool Whether the name is qualified 65 * @return bool Whether the name is qualified
60 */ 66 */
61 public function isQualified() { 67 public function isQualified() : bool {
62 return 1 < count($this->parts); 68 return 1 < count($this->parts);
63 } 69 }
64 70
65 /** 71 /**
66 * Checks whether the name is fully qualified. (E.g. \Name) 72 * Checks whether the name is fully qualified. (E.g. \Name)
67 * 73 *
68 * @return bool Whether the name is fully qualified 74 * @return bool Whether the name is fully qualified
69 */ 75 */
70 public function isFullyQualified() { 76 public function isFullyQualified() : bool {
71 return false; 77 return false;
72 } 78 }
73 79
74 /** 80 /**
75 * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name) 81 * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
76 * 82 *
77 * @return bool Whether the name is relative 83 * @return bool Whether the name is relative
78 */ 84 */
79 public function isRelative() { 85 public function isRelative() : bool {
80 return false; 86 return false;
87 }
88
89 /**
90 * Returns a string representation of the name itself, without taking taking the name type into
91 * account (e.g., not including a leading backslash for fully qualified names).
92 *
93 * @return string String representation
94 */
95 public function toString() : string {
96 return implode('\\', $this->parts);
97 }
98
99 /**
100 * Returns a string representation of the name as it would occur in code (e.g., including
101 * leading backslash for fully qualified names.
102 *
103 * @return string String representation
104 */
105 public function toCodeString() : string {
106 return $this->toString();
107 }
108
109 /**
110 * Returns lowercased string representation of the name, without taking the name type into
111 * account (e.g., no leading backslash for fully qualified names).
112 *
113 * @return string Lowercased string representation
114 */
115 public function toLowerString() : string {
116 return strtolower(implode('\\', $this->parts));
117 }
118
119 /**
120 * Checks whether the identifier is a special class name (self, parent or static).
121 *
122 * @return bool Whether identifier is a special class name
123 */
124 public function isSpecialClassName() : bool {
125 return count($this->parts) === 1
126 && isset(self::$specialClassNames[strtolower($this->parts[0])]);
81 } 127 }
82 128
83 /** 129 /**
84 * Returns a string representation of the name by imploding the namespace parts with the 130 * Returns a string representation of the name by imploding the namespace parts with the
85 * namespace separator. 131 * namespace separator.
86 * 132 *
87 * @return string String representation 133 * @return string String representation
88 */ 134 */
89 public function toString() { 135 public function __toString() : string {
90 return implode('\\', $this->parts);
91 }
92
93 /**
94 * Returns a string representation of the name by imploding the namespace parts with the
95 * namespace separator.
96 *
97 * @return string String representation
98 */
99 public function __toString() {
100 return implode('\\', $this->parts); 136 return implode('\\', $this->parts);
101 } 137 }
102 138
103 /** 139 /**
104 * Gets a slice of a name (similar to array_slice). 140 * Gets a slice of a name (similar to array_slice).
114 * @param int $offset Offset to start the slice at (may be negative) 150 * @param int $offset Offset to start the slice at (may be negative)
115 * @param int|null $length Length of the slice (may be negative) 151 * @param int|null $length Length of the slice (may be negative)
116 * 152 *
117 * @return static|null Sliced name 153 * @return static|null Sliced name
118 */ 154 */
119 public function slice($offset, $length = null) { 155 public function slice(int $offset, int $length = null) {
120 $numParts = count($this->parts); 156 $numParts = count($this->parts);
121 157
122 $realOffset = $offset < 0 ? $offset + $numParts : $offset; 158 $realOffset = $offset < 0 ? $offset + $numParts : $offset;
123 if ($realOffset < 0 || $realOffset > $numParts) { 159 if ($realOffset < 0 || $realOffset > $numParts) {
124 throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); 160 throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
150 * If one of the arguments is null, a new instance of the other name will be returned. If both 186 * If one of the arguments is null, a new instance of the other name will be returned. If both
151 * arguments are null, null will be returned. As such, writing 187 * arguments are null, null will be returned. As such, writing
152 * Name::concat($namespace, $shortName) 188 * Name::concat($namespace, $shortName)
153 * where $namespace is a Name node or null will work as expected. 189 * where $namespace is a Name node or null will work as expected.
154 * 190 *
155 * @param string|array|self|null $name1 The first name 191 * @param string|string[]|self|null $name1 The first name
156 * @param string|array|self|null $name2 The second name 192 * @param string|string[]|self|null $name2 The second name
157 * @param array $attributes Attributes to assign to concatenated name 193 * @param array $attributes Attributes to assign to concatenated name
158 * 194 *
159 * @return static|null Concatenated name 195 * @return static|null Concatenated name
160 */ 196 */
161 public static function concat($name1, $name2, array $attributes = []) { 197 public static function concat($name1, $name2, array $attributes = []) {
162 if (null === $name1 && null === $name2) { 198 if (null === $name1 && null === $name2) {
163 return null; 199 return null;
164 } elseif (null === $name1) { 200 } elseif (null === $name1) {
165 return new static(self::prepareName($name2), $attributes); 201 return new static(self::prepareName($name2), $attributes);
166 } else if (null === $name2) { 202 } elseif (null === $name2) {
167 return new static(self::prepareName($name1), $attributes); 203 return new static(self::prepareName($name1), $attributes);
168 } else { 204 } else {
169 return new static( 205 return new static(
170 array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes 206 array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
171 ); 207 );
174 210
175 /** 211 /**
176 * Prepares a (string, array or Name node) name for use in name changing methods by converting 212 * Prepares a (string, array or Name node) name for use in name changing methods by converting
177 * it to an array. 213 * it to an array.
178 * 214 *
179 * @param string|array|self $name Name to prepare 215 * @param string|string[]|self $name Name to prepare
180 * 216 *
181 * @return array Prepared name 217 * @return string[] Prepared name
182 */ 218 */
183 private static function prepareName($name) { 219 private static function prepareName($name) : array {
184 if (\is_string($name)) { 220 if (\is_string($name)) {
221 if ('' === $name) {
222 throw new \InvalidArgumentException('Name cannot be empty');
223 }
224
185 return explode('\\', $name); 225 return explode('\\', $name);
186 } elseif (\is_array($name)) { 226 } elseif (\is_array($name)) {
227 if (empty($name)) {
228 throw new \InvalidArgumentException('Name cannot be empty');
229 }
230
187 return $name; 231 return $name;
188 } elseif ($name instanceof self) { 232 } elseif ($name instanceof self) {
189 return $name->parts; 233 return $name->parts;
190 } 234 }
191 235
192 throw new \InvalidArgumentException( 236 throw new \InvalidArgumentException(
193 'Expected string, array of parts or Name instance' 237 'Expected string, array of parts or Name instance'
194 ); 238 );
195 } 239 }
240
241 public function getType() : string {
242 return 'Name';
243 }
196 } 244 }