From 2f67429ec93857b8a3ae5a15165818e5eda8fb56 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 21 Jul 2018 21:58:32 +0200 Subject: [PATCH] Add check for leading backslash in Name ctor Fixes #523. --- CHANGELOG.md | 5 +++++ lib/PhpParser/Node/Name.php | 4 ++++ test/PhpParser/Node/NameTest.php | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f2782..fa0ae19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) -------------------------- diff --git a/lib/PhpParser/Node/Name.php b/lib/PhpParser/Node/Name.php index 5d923d0..32ca723 100644 --- a/lib/PhpParser/Node/Name.php +++ b/lib/PhpParser/Node/Name.php @@ -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)) { diff --git a/test/PhpParser/Node/NameTest.php b/test/PhpParser/Node/NameTest.php index 8fe9ed6..a035b79 100644 --- a/test/PhpParser/Node/NameTest.php +++ b/test/PhpParser/Node/NameTest.php @@ -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);