From 6d0589d14f540f7e6be0a7f91b2d73f45847cb71 Mon Sep 17 00:00:00 2001 From: nikic Date: Mon, 11 Aug 2014 22:04:52 +0200 Subject: [PATCH] Ensure that special class names are unqualified Replicates the PHP error message --- lib/PhpParser/NodeVisitor/NameResolver.php | 7 ++++++ .../NodeVisitor/NameResolverTest.php | 25 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/PhpParser/NodeVisitor/NameResolver.php b/lib/PhpParser/NodeVisitor/NameResolver.php index 2850fef..1337b76 100644 --- a/lib/PhpParser/NodeVisitor/NameResolver.php +++ b/lib/PhpParser/NodeVisitor/NameResolver.php @@ -123,6 +123,13 @@ class NameResolver extends NodeVisitorAbstract protected function resolveClassName(Name $name) { // don't resolve special class names if (in_array(strtolower($name), array('self', 'parent', 'static'))) { + if (!$name->isUnqualified()) { + throw new Error( + sprintf("'\\%s' is an invalid class name", $name->toString()), + $name->getLine() + ); + } + return $name; } diff --git a/test/PhpParser/NodeVisitor/NameResolverTest.php b/test/PhpParser/NodeVisitor/NameResolverTest.php index b833591..14a99c0 100644 --- a/test/PhpParser/NodeVisitor/NameResolverTest.php +++ b/test/PhpParser/NodeVisitor/NameResolverTest.php @@ -6,6 +6,7 @@ use PhpParser; use PhpParser\Node; use PhpParser\Node\Name; use PhpParser\Node\Stmt; +use PhpParser\Node\Expr; class NameResolverTest extends \PHPUnit_Framework_TestCase { @@ -258,17 +259,17 @@ EOC; } /** - * @dataProvider provideTestAlreadyInUseError + * @dataProvider provideTestError */ - public function testAlreadyInUseError(Stmt\Use_ $use, $errorMsg) { + public function testError(Node $stmt, $errorMsg) { $this->setExpectedException('PhpParser\Error', $errorMsg); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); - $traverser->traverse(array($use)); + $traverser->traverse(array($stmt)); } - public function provideTestAlreadyInUseError() { + public function provideTestError() { return array( array( new Stmt\Use_(array( @@ -291,6 +292,22 @@ EOC; ), Stmt\Use_::TYPE_CONSTANT), 'Cannot use const C\D as B because the name is already in use on line 2' ), + array( + new Expr\New_(new Name\FullyQualified('self', array('startLine' => 3))), + "'\\self' is an invalid class name on line 3" + ), + array( + new Expr\New_(new Name\Relative('self', array('startLine' => 3))), + "'\\self' is an invalid class name on line 3" + ), + array( + new Expr\New_(new Name\FullyQualified('PARENT', array('startLine' => 3))), + "'\\PARENT' is an invalid class name on line 3" + ), + array( + new Expr\New_(new Name\Relative('STATIC', array('startLine' => 3))), + "'\\STATIC' is an invalid class name on line 3" + ), ); }