| 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: | |
| 11: | |
| 12: | |
| 13: | public $parts; |
| 14: | |
| 15: | private static $specialClassNames = [ |
| 16: | 'self' => true, |
| 17: | 'parent' => true, |
| 18: | 'static' => true, |
| 19: | ]; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | public function __construct($name, array $attributes = []) { |
| 28: | $this->attributes = $attributes; |
| 29: | $this->parts = self::prepareName($name); |
| 30: | } |
| 31: | |
| 32: | public function getSubNodeNames() : array { |
| 33: | return ['parts']; |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function getParts(): array { |
| 42: | return $this->parts; |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | public function getFirst() : string { |
| 51: | return $this->parts[0]; |
| 52: | } |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public function getLast() : string { |
| 60: | return $this->parts[count($this->parts) - 1]; |
| 61: | } |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public function isUnqualified() : bool { |
| 69: | return 1 === count($this->parts); |
| 70: | } |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | public function isQualified() : bool { |
| 78: | return 1 < count($this->parts); |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | public function isFullyQualified() : bool { |
| 87: | return false; |
| 88: | } |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | public function isRelative() : bool { |
| 96: | return false; |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | public function toString() : string { |
| 106: | return implode('\\', $this->parts); |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | public function toCodeString() : string { |
| 116: | return $this->toString(); |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | public function toLowerString() : string { |
| 126: | return strtolower(implode('\\', $this->parts)); |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public function isSpecialClassName() : bool { |
| 135: | return count($this->parts) === 1 |
| 136: | && isset(self::$specialClassNames[strtolower($this->parts[0])]); |
| 137: | } |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | public function __toString() : string { |
| 146: | return implode('\\', $this->parts); |
| 147: | } |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | public function slice(int $offset, ?int $length = null) { |
| 166: | $numParts = count($this->parts); |
| 167: | |
| 168: | $realOffset = $offset < 0 ? $offset + $numParts : $offset; |
| 169: | if ($realOffset < 0 || $realOffset > $numParts) { |
| 170: | throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); |
| 171: | } |
| 172: | |
| 173: | if (null === $length) { |
| 174: | $realLength = $numParts - $realOffset; |
| 175: | } else { |
| 176: | $realLength = $length < 0 ? $length + $numParts - $realOffset : $length; |
| 177: | if ($realLength < 0 || $realLength > $numParts - $realOffset) { |
| 178: | throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length)); |
| 179: | } |
| 180: | } |
| 181: | |
| 182: | if ($realLength === 0) { |
| 183: | |
| 184: | return null; |
| 185: | } |
| 186: | |
| 187: | return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes); |
| 188: | } |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | public static function concat($name1, $name2, array $attributes = []) { |
| 208: | if (null === $name1 && null === $name2) { |
| 209: | return null; |
| 210: | } elseif (null === $name1) { |
| 211: | return new static(self::prepareName($name2), $attributes); |
| 212: | } elseif (null === $name2) { |
| 213: | return new static(self::prepareName($name1), $attributes); |
| 214: | } else { |
| 215: | return new static( |
| 216: | array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes |
| 217: | ); |
| 218: | } |
| 219: | } |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | private static function prepareName($name) : array { |
| 230: | if (\is_string($name)) { |
| 231: | if ('' === $name) { |
| 232: | throw new \InvalidArgumentException('Name cannot be empty'); |
| 233: | } |
| 234: | |
| 235: | return explode('\\', $name); |
| 236: | } elseif (\is_array($name)) { |
| 237: | if (empty($name)) { |
| 238: | throw new \InvalidArgumentException('Name cannot be empty'); |
| 239: | } |
| 240: | |
| 241: | return $name; |
| 242: | } elseif ($name instanceof self) { |
| 243: | return $name->parts; |
| 244: | } |
| 245: | |
| 246: | throw new \InvalidArgumentException( |
| 247: | 'Expected string, array of parts or Name instance' |
| 248: | ); |
| 249: | } |
| 250: | |
| 251: | public function getType() : string { |
| 252: | return 'Name'; |
| 253: | } |
| 254: | } |
| 255: | |