1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Explicitly support Name copy construction

It already worked beforehand by accident ... make clear it's
actually supported.
This commit is contained in:
Nikita Popov 2016-10-22 16:41:58 +02:00
parent 7672b974ff
commit 91cb82d3d2
3 changed files with 14 additions and 15 deletions

View File

@ -12,16 +12,12 @@ class Name extends NodeAbstract
/**
* Constructs a name node.
*
* @param string|array $parts Parts of the name (or name as string)
* @param string|array|self $name Name as string, part array or Name instance (copy ctor)
* @param array $attributes Additional attributes
*/
public function __construct($parts, array $attributes = array()) {
if (!is_array($parts)) {
$parts = explode('\\', $parts);
}
public function __construct($name, array $attributes = array()) {
parent::__construct($attributes);
$this->parts = $parts;
$this->parts = self::prepareName($name);
}
public function getSubNodeNames() {
@ -165,16 +161,16 @@ class Name extends NodeAbstract
* @return array Prepared name
*/
private static function prepareName($name) {
if (is_string($name)) {
if (\is_string($name)) {
return explode('\\', $name);
} elseif (is_array($name)) {
} elseif (\is_array($name)) {
return $name;
} elseif ($name instanceof self) {
return $name->parts;
}
throw new \InvalidArgumentException(
'When changing a name you need to pass either a string, an array or a Name node'
'Expected string, array of parts or Name instance'
);
}
}

View File

@ -200,7 +200,7 @@ class NameResolver extends NodeVisitorAbstract
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
}
return new FullyQualified($name->parts, $name->getAttributes());
return new FullyQualified($name, $name->getAttributes());
}
protected function resolveOtherName(Name $name, $type) {
@ -229,7 +229,7 @@ class NameResolver extends NodeVisitorAbstract
if (null === $this->namespace) {
// outside of a namespace unaliased unqualified is same as fully qualified
return new FullyQualified($name->parts, $name->getAttributes());
return new FullyQualified($name, $name->getAttributes());
}
// unqualified names inside a namespace cannot be resolved at compile-time
@ -244,7 +244,7 @@ class NameResolver extends NodeVisitorAbstract
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
}
return new FullyQualified($name->parts, $name->getAttributes());
return new FullyQualified($name, $name->getAttributes());
}
protected function addNamespacedName(Node $node) {

View File

@ -10,6 +10,9 @@ class NameTest extends \PHPUnit_Framework_TestCase
$name = new Name('foo\bar');
$this->assertSame(array('foo', 'bar'), $name->parts);
$name = new Name($name);
$this->assertSame(array('foo', 'bar'), $name->parts);
}
public function testGet() {
@ -122,7 +125,7 @@ class NameTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage When changing a name you need to pass either a string, an array or a Name node
* @expectedExceptionMessage Expected string, array of parts or Name instance
*/
public function testInvalidArg() {
Name::concat('foo', new \stdClass);