[NodeTypeResolver] add Param type to VariableTypeResolverTest

This commit is contained in:
TomasVotruba 2017-10-23 12:29:33 +02:00
parent d2396be720
commit 67471f4728
3 changed files with 29 additions and 1 deletions

View File

@ -6,6 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverAwareInterface;
use Rector\NodeTypeResolver\Contract\PerNodeTypeResolver\PerNodeTypeResolverInterface;
@ -58,6 +59,17 @@ final class VariableTypeResolver implements PerNodeTypeResolverInterface, NodeTy
return $variableNode->getAttribute(Attribute::CLASS_NAME);
}
if ($parentNode instanceof Param) {
$variableName = $variableNode->name;
$variableType = $this->nodeTypeResolver->resolve($parentNode->type);
if ($variableType) {
$this->typeContext->addVariableWithType($variableName, $variableType);
return $variableType;
}
}
return null;
}

View File

@ -0,0 +1,7 @@
<?php
namespace SomeNamespace;
array_map(function (UseUse $useUse) {
return $useUse;
}, []);

View File

@ -8,7 +8,7 @@ use Rector\NodeTypeResolver\Tests\AbstractNodeTypeResolverTest;
final class VariableTypeResolverTest extends AbstractNodeTypeResolverTest
{
public function test(): void
public function testNewAndAssign(): void
{
$nodes = $this->getNodesWithTypesForFile(__DIR__ . '/Source/SomeClass.php.inc');
$variableNodes = $this->nodeFinder->findInstanceOf($nodes, Variable::class);
@ -17,4 +17,13 @@ final class VariableTypeResolverTest extends AbstractNodeTypeResolverTest
$this->assertSame('SomeNamespace\AnotherType', $variableNodes[1]->getAttribute(Attribute::TYPE));
$this->assertSame('SomeNamespace\AnotherType', $variableNodes[2]->getAttribute(Attribute::TYPE));
}
public function testCallbackArgumentTypehint(): void
{
$nodes = $this->getNodesWithTypesForFile(__DIR__ . '/Source/callbackArgumentTypehint.php.inc');
$variableNodes = $this->nodeFinder->findInstanceOf($nodes, Variable::class);
$this->assertSame('SomeNamespace\UseUse', $variableNodes[0]->getAttribute(Attribute::TYPE));
$this->assertSame('SomeNamespace\UseUse', $variableNodes[1]->getAttribute(Attribute::TYPE));
}
}