mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2025-01-22 05:11:39 +01:00
Ensure that special class names are unqualified
Replicates the PHP error message
This commit is contained in:
parent
ef121e690c
commit
6d0589d14f
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user