2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2011-04-18 19:02:30 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node;
|
|
|
|
|
|
|
|
use PhpParser\NodeAbstract;
|
|
|
|
|
|
|
|
class Name extends NodeAbstract
|
2011-04-18 19:02:30 +02:00
|
|
|
{
|
2019-05-09 14:17:28 +02:00
|
|
|
/** @var string[] Parts of the name */
|
2015-02-28 18:44:28 +01:00
|
|
|
public $parts;
|
|
|
|
|
2017-08-15 22:56:53 +02:00
|
|
|
private static $specialClassNames = [
|
|
|
|
'self' => true,
|
|
|
|
'parent' => true,
|
|
|
|
'static' => true,
|
|
|
|
];
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2011-08-09 14:55:45 +02:00
|
|
|
*/
|
2017-08-13 14:06:08 +02:00
|
|
|
public function __construct($name, array $attributes = []) {
|
2019-05-12 14:55:21 +02:00
|
|
|
$this->attributes = $attributes;
|
2016-10-22 16:41:58 +02:00
|
|
|
$this->parts = self::prepareName($name);
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getSubNodeNames() : array {
|
2017-08-13 14:06:08 +02:00
|
|
|
return ['parts'];
|
2011-08-09 14:55:45 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 13:23:35 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2011-08-04 13:23:35 +02:00
|
|
|
return $this->parts[0];
|
|
|
|
}
|
|
|
|
|
2011-08-04 12:02:14 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2011-08-04 12:02:14 +02:00
|
|
|
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 {
|
2017-08-13 14:13:51 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-04 12:02:14 +02:00
|
|
|
/**
|
2020-08-23 00:35:58 +02:00
|
|
|
* Returns a string representation of the name itself, without taking the name type into
|
2017-04-28 17:10:30 +02:00
|
|
|
* account (e.g., not including a leading backslash for fully qualified names).
|
2011-08-04 12:02:14 +02:00
|
|
|
*
|
|
|
|
* @return string String representation
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function toString() : string {
|
2016-10-09 12:45:15 +02:00
|
|
|
return implode('\\', $this->parts);
|
2011-08-04 12:02:14 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 17:10:30 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2017-04-28 17:10:30 +02:00
|
|
|
return $this->toString();
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:48:24 +02:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:56:53 +02:00
|
|
|
/**
|
|
|
|
* 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])]);
|
|
|
|
}
|
|
|
|
|
2011-08-04 12:02:14 +02:00
|
|
|
/**
|
|
|
|
* Returns a string representation of the name by imploding the namespace parts with the
|
2011-08-04 16:30:04 +02:00
|
|
|
* namespace separator.
|
2011-08-04 12:02:14 +02:00
|
|
|
*
|
|
|
|
* @return string String representation
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function __toString() : string {
|
2011-09-24 16:53:40 +02:00
|
|
|
return implode('\\', $this->parts);
|
2011-08-04 12:02:14 +02:00
|
|
|
}
|
2011-08-04 16:30:04 +02:00
|
|
|
|
2015-06-12 23:05:28 +02:00
|
|
|
/**
|
2015-07-12 23:55:57 +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.
|
2015-06-12 23:05:28 +02:00
|
|
|
*
|
2016-10-22 17:02:38 +02:00
|
|
|
* If the slice is empty, null is returned. The null value will be correctly handled in
|
|
|
|
* concatenations using concat().
|
2015-07-12 23:55:57 +02:00
|
|
|
*
|
2016-07-25 14:47:24 +02:00
|
|
|
* 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)
|
2015-07-12 23:55:57 +02:00
|
|
|
*
|
2016-10-22 17:02:38 +02:00
|
|
|
* @return static|null Sliced name
|
2015-06-12 23:05:28 +02:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function slice(int $offset, int $length = null) {
|
2016-07-25 14:47:24 +02:00
|
|
|
$numParts = count($this->parts);
|
|
|
|
|
|
|
|
$realOffset = $offset < 0 ? $offset + $numParts : $offset;
|
|
|
|
if ($realOffset < 0 || $realOffset > $numParts) {
|
2015-07-12 23:55:57 +02:00
|
|
|
throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
|
|
|
|
}
|
|
|
|
|
2016-07-25 14:47:24 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 17:02:38 +02:00
|
|
|
if ($realLength === 0) {
|
|
|
|
// Empty slice is represented as null
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-25 14:47:24 +02:00
|
|
|
return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes);
|
2015-07-12 23:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-10-22 17:02:38 +02:00
|
|
|
* 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
|
2015-07-12 23:55:57 +02:00
|
|
|
*
|
2016-10-22 17:02:38 +02:00
|
|
|
* @return static|null Concatenated name
|
2015-07-12 23:55:57 +02:00
|
|
|
*/
|
|
|
|
public static function concat($name1, $name2, array $attributes = []) {
|
2016-10-22 17:02:38 +02:00
|
|
|
if (null === $name1 && null === $name2) {
|
|
|
|
return null;
|
|
|
|
} elseif (null === $name1) {
|
|
|
|
return new static(self::prepareName($name2), $attributes);
|
2018-01-13 16:03:36 +01:00
|
|
|
} elseif (null === $name2) {
|
2016-10-22 17:02:38 +02:00
|
|
|
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 {
|
2016-10-22 16:41:58 +02:00
|
|
|
if (\is_string($name)) {
|
2017-12-01 18:13:55 +01:00
|
|
|
if ('' === $name) {
|
|
|
|
throw new \InvalidArgumentException('Name cannot be empty');
|
|
|
|
}
|
|
|
|
|
2011-08-04 16:30:04 +02:00
|
|
|
return explode('\\', $name);
|
2016-10-22 16:41:58 +02:00
|
|
|
} elseif (\is_array($name)) {
|
2017-12-01 18:13:55 +01:00
|
|
|
if (empty($name)) {
|
|
|
|
throw new \InvalidArgumentException('Name cannot be empty');
|
|
|
|
}
|
|
|
|
|
2011-09-28 18:11:28 +02:00
|
|
|
return $name;
|
2011-08-04 16:30:04 +02:00
|
|
|
} elseif ($name instanceof self) {
|
|
|
|
return $name->parts;
|
|
|
|
}
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \InvalidArgumentException(
|
2016-10-22 16:41:58 +02:00
|
|
|
'Expected string, array of parts or Name instance'
|
2011-09-28 18:11:28 +02:00
|
|
|
);
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
2019-05-09 14:17:28 +02:00
|
|
|
|
2018-01-10 18:57:48 +01:00
|
|
|
public function getType() : string {
|
2017-11-12 21:25:57 +01:00
|
|
|
return 'Name';
|
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|