2011-09-28 20:14:27 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\NodeVisitor;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\Node;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
class NameResolverTest extends \PHPUnit_Framework_TestCase
|
2011-09-28 20:14:27 +02:00
|
|
|
{
|
2015-03-21 18:47:20 +01:00
|
|
|
private function canonicalize($string) {
|
|
|
|
return str_replace("\r\n", "\n", $string);
|
|
|
|
}
|
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
/**
|
2014-09-28 12:48:55 +02:00
|
|
|
* @covers PhpParser\NodeVisitor\NameResolver
|
2011-11-27 12:53:48 +01:00
|
|
|
*/
|
2011-11-12 13:24:59 +01:00
|
|
|
public function testResolveNames() {
|
2014-03-26 23:26:31 +01:00
|
|
|
$code = <<<'EOC'
|
2011-09-28 20:14:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Foo {
|
|
|
|
use Hallo as Hi;
|
|
|
|
|
|
|
|
new Bar();
|
|
|
|
new Hi();
|
2014-03-26 23:26:31 +01:00
|
|
|
new Hi\Bar();
|
|
|
|
new \Bar();
|
|
|
|
new namespace\Bar();
|
2011-09-28 20:14:27 +02:00
|
|
|
|
|
|
|
bar();
|
|
|
|
hi();
|
2014-03-26 23:26:31 +01:00
|
|
|
Hi\bar();
|
|
|
|
foo\bar();
|
|
|
|
\bar();
|
|
|
|
namespace\bar();
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
namespace {
|
|
|
|
use Hallo as Hi;
|
|
|
|
|
|
|
|
new Bar();
|
|
|
|
new Hi();
|
2014-03-26 23:26:31 +01:00
|
|
|
new Hi\Bar();
|
|
|
|
new \Bar();
|
|
|
|
new namespace\Bar();
|
2011-09-28 20:14:27 +02:00
|
|
|
|
|
|
|
bar();
|
|
|
|
hi();
|
2014-03-26 23:26:31 +01:00
|
|
|
Hi\bar();
|
|
|
|
foo\bar();
|
|
|
|
\bar();
|
|
|
|
namespace\bar();
|
|
|
|
}
|
|
|
|
namespace Bar {
|
|
|
|
use function foo\bar as baz;
|
|
|
|
use const foo\BAR as BAZ;
|
|
|
|
use foo as bar;
|
|
|
|
|
|
|
|
bar();
|
|
|
|
baz();
|
|
|
|
bar\foo();
|
|
|
|
baz\foo();
|
|
|
|
BAR();
|
|
|
|
BAZ();
|
|
|
|
BAR\FOO();
|
|
|
|
BAZ\FOO();
|
|
|
|
|
|
|
|
bar;
|
|
|
|
baz;
|
|
|
|
bar\foo;
|
|
|
|
baz\foo;
|
|
|
|
BAR;
|
|
|
|
BAZ;
|
|
|
|
BAR\FOO;
|
|
|
|
BAZ\FOO;
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2015-06-12 23:05:28 +02:00
|
|
|
namespace Baz {
|
|
|
|
use A\T\{B\C, D\E};
|
|
|
|
use function X\T\{b\c, d\e};
|
|
|
|
use const Y\T\{B\C, D\E};
|
2015-06-13 11:27:38 +02:00
|
|
|
use Z\T\{G, function f, const K};
|
2015-06-12 23:05:28 +02:00
|
|
|
|
|
|
|
new C;
|
|
|
|
new E;
|
|
|
|
new C\D;
|
|
|
|
new E\F;
|
2015-06-13 11:27:38 +02:00
|
|
|
new G;
|
|
|
|
|
2015-06-12 23:05:28 +02:00
|
|
|
c();
|
|
|
|
e();
|
2015-06-13 11:27:38 +02:00
|
|
|
f();
|
2015-06-12 23:05:28 +02:00
|
|
|
C;
|
|
|
|
E;
|
2015-06-13 11:27:38 +02:00
|
|
|
K;
|
2015-06-12 23:05:28 +02:00
|
|
|
}
|
2011-09-28 20:14:27 +02:00
|
|
|
EOC;
|
2014-03-26 23:26:31 +01:00
|
|
|
$expectedCode = <<<'EOC'
|
2011-09-28 20:14:27 +02:00
|
|
|
namespace Foo {
|
|
|
|
use Hallo as Hi;
|
2014-03-26 23:26:31 +01:00
|
|
|
new \Foo\Bar();
|
|
|
|
new \Hallo();
|
|
|
|
new \Hallo\Bar();
|
|
|
|
new \Bar();
|
|
|
|
new \Foo\Bar();
|
2011-09-28 20:14:27 +02:00
|
|
|
bar();
|
|
|
|
hi();
|
2014-03-26 23:26:31 +01:00
|
|
|
\Hallo\bar();
|
|
|
|
\Foo\foo\bar();
|
|
|
|
\bar();
|
|
|
|
\Foo\bar();
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
namespace {
|
|
|
|
use Hallo as Hi;
|
2014-03-26 23:26:31 +01:00
|
|
|
new \Bar();
|
|
|
|
new \Hallo();
|
|
|
|
new \Hallo\Bar();
|
|
|
|
new \Bar();
|
|
|
|
new \Bar();
|
2016-10-11 19:43:29 +02:00
|
|
|
\bar();
|
|
|
|
\hi();
|
2014-03-26 23:26:31 +01:00
|
|
|
\Hallo\bar();
|
|
|
|
\foo\bar();
|
|
|
|
\bar();
|
|
|
|
\bar();
|
|
|
|
}
|
|
|
|
namespace Bar {
|
|
|
|
use function foo\bar as baz;
|
|
|
|
use const foo\BAR as BAZ;
|
|
|
|
use foo as bar;
|
|
|
|
bar();
|
|
|
|
\foo\bar();
|
|
|
|
\foo\foo();
|
|
|
|
\Bar\baz\foo();
|
|
|
|
BAR();
|
|
|
|
\foo\bar();
|
|
|
|
\foo\FOO();
|
|
|
|
\Bar\BAZ\FOO();
|
|
|
|
bar;
|
|
|
|
baz;
|
|
|
|
\foo\foo;
|
|
|
|
\Bar\baz\foo;
|
|
|
|
BAR;
|
|
|
|
\foo\BAR;
|
|
|
|
\foo\FOO;
|
|
|
|
\Bar\BAZ\FOO;
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2015-06-12 23:05:28 +02:00
|
|
|
namespace Baz {
|
|
|
|
use A\T\{B\C, D\E};
|
|
|
|
use function X\T\{b\c, d\e};
|
|
|
|
use const Y\T\{B\C, D\E};
|
2015-06-13 11:27:38 +02:00
|
|
|
use Z\T\{G, function f, const K};
|
2015-06-12 23:05:28 +02:00
|
|
|
new \A\T\B\C();
|
|
|
|
new \A\T\D\E();
|
|
|
|
new \A\T\B\C\D();
|
|
|
|
new \A\T\D\E\F();
|
2015-06-13 11:27:38 +02:00
|
|
|
new \Z\T\G();
|
2015-06-12 23:05:28 +02:00
|
|
|
\X\T\b\c();
|
|
|
|
\X\T\d\e();
|
2015-06-13 11:27:38 +02:00
|
|
|
\Z\T\f();
|
2015-06-12 23:05:28 +02:00
|
|
|
\Y\T\B\C;
|
|
|
|
\Y\T\D\E;
|
2015-06-13 11:27:38 +02:00
|
|
|
\Z\T\K;
|
2015-06-12 23:05:28 +02:00
|
|
|
}
|
2011-09-28 20:14:27 +02:00
|
|
|
EOC;
|
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative);
|
2014-02-06 14:44:16 +01:00
|
|
|
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
2011-09-28 20:14:27 +02:00
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
$stmts = $parser->parse($code);
|
2011-09-28 20:14:27 +02:00
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
|
2015-03-21 18:47:20 +01:00
|
|
|
$this->assertSame(
|
|
|
|
$this->canonicalize($expectedCode),
|
|
|
|
$prettyPrinter->prettyPrint($stmts)
|
|
|
|
);
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
/**
|
2014-09-28 12:48:55 +02:00
|
|
|
* @covers PhpParser\NodeVisitor\NameResolver
|
2011-11-27 12:53:48 +01:00
|
|
|
*/
|
2011-12-10 12:11:53 +01:00
|
|
|
public function testResolveLocations() {
|
2014-03-26 23:26:31 +01:00
|
|
|
$code = <<<'EOC'
|
2011-11-12 13:24:59 +01:00
|
|
|
<?php
|
2013-01-15 18:21:42 +01:00
|
|
|
namespace NS;
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2014-11-03 16:06:43 +01:00
|
|
|
class A extends B implements C, D {
|
|
|
|
use E, F, G {
|
|
|
|
f as private g;
|
|
|
|
E::h as i;
|
|
|
|
E::j insteadof F, G;
|
|
|
|
}
|
2013-01-15 18:21:42 +01:00
|
|
|
}
|
2011-12-10 12:11:53 +01:00
|
|
|
|
2014-11-03 16:06:43 +01:00
|
|
|
interface A extends C, D {
|
2015-03-12 13:17:31 +01:00
|
|
|
public function a(A $a) : A;
|
2013-01-15 18:21:42 +01:00
|
|
|
}
|
2011-12-10 12:11:53 +01:00
|
|
|
|
2016-12-05 22:21:34 +01:00
|
|
|
function fn(A $a) : A {}
|
|
|
|
function fn2(array $a) : array {}
|
|
|
|
function(A $a) : A {};
|
|
|
|
|
|
|
|
function fn3(?A $a) : ?A {}
|
|
|
|
function fn4(?array $a) : ?array {}
|
2015-03-12 13:17:31 +01:00
|
|
|
|
2013-01-15 18:21:42 +01:00
|
|
|
A::b();
|
2014-03-26 23:26:31 +01:00
|
|
|
A::$b;
|
2013-01-15 18:21:42 +01:00
|
|
|
A::B;
|
|
|
|
new A;
|
2014-03-26 23:26:31 +01:00
|
|
|
$a instanceof A;
|
2012-05-06 17:58:31 +02:00
|
|
|
|
2013-01-15 18:21:42 +01:00
|
|
|
namespace\a();
|
|
|
|
namespace\A;
|
|
|
|
|
|
|
|
try {
|
2014-03-26 23:26:31 +01:00
|
|
|
$someThing;
|
|
|
|
} catch (A $a) {
|
|
|
|
$someThingElse;
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
2011-12-10 12:11:53 +01:00
|
|
|
EOC;
|
2014-03-26 23:26:31 +01:00
|
|
|
$expectedCode = <<<'EOC'
|
2013-01-15 18:21:42 +01:00
|
|
|
namespace NS;
|
|
|
|
|
2014-11-03 16:06:43 +01:00
|
|
|
class A extends \NS\B implements \NS\C, \NS\D
|
2013-01-15 18:21:42 +01:00
|
|
|
{
|
2014-11-03 16:06:43 +01:00
|
|
|
use \NS\E, \NS\F, \NS\G {
|
2014-11-03 16:16:15 +01:00
|
|
|
f as private g;
|
2014-11-03 16:06:43 +01:00
|
|
|
\NS\E::h as i;
|
|
|
|
\NS\E::j insteadof \NS\F, \NS\G;
|
|
|
|
}
|
2013-01-15 18:21:42 +01:00
|
|
|
}
|
2014-11-03 16:06:43 +01:00
|
|
|
interface A extends \NS\C, \NS\D
|
2013-01-15 18:21:42 +01:00
|
|
|
{
|
2015-03-12 13:17:31 +01:00
|
|
|
public function a(\NS\A $a) : \NS\A;
|
|
|
|
}
|
2016-12-05 22:21:34 +01:00
|
|
|
function fn(\NS\A $a) : \NS\A
|
2015-03-12 13:17:31 +01:00
|
|
|
{
|
|
|
|
}
|
2016-12-05 22:21:34 +01:00
|
|
|
function fn2(array $a) : array
|
2015-03-12 13:17:31 +01:00
|
|
|
{
|
2013-01-15 18:21:42 +01:00
|
|
|
}
|
2016-12-05 22:21:34 +01:00
|
|
|
function (\NS\A $a) : \NS\A {
|
2015-03-12 13:17:31 +01:00
|
|
|
};
|
2016-12-06 12:26:21 +01:00
|
|
|
function fn3(?\NS\A $a) : ?\NS\A
|
2016-12-05 22:21:34 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
function fn4(?array $a) : ?array
|
|
|
|
{
|
|
|
|
}
|
2014-03-26 23:26:31 +01:00
|
|
|
\NS\A::b();
|
|
|
|
\NS\A::$b;
|
|
|
|
\NS\A::B;
|
|
|
|
new \NS\A();
|
|
|
|
$a instanceof \NS\A;
|
|
|
|
\NS\a();
|
|
|
|
\NS\A;
|
2013-01-15 18:21:42 +01:00
|
|
|
try {
|
2014-03-26 23:26:31 +01:00
|
|
|
$someThing;
|
|
|
|
} catch (\NS\A $a) {
|
|
|
|
$someThingElse;
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
|
|
|
EOC;
|
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative);
|
2014-02-06 14:44:16 +01:00
|
|
|
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2012-04-25 20:04:46 +02:00
|
|
|
$stmts = $parser->parse($code);
|
2011-11-12 13:24:59 +01:00
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
|
2015-03-21 18:47:20 +01:00
|
|
|
$this->assertSame(
|
|
|
|
$this->canonicalize($expectedCode),
|
|
|
|
$prettyPrinter->prettyPrint($stmts)
|
|
|
|
);
|
2011-12-10 12:11:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoResolveSpecialName() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$stmts = array(new Node\Expr\New_(new Name('self')));
|
2011-12-10 12:11:53 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
2011-12-10 12:11:53 +01:00
|
|
|
|
|
|
|
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:43:29 +02:00
|
|
|
public function testAddDeclarationNamespacedName() {
|
2015-07-14 17:39:56 +02:00
|
|
|
$nsStmts = array(
|
2014-02-06 14:44:16 +01:00
|
|
|
new Stmt\Class_('A'),
|
|
|
|
new Stmt\Interface_('B'),
|
|
|
|
new Stmt\Function_('C'),
|
|
|
|
new Stmt\Const_(array(
|
2015-07-14 17:39:56 +02:00
|
|
|
new Node\Const_('D', new Node\Scalar\LNumber(42))
|
2011-12-10 12:11:53 +01:00
|
|
|
)),
|
2015-07-14 17:39:56 +02:00
|
|
|
new Stmt\Trait_('E'),
|
2015-04-26 23:04:31 +02:00
|
|
|
new Expr\New_(new Stmt\Class_(null)),
|
2015-07-14 17:39:56 +02:00
|
|
|
);
|
2011-12-10 12:11:53 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
2011-12-10 12:11:53 +01:00
|
|
|
|
2015-07-14 17:39:56 +02:00
|
|
|
$stmts = $traverser->traverse([new Stmt\Namespace_(new Name('NS'), $nsStmts)]);
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('NS\\A', (string) $stmts[0]->stmts[0]->namespacedName);
|
|
|
|
$this->assertSame('NS\\B', (string) $stmts[0]->stmts[1]->namespacedName);
|
|
|
|
$this->assertSame('NS\\C', (string) $stmts[0]->stmts[2]->namespacedName);
|
|
|
|
$this->assertSame('NS\\D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName);
|
2015-07-14 17:39:56 +02:00
|
|
|
$this->assertSame('NS\\E', (string) $stmts[0]->stmts[4]->namespacedName);
|
|
|
|
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class);
|
|
|
|
|
|
|
|
$stmts = $traverser->traverse([new Stmt\Namespace_(null, $nsStmts)]);
|
|
|
|
$this->assertSame('A', (string) $stmts[0]->stmts[0]->namespacedName);
|
|
|
|
$this->assertSame('B', (string) $stmts[0]->stmts[1]->namespacedName);
|
|
|
|
$this->assertSame('C', (string) $stmts[0]->stmts[2]->namespacedName);
|
|
|
|
$this->assertSame('D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName);
|
|
|
|
$this->assertSame('E', (string) $stmts[0]->stmts[4]->namespacedName);
|
|
|
|
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class);
|
2011-12-10 12:11:53 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 19:43:29 +02:00
|
|
|
public function testAddRuntimeResolvedNamespacedName() {
|
|
|
|
$stmts = array(
|
|
|
|
new Stmt\Namespace_(new Name('NS'), array(
|
|
|
|
new Expr\FuncCall(new Name('foo')),
|
|
|
|
new Expr\ConstFetch(new Name('FOO')),
|
|
|
|
)),
|
|
|
|
new Stmt\Namespace_(null, array(
|
|
|
|
new Expr\FuncCall(new Name('foo')),
|
|
|
|
new Expr\ConstFetch(new Name('FOO')),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
|
|
|
|
$this->assertSame('NS\\foo', (string) $stmts[0]->stmts[0]->name->getAttribute('namespacedName'));
|
|
|
|
$this->assertSame('NS\\FOO', (string) $stmts[0]->stmts[1]->name->getAttribute('namespacedName'));
|
|
|
|
|
|
|
|
$this->assertFalse($stmts[1]->stmts[0]->name->hasAttribute('namespacedName'));
|
|
|
|
$this->assertFalse($stmts[1]->stmts[1]->name->hasAttribute('namespacedName'));
|
|
|
|
}
|
|
|
|
|
2011-12-10 12:11:53 +01:00
|
|
|
/**
|
2014-08-11 22:04:52 +02:00
|
|
|
* @dataProvider provideTestError
|
2011-12-10 12:11:53 +01:00
|
|
|
*/
|
2014-08-11 22:04:52 +02:00
|
|
|
public function testError(Node $stmt, $errorMsg) {
|
2014-03-26 23:26:31 +01:00
|
|
|
$this->setExpectedException('PhpParser\Error', $errorMsg);
|
2011-12-10 12:11:53 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
2014-08-11 22:04:52 +02:00
|
|
|
$traverser->traverse(array($stmt));
|
2014-03-26 23:26:31 +01:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:04:52 +02:00
|
|
|
public function provideTestError() {
|
2014-03-26 23:26:31 +01:00
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
new Stmt\Use_(array(
|
2015-06-13 11:27:38 +02:00
|
|
|
new Stmt\UseUse(new Name('A\B'), 'B', 0, array('startLine' => 1)),
|
|
|
|
new Stmt\UseUse(new Name('C\D'), 'B', 0, array('startLine' => 2)),
|
2014-03-26 23:26:31 +01:00
|
|
|
), Stmt\Use_::TYPE_NORMAL),
|
|
|
|
'Cannot use C\D as B because the name is already in use on line 2'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new Stmt\Use_(array(
|
2015-06-13 11:27:38 +02:00
|
|
|
new Stmt\UseUse(new Name('a\b'), 'b', 0, array('startLine' => 1)),
|
|
|
|
new Stmt\UseUse(new Name('c\d'), 'B', 0, array('startLine' => 2)),
|
2014-03-26 23:26:31 +01:00
|
|
|
), Stmt\Use_::TYPE_FUNCTION),
|
|
|
|
'Cannot use function c\d as B because the name is already in use on line 2'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
new Stmt\Use_(array(
|
2015-06-13 11:27:38 +02:00
|
|
|
new Stmt\UseUse(new Name('A\B'), 'B', 0, array('startLine' => 1)),
|
|
|
|
new Stmt\UseUse(new Name('C\D'), 'B', 0, array('startLine' => 2)),
|
2014-03-26 23:26:31 +01:00
|
|
|
), Stmt\Use_::TYPE_CONSTANT),
|
|
|
|
'Cannot use const C\D as B because the name is already in use on line 2'
|
|
|
|
),
|
2014-08-11 22:04:52 +02:00
|
|
|
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"
|
|
|
|
),
|
2014-03-26 23:26:31 +01:00
|
|
|
);
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
2013-11-13 11:49:45 +01:00
|
|
|
|
|
|
|
public function testClassNameIsCaseInsensitive()
|
|
|
|
{
|
2014-03-26 23:26:31 +01:00
|
|
|
$source = <<<'EOC'
|
2013-11-13 11:49:45 +01:00
|
|
|
<?php
|
|
|
|
namespace Foo;
|
2014-03-26 23:26:31 +01:00
|
|
|
use Bar\Baz;
|
|
|
|
$test = new baz();
|
2013-11-13 11:49:45 +01:00
|
|
|
EOC;
|
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative);
|
2013-11-13 11:49:45 +01:00
|
|
|
$stmts = $parser->parse($source);
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
2013-11-13 11:49:45 +01:00
|
|
|
|
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
$stmt = $stmts[0];
|
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(array('Bar', 'Baz'), $stmt->stmts[1]->expr->class->parts);
|
2013-11-13 11:49:45 +01:00
|
|
|
}
|
2014-08-11 21:41:54 +02:00
|
|
|
|
|
|
|
public function testSpecialClassNamesAreCaseInsensitive() {
|
|
|
|
$source = <<<'EOC'
|
|
|
|
<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class Bar
|
|
|
|
{
|
|
|
|
public static function method()
|
|
|
|
{
|
|
|
|
SELF::method();
|
|
|
|
PARENT::method();
|
|
|
|
STATIC::method();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOC;
|
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative);
|
2014-08-11 21:41:54 +02:00
|
|
|
$stmts = $parser->parse($source);
|
|
|
|
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver);
|
|
|
|
|
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
$classStmt = $stmts[0];
|
|
|
|
$methodStmt = $classStmt->stmts[0]->stmts[0];
|
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame('SELF', (string)$methodStmt->stmts[0]->class);
|
|
|
|
$this->assertSame('PARENT', (string)$methodStmt->stmts[1]->class);
|
|
|
|
$this->assertSame('STATIC', (string)$methodStmt->stmts[2]->class);
|
2014-08-11 21:41:54 +02:00
|
|
|
}
|
2016-12-17 11:13:43 +01:00
|
|
|
|
|
|
|
public function testAddOriginalNames() {
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver(null, ['preserveOriginalNames' => true]));
|
|
|
|
|
|
|
|
$n1 = new Name('Bar');
|
|
|
|
$n2 = new Name('bar');
|
|
|
|
$origStmts = [
|
|
|
|
new Stmt\Namespace_(new Name('Foo'), [
|
|
|
|
new Expr\ClassConstFetch($n1, 'FOO'),
|
|
|
|
new Expr\FuncCall($n2),
|
|
|
|
])
|
|
|
|
];
|
|
|
|
|
|
|
|
$stmts = $traverser->traverse($origStmts);
|
|
|
|
|
|
|
|
$this->assertSame($n1, $stmts[0]->stmts[0]->class->getAttribute('originalName'));
|
|
|
|
$this->assertSame($n2, $stmts[0]->stmts[1]->name->getAttribute('originalName'));
|
|
|
|
}
|
2016-12-26 21:34:25 +01:00
|
|
|
|
|
|
|
public function testAttributeOnlyMode() {
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
|
|
|
$traverser->addVisitor(new NameResolver(null, ['replaceNodes' => false]));
|
|
|
|
|
|
|
|
$n1 = new Name('Bar');
|
|
|
|
$n2 = new Name('bar');
|
|
|
|
$origStmts = [
|
|
|
|
new Stmt\Namespace_(new Name('Foo'), [
|
|
|
|
new Expr\ClassConstFetch($n1, 'FOO'),
|
|
|
|
new Expr\FuncCall($n2),
|
|
|
|
])
|
|
|
|
];
|
|
|
|
|
|
|
|
$traverser->traverse($origStmts);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
new Name\FullyQualified('Foo\Bar'), $n1->getAttribute('resolvedName'));
|
|
|
|
$this->assertFalse($n2->hasAttribute('resolvedName'));
|
|
|
|
$this->assertEquals(
|
|
|
|
new Name\FullyQualified('Foo\bar'), $n2->getAttribute('namespacedName'));
|
|
|
|
}
|
2013-11-13 11:49:45 +01:00
|
|
|
}
|