Removed modules/contrib/media module to allow update to the core media module
[yaffs-website] / vendor / nikic / php-parser / lib / PhpParser / Node / Name.php
1 <?php declare(strict_types=1);
2
3 namespace PhpParser\Node;
4
5 use PhpParser\NodeAbstract;
6
7 class Name extends NodeAbstract
8 {
9     /**
10      * @var string[] Parts of the name
11      */
12     public $parts;
13
14     private static $specialClassNames = [
15         'self'   => true,
16         'parent' => true,
17         'static' => true,
18     ];
19
20     /**
21      * Constructs a name node.
22      *
23      * @param string|string[]|self $name       Name as string, part array or Name instance (copy ctor)
24      * @param array                $attributes Additional attributes
25      */
26     public function __construct($name, array $attributes = []) {
27         parent::__construct($attributes);
28         $this->parts = self::prepareName($name);
29     }
30
31     public function getSubNodeNames() : array {
32         return ['parts'];
33     }
34
35     /**
36      * Gets the first part of the name, i.e. everything before the first namespace separator.
37      *
38      * @return string First part of the name
39      */
40     public function getFirst() : string {
41         return $this->parts[0];
42     }
43
44     /**
45      * Gets the last part of the name, i.e. everything after the last namespace separator.
46      *
47      * @return string Last part of the name
48      */
49     public function getLast() : string {
50         return $this->parts[count($this->parts) - 1];
51     }
52
53     /**
54      * Checks whether the name is unqualified. (E.g. Name)
55      *
56      * @return bool Whether the name is unqualified
57      */
58     public function isUnqualified() : bool {
59         return 1 === count($this->parts);
60     }
61
62     /**
63      * Checks whether the name is qualified. (E.g. Name\Name)
64      *
65      * @return bool Whether the name is qualified
66      */
67     public function isQualified() : bool {
68         return 1 < count($this->parts);
69     }
70
71     /**
72      * Checks whether the name is fully qualified. (E.g. \Name)
73      *
74      * @return bool Whether the name is fully qualified
75      */
76     public function isFullyQualified() : bool {
77         return false;
78     }
79
80     /**
81      * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
82      *
83      * @return bool Whether the name is relative
84      */
85     public function isRelative() : bool {
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])]);
127     }
128
129     /**
130      * Returns a string representation of the name by imploding the namespace parts with the
131      * namespace separator.
132      *
133      * @return string String representation
134      */
135     public function __toString() : string {
136         return implode('\\', $this->parts);
137     }
138
139     /**
140      * Gets a slice of a name (similar to array_slice).
141      *
142      * This method returns a new instance of the same type as the original and with the same
143      * attributes.
144      *
145      * If the slice is empty, null is returned. The null value will be correctly handled in
146      * concatenations using concat().
147      *
148      * Offset and length have the same meaning as in array_slice().
149      *
150      * @param int      $offset Offset to start the slice at (may be negative)
151      * @param int|null $length Length of the slice (may be negative)
152      *
153      * @return static|null Sliced name
154      */
155     public function slice(int $offset, int $length = null) {
156         $numParts = count($this->parts);
157
158         $realOffset = $offset < 0 ? $offset + $numParts : $offset;
159         if ($realOffset < 0 || $realOffset > $numParts) {
160             throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
161         }
162
163         if (null === $length) {
164             $realLength = $numParts - $realOffset;
165         } else {
166             $realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
167             if ($realLength < 0 || $realLength > $numParts) {
168                 throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
169             }
170         }
171
172         if ($realLength === 0) {
173             // Empty slice is represented as null
174             return null;
175         }
176
177         return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes);
178     }
179
180     /**
181      * Concatenate two names, yielding a new Name instance.
182      *
183      * The type of the generated instance depends on which class this method is called on, for
184      * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
185      *
186      * If one of the arguments is null, a new instance of the other name will be returned. If both
187      * arguments are null, null will be returned. As such, writing
188      *     Name::concat($namespace, $shortName)
189      * where $namespace is a Name node or null will work as expected.
190      *
191      * @param string|string[]|self|null $name1      The first name
192      * @param string|string[]|self|null $name2      The second name
193      * @param array                     $attributes Attributes to assign to concatenated name
194      *
195      * @return static|null Concatenated name
196      */
197     public static function concat($name1, $name2, array $attributes = []) {
198         if (null === $name1 && null === $name2) {
199             return null;
200         } elseif (null === $name1) {
201             return new static(self::prepareName($name2), $attributes);
202         } elseif (null === $name2) {
203             return new static(self::prepareName($name1), $attributes);
204         } else {
205             return new static(
206                 array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
207             );
208         }
209     }
210
211     /**
212      * Prepares a (string, array or Name node) name for use in name changing methods by converting
213      * it to an array.
214      *
215      * @param string|string[]|self $name Name to prepare
216      *
217      * @return string[] Prepared name
218      */
219     private static function prepareName($name) : array {
220         if (\is_string($name)) {
221             if ('' === $name) {
222                 throw new \InvalidArgumentException('Name cannot be empty');
223             }
224
225             return explode('\\', $name);
226         } elseif (\is_array($name)) {
227             if (empty($name)) {
228                 throw new \InvalidArgumentException('Name cannot be empty');
229             }
230
231             return $name;
232         } elseif ($name instanceof self) {
233             return $name->parts;
234         }
235
236         throw new \InvalidArgumentException(
237             'Expected string, array of parts or Name instance'
238         );
239     }
240     
241     public function getType() : string {
242         return 'Name';
243     }
244 }