Laravel version update
Laravel version update
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
<?php
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace PhpParser\NodeVisitor;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Expr;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class NameResolverTest extends \PHPUnit_Framework_TestCase
|
||||
class NameResolverTest extends TestCase
|
||||
{
|
||||
private function canonicalize($string) {
|
||||
return str_replace("\r\n", "\n", $string);
|
||||
@@ -118,8 +119,8 @@ namespace {
|
||||
new \Hallo\Bar();
|
||||
new \Bar();
|
||||
new \Bar();
|
||||
bar();
|
||||
hi();
|
||||
\bar();
|
||||
\hi();
|
||||
\Hallo\bar();
|
||||
\foo\bar();
|
||||
\bar();
|
||||
@@ -199,9 +200,12 @@ interface A extends C, D {
|
||||
public function a(A $a) : A;
|
||||
}
|
||||
|
||||
function fn() : A {}
|
||||
function fn2() : array {}
|
||||
function() : A {};
|
||||
function fn(A $a) : A {}
|
||||
function fn2(array $a) : array {}
|
||||
function(A $a) : A {};
|
||||
|
||||
function fn3(?A $a) : ?A {}
|
||||
function fn4(?array $a) : ?array {}
|
||||
|
||||
A::b();
|
||||
A::$b;
|
||||
@@ -233,14 +237,20 @@ interface A extends \NS\C, \NS\D
|
||||
{
|
||||
public function a(\NS\A $a) : \NS\A;
|
||||
}
|
||||
function fn() : \NS\A
|
||||
function fn(\NS\A $a) : \NS\A
|
||||
{
|
||||
}
|
||||
function fn2() : array
|
||||
function fn2(array $a) : array
|
||||
{
|
||||
}
|
||||
function () : \NS\A {
|
||||
function (\NS\A $a) : \NS\A {
|
||||
};
|
||||
function fn3(?\NS\A $a) : ?\NS\A
|
||||
{
|
||||
}
|
||||
function fn4(?array $a) : ?array
|
||||
{
|
||||
}
|
||||
\NS\A::b();
|
||||
\NS\A::$b;
|
||||
\NS\A::B;
|
||||
@@ -270,7 +280,7 @@ EOC;
|
||||
}
|
||||
|
||||
public function testNoResolveSpecialName() {
|
||||
$stmts = array(new Node\Expr\New_(new Name('self')));
|
||||
$stmts = [new Node\Expr\New_(new Name('self'))];
|
||||
|
||||
$traverser = new PhpParser\NodeTraverser;
|
||||
$traverser->addVisitor(new NameResolver);
|
||||
@@ -278,17 +288,17 @@ EOC;
|
||||
$this->assertEquals($stmts, $traverser->traverse($stmts));
|
||||
}
|
||||
|
||||
public function testAddNamespacedName() {
|
||||
$nsStmts = array(
|
||||
public function testAddDeclarationNamespacedName() {
|
||||
$nsStmts = [
|
||||
new Stmt\Class_('A'),
|
||||
new Stmt\Interface_('B'),
|
||||
new Stmt\Function_('C'),
|
||||
new Stmt\Const_(array(
|
||||
new Stmt\Const_([
|
||||
new Node\Const_('D', new Node\Scalar\LNumber(42))
|
||||
)),
|
||||
]),
|
||||
new Stmt\Trait_('E'),
|
||||
new Expr\New_(new Stmt\Class_(null)),
|
||||
);
|
||||
];
|
||||
|
||||
$traverser = new PhpParser\NodeTraverser;
|
||||
$traverser->addVisitor(new NameResolver);
|
||||
@@ -310,57 +320,81 @@ EOC;
|
||||
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class);
|
||||
}
|
||||
|
||||
public function testAddRuntimeResolvedNamespacedName() {
|
||||
$stmts = [
|
||||
new Stmt\Namespace_(new Name('NS'), [
|
||||
new Expr\FuncCall(new Name('foo')),
|
||||
new Expr\ConstFetch(new Name('FOO')),
|
||||
]),
|
||||
new Stmt\Namespace_(null, [
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTestError
|
||||
*/
|
||||
public function testError(Node $stmt, $errorMsg) {
|
||||
$this->setExpectedException('PhpParser\Error', $errorMsg);
|
||||
$this->expectException(\PhpParser\Error::class);
|
||||
$this->expectExceptionMessage($errorMsg);
|
||||
|
||||
$traverser = new PhpParser\NodeTraverser;
|
||||
$traverser->addVisitor(new NameResolver);
|
||||
$traverser->traverse(array($stmt));
|
||||
$traverser->traverse([$stmt]);
|
||||
}
|
||||
|
||||
public function provideTestError() {
|
||||
return array(
|
||||
array(
|
||||
new Stmt\Use_(array(
|
||||
new Stmt\UseUse(new Name('A\B'), 'B', 0, array('startLine' => 1)),
|
||||
new Stmt\UseUse(new Name('C\D'), 'B', 0, array('startLine' => 2)),
|
||||
), Stmt\Use_::TYPE_NORMAL),
|
||||
return [
|
||||
[
|
||||
new Stmt\Use_([
|
||||
new Stmt\UseUse(new Name('A\B'), 'B', 0, ['startLine' => 1]),
|
||||
new Stmt\UseUse(new Name('C\D'), 'B', 0, ['startLine' => 2]),
|
||||
], 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(
|
||||
new Stmt\UseUse(new Name('a\b'), 'b', 0, array('startLine' => 1)),
|
||||
new Stmt\UseUse(new Name('c\d'), 'B', 0, array('startLine' => 2)),
|
||||
), Stmt\Use_::TYPE_FUNCTION),
|
||||
],
|
||||
[
|
||||
new Stmt\Use_([
|
||||
new Stmt\UseUse(new Name('a\b'), 'b', 0, ['startLine' => 1]),
|
||||
new Stmt\UseUse(new Name('c\d'), 'B', 0, ['startLine' => 2]),
|
||||
], 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(
|
||||
new Stmt\UseUse(new Name('A\B'), 'B', 0, array('startLine' => 1)),
|
||||
new Stmt\UseUse(new Name('C\D'), 'B', 0, array('startLine' => 2)),
|
||||
), Stmt\Use_::TYPE_CONSTANT),
|
||||
],
|
||||
[
|
||||
new Stmt\Use_([
|
||||
new Stmt\UseUse(new Name('A\B'), 'B', 0, ['startLine' => 1]),
|
||||
new Stmt\UseUse(new Name('C\D'), 'B', 0, ['startLine' => 2]),
|
||||
], 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))),
|
||||
],
|
||||
[
|
||||
new Expr\New_(new Name\FullyQualified('self', ['startLine' => 3])),
|
||||
"'\\self' is an invalid class name on line 3"
|
||||
),
|
||||
array(
|
||||
new Expr\New_(new Name\Relative('self', array('startLine' => 3))),
|
||||
],
|
||||
[
|
||||
new Expr\New_(new Name\Relative('self', ['startLine' => 3])),
|
||||
"'\\self' is an invalid class name on line 3"
|
||||
),
|
||||
array(
|
||||
new Expr\New_(new Name\FullyQualified('PARENT', array('startLine' => 3))),
|
||||
],
|
||||
[
|
||||
new Expr\New_(new Name\FullyQualified('PARENT', ['startLine' => 3])),
|
||||
"'\\PARENT' is an invalid class name on line 3"
|
||||
),
|
||||
array(
|
||||
new Expr\New_(new Name\Relative('STATIC', array('startLine' => 3))),
|
||||
],
|
||||
[
|
||||
new Expr\New_(new Name\Relative('STATIC', ['startLine' => 3])),
|
||||
"'\\STATIC' is an invalid class name on line 3"
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testClassNameIsCaseInsensitive()
|
||||
@@ -381,7 +415,8 @@ EOC;
|
||||
$stmts = $traverser->traverse($stmts);
|
||||
$stmt = $stmts[0];
|
||||
|
||||
$this->assertSame(array('Bar', 'Baz'), $stmt->stmts[1]->expr->class->parts);
|
||||
$assign = $stmt->stmts[1]->expr;
|
||||
$this->assertSame(['Bar', 'Baz'], $assign->expr->class->parts);
|
||||
}
|
||||
|
||||
public function testSpecialClassNamesAreCaseInsensitive() {
|
||||
@@ -410,8 +445,49 @@ EOC;
|
||||
$classStmt = $stmts[0];
|
||||
$methodStmt = $classStmt->stmts[0]->stmts[0];
|
||||
|
||||
$this->assertSame('SELF', (string)$methodStmt->stmts[0]->class);
|
||||
$this->assertSame('PARENT', (string)$methodStmt->stmts[1]->class);
|
||||
$this->assertSame('STATIC', (string)$methodStmt->stmts[2]->class);
|
||||
$this->assertSame('SELF', (string) $methodStmt->stmts[0]->expr->class);
|
||||
$this->assertSame('PARENT', (string) $methodStmt->stmts[1]->expr->class);
|
||||
$this->assertSame('STATIC', (string) $methodStmt->stmts[2]->expr->class);
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user