[Renaming] Do not rename docblock same name not found in use inside namespace (#2471)

* Another failing test for #7209

* Closes #2468

* rename fixture

* fix

* Fix

* Fix

* fix

* fxi

* final touch: clean up

* final touch: clean up

* re-trigger CI

Co-authored-by: Einar Gangsø <mail@einargangso.no>
This commit is contained in:
Abdul Malik Ikhsan 2022-06-11 18:54:13 +07:00 committed by GitHub
parent 797cb38b88
commit a5340c6f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 15 deletions

View File

@ -6,7 +6,6 @@ namespace Rector\NodeTypeResolver\PhpDocNodeVisitor;
use PhpParser\Node as PhpParserNode;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
@ -25,7 +24,6 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
@ -68,20 +66,11 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
return null;
}
$previousNode = $phpParserNode->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($previousNode instanceof FullyQualified) {
return null;
}
$identifier = clone $node;
$namespacedName = $this->resolveNamespacedName($phpParserNode, $node->name);
$namespacedName = $this->resolveNamespacedName($identifier, $phpParserNode, $node->name);
$identifier->name = $namespacedName;
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($identifier, $phpParserNode);
if ($staticType instanceof AliasedObjectType) {
return null;
}
// make sure to compare FQNs
$objectType = $this->expandShortenedObjectType($staticType);
@ -115,8 +104,11 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
$this->oldToNewTypes = $oldToNewTypes;
}
private function resolveNamespacedName(PhpParserNode $phpParserNode, string $name): string
{
private function resolveNamespacedName(
IdentifierTypeNode $identifierTypeNode,
PhpParserNode $phpParserNode,
string $name
): string {
if (str_starts_with($name, '\\')) {
return $name;
}
@ -125,6 +117,19 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
return $name;
}
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
$identifierTypeNode,
$phpParserNode
);
if (! $staticType instanceof ObjectType) {
return $name;
}
if ($staticType instanceof ShortenedObjectType) {
return $name;
}
$uses = $this->useImportsResolver->resolveForNode($phpParserNode);
$namespace = $this->betterNodeFinder->findParentType($phpParserNode, Namespace_::class);
@ -143,7 +148,13 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
return $namespaceName . '\\' . $name;
}
return $this->resolveNamefromUse($uses, $name);
$nameFromUse = $this->resolveNamefromUse($uses, $name);
if ($nameFromUse !== $name) {
return $nameFromUse;
}
return $namespaceName . '\\' . $nameFromUse;
}
/**

View File

@ -0,0 +1,31 @@
<?php
namespace Foo2;
use Foo\Bar;
class Foo2 extends FooBar
{
/**
* @return Storage|\Storage
*/
public function bar($foo){}
}
?>
-----
<?php
namespace Foo2;
use Foo\Bar;
class Foo2 extends FooBar
{
/**
* @return Storage|\Illuminate\Support\Facades\Storage
*/
public function bar($foo){}
}
?>