123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php declare(strict_types=1);
- namespace PhpParser\Node;
- use PhpParser\NodeAbstract;
- class Name extends NodeAbstract {
-
- public string $name;
-
- private static array $specialClassNames = [
- 'self' => true,
- 'parent' => true,
- 'static' => true,
- ];
-
- final public function __construct($name, array $attributes = []) {
- $this->attributes = $attributes;
- $this->name = self::prepareName($name);
- }
- public function getSubNodeNames(): array {
- return ['name'];
- }
-
- public function getParts(): array {
- return \explode('\\', $this->name);
- }
-
- public function getFirst(): string {
- if (false !== $pos = \strpos($this->name, '\\')) {
- return \substr($this->name, 0, $pos);
- }
- return $this->name;
- }
-
- public function getLast(): string {
- if (false !== $pos = \strrpos($this->name, '\\')) {
- return \substr($this->name, $pos + 1);
- }
- return $this->name;
- }
-
- public function isUnqualified(): bool {
- return false === \strpos($this->name, '\\');
- }
-
- public function isQualified(): bool {
- return false !== \strpos($this->name, '\\');
- }
-
- public function isFullyQualified(): bool {
- return false;
- }
-
- public function isRelative(): bool {
- return false;
- }
-
- public function toString(): string {
- return $this->name;
- }
-
- public function toCodeString(): string {
- return $this->toString();
- }
-
- public function toLowerString(): string {
- return strtolower($this->name);
- }
-
- public function isSpecialClassName(): bool {
- return isset(self::$specialClassNames[strtolower($this->name)]);
- }
-
- public function __toString(): string {
- return $this->name;
- }
-
- public function slice(int $offset, ?int $length = null) {
- if ($offset === 1 && $length === null) {
-
- if (false !== $pos = \strpos($this->name, '\\')) {
- return new static(\substr($this->name, $pos + 1));
- }
- return null;
- }
- $parts = \explode('\\', $this->name);
- $numParts = \count($parts);
- $realOffset = $offset < 0 ? $offset + $numParts : $offset;
- if ($realOffset < 0 || $realOffset > $numParts) {
- throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
- }
- if (null === $length) {
- $realLength = $numParts - $realOffset;
- } else {
- $realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
- if ($realLength < 0 || $realLength > $numParts - $realOffset) {
- throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
- }
- }
- if ($realLength === 0) {
-
- return null;
- }
- return new static(array_slice($parts, $realOffset, $realLength), $this->attributes);
- }
-
- public static function concat($name1, $name2, array $attributes = []) {
- if (null === $name1 && null === $name2) {
- return null;
- }
- if (null === $name1) {
- return new static($name2, $attributes);
- }
- if (null === $name2) {
- return new static($name1, $attributes);
- } else {
- return new static(
- self::prepareName($name1) . '\\' . self::prepareName($name2), $attributes
- );
- }
- }
-
- private static function prepareName($name): string {
- if (\is_string($name)) {
- if ('' === $name) {
- throw new \InvalidArgumentException('Name cannot be empty');
- }
- return $name;
- }
- if (\is_array($name)) {
- if (empty($name)) {
- throw new \InvalidArgumentException('Name cannot be empty');
- }
- return implode('\\', $name);
- }
- if ($name instanceof self) {
- return $name->name;
- }
- throw new \InvalidArgumentException(
- 'Expected string, array of parts or Name instance'
- );
- }
- public function getType(): string {
- return 'Name';
- }
- }
|