| 1 |
<?php |
| 2 |
// phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards. |
| 3 |
|
| 4 |
namespace WPDeveloper\BetterDocs\Dependencies\PhpParser\Node; |
| 5 |
|
| 6 |
use WPDeveloper\BetterDocs\Dependencies\PhpParser\NodeAbstract; |
| 7 |
|
| 8 |
class Name extends NodeAbstract |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var string[] Parts of the name |
| 12 |
*/ |
| 13 |
public $parts; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructs a name node. |
| 17 |
* |
| 18 |
* @param string|array|self $name Name as string, part array or Name instance (copy ctor) |
| 19 |
* @param array $attributes Additional attributes |
| 20 |
*/ |
| 21 |
public function __construct($name, array $attributes = array()) { |
| 22 |
parent::__construct($attributes); |
| 23 |
$this->parts = self::prepareName($name); |
| 24 |
} |
| 25 |
|
| 26 |
public function getSubNodeNames() { |
| 27 |
return array('parts'); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Gets the first part of the name, i.e. everything before the first namespace separator. |
| 32 |
* |
| 33 |
* @return string First part of the name |
| 34 |
*/ |
| 35 |
public function getFirst() { |
| 36 |
return $this->parts[0]; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Gets the last part of the name, i.e. everything after the last namespace separator. |
| 41 |
* |
| 42 |
* @return string Last part of the name |
| 43 |
*/ |
| 44 |
public function getLast() { |
| 45 |
return $this->parts[count($this->parts) - 1]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Checks whether the name is unqualified. (E.g. Name) |
| 50 |
* |
| 51 |
* @return bool Whether the name is unqualified |
| 52 |
*/ |
| 53 |
public function isUnqualified() { |
| 54 |
return 1 == count($this->parts); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Checks whether the name is qualified. (E.g. Name\Name) |
| 59 |
* |
| 60 |
* @return bool Whether the name is qualified |
| 61 |
*/ |
| 62 |
public function isQualified() { |
| 63 |
return 1 < count($this->parts); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Checks whether the name is fully qualified. (E.g. \Name) |
| 68 |
* |
| 69 |
* @return bool Whether the name is fully qualified |
| 70 |
*/ |
| 71 |
public function isFullyQualified() { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name) |
| 77 |
* |
| 78 |
* @return bool Whether the name is relative |
| 79 |
*/ |
| 80 |
public function isRelative() { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns a string representation of the name by imploding the namespace parts with the |
| 86 |
* namespace separator. |
| 87 |
* |
| 88 |
* @return string String representation |
| 89 |
*/ |
| 90 |
public function toString() { |
| 91 |
return implode('\\', $this->parts); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns a string representation of the name by imploding the namespace parts with the |
| 96 |
* namespace separator. |
| 97 |
* |
| 98 |
* @return string String representation |
| 99 |
*/ |
| 100 |
public function __toString() { |
| 101 |
return implode('\\', $this->parts); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Gets a slice of a name (similar to array_slice). |
| 106 |
* |
| 107 |
* This method returns a new instance of the same type as the original and with the same |
| 108 |
* attributes. |
| 109 |
* |
| 110 |
* If the slice is empty, null is returned. The null value will be correctly handled in |
| 111 |
* concatenations using concat(). |
| 112 |
* |
| 113 |
* Offset and length have the same meaning as in array_slice(). |
| 114 |
* |
| 115 |
* @param int $offset Offset to start the slice at (may be negative) |
| 116 |
* @param int|null $length Length of the slice (may be negative) |
| 117 |
* |
| 118 |
* @return static|null Sliced name |
| 119 |
*/ |
| 120 |
public function slice($offset, $length = null) { |
| 121 |
$numParts = count($this->parts); |
| 122 |
|
| 123 |
$realOffset = $offset < 0 ? $offset + $numParts : $offset; |
| 124 |
if ($realOffset < 0 || $realOffset > $numParts) { |
| 125 |
throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); |
| 126 |
} |
| 127 |
|
| 128 |
if (null === $length) { |
| 129 |
$realLength = $numParts - $realOffset; |
| 130 |
} else { |
| 131 |
$realLength = $length < 0 ? $length + $numParts - $realOffset : $length; |
| 132 |
if ($realLength < 0 || $realLength > $numParts) { |
| 133 |
throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if ($realLength === 0) { |
| 138 |
// Empty slice is represented as null |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Concatenate two names, yielding a new Name instance. |
| 147 |
* |
| 148 |
* The type of the generated instance depends on which class this method is called on, for |
| 149 |
* example Name\FullyQualified::concat() will yield a Name\FullyQualified instance. |
| 150 |
* |
| 151 |
* If one of the arguments is null, a new instance of the other name will be returned. If both |
| 152 |
* arguments are null, null will be returned. As such, writing |
| 153 |
* Name::concat($namespace, $shortName) |
| 154 |
* where $namespace is a Name node or null will work as expected. |
| 155 |
* |
| 156 |
* @param string|array|self|null $name1 The first name |
| 157 |
* @param string|array|self|null $name2 The second name |
| 158 |
* @param array $attributes Attributes to assign to concatenated name |
| 159 |
* |
| 160 |
* @return static|null Concatenated name |
| 161 |
*/ |
| 162 |
public static function concat($name1, $name2, array $attributes = []) { |
| 163 |
if (null === $name1 && null === $name2) { |
| 164 |
return null; |
| 165 |
} elseif (null === $name1) { |
| 166 |
return new static(self::prepareName($name2), $attributes); |
| 167 |
} else if (null === $name2) { |
| 168 |
return new static(self::prepareName($name1), $attributes); |
| 169 |
} else { |
| 170 |
return new static( |
| 171 |
array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Prepares a (string, array or Name node) name for use in name changing methods by converting |
| 178 |
* it to an array. |
| 179 |
* |
| 180 |
* @param string|array|self $name Name to prepare |
| 181 |
* |
| 182 |
* @return array Prepared name |
| 183 |
*/ |
| 184 |
private static function prepareName($name) { |
| 185 |
if (\is_string($name)) { |
| 186 |
return explode('\\', $name); |
| 187 |
} elseif (\is_array($name)) { |
| 188 |
return $name; |
| 189 |
} elseif ($name instanceof self) { |
| 190 |
return $name->parts; |
| 191 |
} |
| 192 |
|
| 193 |
throw new \InvalidArgumentException( |
| 194 |
'Expected string, array of parts or Name instance' |
| 195 |
); |
| 196 |
} |
| 197 |
} |
| 198 |
|