[ReflectionDocBlock] make NamespaceAnalyzer work with use aliases

This commit is contained in:
TomasVotruba 2017-10-27 22:40:55 +02:00
parent be26c7ebcd
commit b240eadda5
5 changed files with 55 additions and 24 deletions

View File

@ -1,21 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\NodeTypeResolver\Tests\PerNodeTypeResolver\PropertyTypeResolver;
use PhpParser\Node\Stmt\Property;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Tests\AbstractNodeTypeResolverTest;
final class NestedPropertyTest extends AbstractNodeTypeResolverTest
{
public function testDocBlock(): void
{
$propertyNodes = $this->getNodesForFileOfType(__DIR__ . '/Source/DocBlockDefinedProperty.inc', Property::class);
$this->assertSame(
['SomeNamespace\PropertyType'],
$propertyNodes[0]->getAttribute(Attribute::TYPES)
);
}
}

View File

@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace Rector\NodeTypeResolver\Tests\PerNodeTypeResolver\PropertyFetchTypeResolver;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Tests\AbstractNodeTypeResolverTest;
final class Test extends AbstractNodeTypeResolverTest
{
public function testDocBlock(): void
{
$propertyFetchNodes = $this->getNodesForFileOfType(
__DIR__ . '/Source/NestedProperty.php.inc',
PropertyFetch::class
);
$this->assertCount(4, $propertyFetchNodes);
$this->assertSame('name', $propertyFetchNodes[0]->name->toString());
$this->assertSame(
null, // should be nothing, is string
$propertyFetchNodes[0]->getAttribute(Attribute::TYPES)
);
$this->assertSame('props', $propertyFetchNodes[1]->name->toString());
$this->assertSame(
null, // should be something
$propertyFetchNodes[1]->getAttribute(Attribute::TYPES)
);
$this->assertSame('node', $propertyFetchNodes[2]->name->toString());
$this->assertSame(
['PhpParser\Node\Stmt\Property'],
$propertyFetchNodes[2]->getAttribute(Attribute::TYPES)
);
$this->assertSame('positionInNode', $propertyFetchNodes[3]->name->toString());
$this->assertSame(
null, // should be null
$propertyFetchNodes[3]->getAttribute(Attribute::TYPES)
);
}
}

View File

@ -10,7 +10,10 @@ final class Test extends AbstractNodeTypeResolverTest
{
public function testDocBlock(): void
{
$propertyNodes = $this->getNodesForFileOfType(__DIR__ . '/Source/DocBlockDefinedProperty.php.inc', Property::class);
$propertyNodes = $this->getNodesForFileOfType(
__DIR__ . '/Source/DocBlockDefinedProperty.php.inc',
Property::class
);
$this->assertSame(
['SomeNamespace\PropertyType'],
@ -25,7 +28,6 @@ final class Test extends AbstractNodeTypeResolverTest
Property::class
);
// @todo: add propertyFetch test
$this->assertSame(
['SomeNamespace\PropertyType'],
$propertyNodes[0]->getAttribute(Attribute::TYPES)

View File

@ -38,12 +38,18 @@ final class NamespaceAnalyzer
/** @var Use_[] $useNodes */
$useNodes = (array) $node->getAttribute(Attribute::USE_NODES);
foreach ($useNodes as $useNode) {
$nodeUseName = $useNode->uses[0]->name->toString();
$useUseNode = $useNode->uses[0];
$nodeUseName = $useUseNode->name->toString();
foreach ($types as $type) {
if (Strings::endsWith($nodeUseName, '\\' . $type)) {
return $nodeUseName;
}
// alias
if ($useUseNode->getAlias() && $type === $useUseNode->getAlias()->toString()) {
return $nodeUseName;
}
}
}