-<?php
+<?php declare(strict_types=1);
namespace PhpParser\Node;
*/
public $parts;
+ private static $specialClassNames = [
+ 'self' => true,
+ 'parent' => true,
+ 'static' => true,
+ ];
+
/**
* Constructs a name node.
*
- * @param string|array|self $name Name as string, part array or Name instance (copy ctor)
- * @param array $attributes Additional attributes
+ * @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 = array()) {
+ public function __construct($name, array $attributes = []) {
parent::__construct($attributes);
$this->parts = self::prepareName($name);
}
- public function getSubNodeNames() {
- return array('parts');
+ public function getSubNodeNames() : array {
+ return ['parts'];
}
/**
*
* @return string First part of the name
*/
- public function getFirst() {
+ public function getFirst() : string {
return $this->parts[0];
}
*
* @return string Last part of the name
*/
- public function getLast() {
+ public function getLast() : string {
return $this->parts[count($this->parts) - 1];
}
*
* @return bool Whether the name is unqualified
*/
- public function isUnqualified() {
- return 1 == count($this->parts);
+ public function isUnqualified() : bool {
+ return 1 === count($this->parts);
}
/**
*
* @return bool Whether the name is qualified
*/
- public function isQualified() {
+ public function isQualified() : bool {
return 1 < count($this->parts);
}
*
* @return bool Whether the name is fully qualified
*/
- public function isFullyQualified() {
+ public function isFullyQualified() : bool {
return false;
}
*
* @return bool Whether the name is relative
*/
- public function isRelative() {
+ public function isRelative() : bool {
return false;
}
/**
- * Returns a string representation of the name by imploding the namespace parts with the
- * namespace separator.
+ * 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
*/
- public function toString() {
+ 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
+ */
+ 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
* namespace separator.
*
* @return string String representation
*/
- public function __toString() {
+ public function __toString() : string {
return implode('\\', $this->parts);
}
*
* @return static|null Sliced name
*/
- public function slice($offset, $length = null) {
+ public function slice(int $offset, int $length = null) {
$numParts = count($this->parts);
$realOffset = $offset < 0 ? $offset + $numParts : $offset;
* Name::concat($namespace, $shortName)
* where $namespace is a Name node or null will work as expected.
*
- * @param string|array|self|null $name1 The first name
- * @param string|array|self|null $name2 The second name
- * @param array $attributes Attributes to assign to concatenated name
+ * @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
*/
return null;
} elseif (null === $name1) {
return new static(self::prepareName($name2), $attributes);
- } else if (null === $name2) {
+ } elseif (null === $name2) {
return new static(self::prepareName($name1), $attributes);
} else {
return new static(
* Prepares a (string, array or Name node) name for use in name changing methods by converting
* it to an array.
*
- * @param string|array|self $name Name to prepare
+ * @param string|string[]|self $name Name to prepare
*
- * @return array Prepared name
+ * @return string[] Prepared name
*/
- private static function prepareName($name) {
+ private static function prepareName($name) : array {
if (\is_string($name)) {
+ if ('' === $name) {
+ throw new \InvalidArgumentException('Name cannot be empty');
+ }
+
return explode('\\', $name);
} elseif (\is_array($name)) {
+ if (empty($name)) {
+ throw new \InvalidArgumentException('Name cannot be empty');
+ }
+
return $name;
} elseif ($name instanceof self) {
return $name->parts;
'Expected string, array of parts or Name instance'
);
}
+
+ public function getType() : string {
+ return 'Name';
+ }
}