1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/lib/PhpParser/Node/Name.php

233 lines
7.2 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
2011-04-18 19:02:30 +02:00
namespace PhpParser\Node;
use PhpParser\NodeAbstract;
class Name extends NodeAbstract
2011-04-18 19:02:30 +02:00
{
2016-10-22 17:05:00 +02:00
/**
* @var string[] Parts of the name
*/
public $parts;
private static $specialClassNames = [
'self' => true,
'parent' => true,
'static' => true,
];
/**
* Constructs a name node.
*
2017-02-02 01:56:15 +01:00
* @param string|string[]|self $name Name as string, part array or Name instance (copy ctor)
* @param array $attributes Additional attributes
*/
public function __construct($name, array $attributes = []) {
2015-05-02 22:17:34 +02:00
parent::__construct($attributes);
$this->parts = self::prepareName($name);
}
2017-04-28 21:40:59 +02:00
public function getSubNodeNames() : array {
return ['parts'];
}
/**
* Gets the first part of the name, i.e. everything before the first namespace separator.
*
* @return string First part of the name
*/
2017-04-28 21:40:59 +02:00
public function getFirst() : string {
return $this->parts[0];
}
/**
* Gets the last part of the name, i.e. everything after the last namespace separator.
*
* @return string Last part of the name
*/
2017-04-28 21:40:59 +02:00
public function getLast() : string {
return $this->parts[count($this->parts) - 1];
}
2011-09-24 18:05:14 +02:00
/**
* Checks whether the name is unqualified. (E.g. Name)
*
* @return bool Whether the name is unqualified
*/
2017-04-28 21:40:59 +02:00
public function isUnqualified() : bool {
return 1 === count($this->parts);
2011-09-24 18:05:14 +02:00
}
/**
* Checks whether the name is qualified. (E.g. Name\Name)
*
* @return bool Whether the name is qualified
*/
2017-04-28 21:40:59 +02:00
public function isQualified() : bool {
2011-09-24 18:05:14 +02:00
return 1 < count($this->parts);
}
/**
* Checks whether the name is fully qualified. (E.g. \Name)
*
* @return bool Whether the name is fully qualified
*/
2017-04-28 21:40:59 +02:00
public function isFullyQualified() : bool {
2011-09-24 18:05:14 +02:00
return false;
}
/**
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
*
* @return bool Whether the name is relative
*/
2017-04-28 21:40:59 +02:00
public function isRelative() : bool {
2011-09-24 18:05:14 +02:00
return false;
}
/**
* Returns a string representation of the name itself, without taking taking the name type into
* account (e.g., not including a leading backslash for fully qualified names).
*
* @return string String representation
*/
2017-04-28 21:40:59 +02:00
public function toString() : string {
return implode('\\', $this->parts);
}
/**
* Returns a string representation of the name as it would occur in code (e.g., including
* leading backslash for fully qualified names.
*
* @return string String representation
*/
2017-04-28 21:40:59 +02:00
public function toCodeString() : string {
return $this->toString();
}
/**
* Returns lowercased string representation of the name, without taking the name type into
* account (e.g., no leading backslash for fully qualified names).
*
* @return string Lowercased string representation
*/
public function toLowerString() : string {
return strtolower(implode('\\', $this->parts));
}
/**
* Checks whether the identifier is a special class name (self, parent or static).
*
* @return bool Whether identifier is a special class name
*/
public function isSpecialClassName() : bool {
return count($this->parts) === 1
&& isset(self::$specialClassNames[strtolower($this->parts[0])]);
}
/**
* Returns a string representation of the name by imploding the namespace parts with the
2011-08-04 16:30:04 +02:00
* namespace separator.
*
* @return string String representation
*/
2017-04-28 21:40:59 +02:00
public function __toString() : string {
return implode('\\', $this->parts);
}
2011-08-04 16:30:04 +02:00
/**
* Gets a slice of a name (similar to array_slice).
*
* This method returns a new instance of the same type as the original and with the same
* attributes.
*
* If the slice is empty, null is returned. The null value will be correctly handled in
* concatenations using concat().
*
* Offset and length have the same meaning as in array_slice().
*
* @param int $offset Offset to start the slice at (may be negative)
* @param int|null $length Length of the slice (may be negative)
*
* @return static|null Sliced name
*/
2017-04-28 21:40:59 +02:00
public function slice(int $offset, int $length = null) {
$numParts = count($this->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) {
throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
}
}
if ($realLength === 0) {
// Empty slice is represented as null
return null;
}
return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes);
}
/**
* Concatenate two names, yielding a new Name instance.
*
* The type of the generated instance depends on which class this method is called on, for
* example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
*
* If one of the arguments is null, a new instance of the other name will be returned. If both
* arguments are null, null will be returned. As such, writing
* Name::concat($namespace, $shortName)
* where $namespace is a Name node or null will work as expected.
*
2017-02-02 01:56:15 +01:00
* @param string|string[]|self|null $name1 The first name
* @param string|string[]|self|null $name2 The second name
* @param array $attributes Attributes to assign to concatenated name
*
* @return static|null Concatenated name
*/
public static function concat($name1, $name2, array $attributes = []) {
if (null === $name1 && null === $name2) {
return null;
} elseif (null === $name1) {
return new static(self::prepareName($name2), $attributes);
} else if (null === $name2) {
return new static(self::prepareName($name1), $attributes);
} else {
return new static(
array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
);
}
2011-08-04 16:30:04 +02:00
}
/**
* Prepares a (string, array or Name node) name for use in name changing methods by converting
* it to an array.
*
2017-02-02 01:56:15 +01:00
* @param string|string[]|self $name Name to prepare
2011-08-04 16:30:04 +02:00
*
2017-02-02 01:56:15 +01:00
* @return string[] Prepared name
2011-08-04 16:30:04 +02:00
*/
2017-04-28 21:40:59 +02:00
private static function prepareName($name) : array {
if (\is_string($name)) {
2011-08-04 16:30:04 +02:00
return explode('\\', $name);
} elseif (\is_array($name)) {
return $name;
2011-08-04 16:30:04 +02:00
} elseif ($name instanceof self) {
return $name->parts;
}
throw new \InvalidArgumentException(
'Expected string, array of parts or Name instance'
);
2011-08-04 16:30:04 +02:00
}
}