Add check for leading backslash in Name ctor

Fixes #523.
This commit is contained in:
Nikita Popov 2018-07-21 21:58:32 +02:00
parent 0cd7207ca6
commit 2f67429ec9
3 changed files with 17 additions and 0 deletions

View File

@ -8,6 +8,11 @@ Version 4.0.4-dev
* `useConst()`
* `var()`
* `propertyFetch()`
### Changed
* Passing a string with a leading backslash to the `Name` constructor will now throw an exception.
Most likely a use of `Name\FullyQualified` was intended.
Version 4.0.3 (2018-07-15)
--------------------------

View File

@ -222,6 +222,10 @@ class Name extends NodeAbstract
throw new \InvalidArgumentException('Name cannot be empty');
}
if ('\\' === $name[0]) {
throw new \InvalidArgumentException('Name cannot start with backslash. Did you mean to use Name\FullyQualified?');
}
return explode('\\', $name);
} elseif (\is_array($name)) {
if (empty($name)) {

View File

@ -155,6 +155,14 @@ class NameTest extends TestCase
new Name([]);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Name cannot start with backslash. Did you mean to use Name\FullyQualified?
*/
public function testInvalidLeadingBackslash() {
new Name('\Foo');
}
/** @dataProvider provideTestIsSpecialClassName */
public function testIsSpecialClassName($name, $expected) {
$name = new Name($name);