mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-25 04:03:55 +01:00
fix RemoveUnusedAliasRector for doc vs class concurency (#2183)
fix RemoveUnusedAliasRector for doc vs class concurency
This commit is contained in:
commit
37ff104bb8
@ -157,6 +157,19 @@ final class PhpDocInfo
|
||||
return $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($paramTagValue, $this->node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Type[]
|
||||
*/
|
||||
public function getParamTypes(): array
|
||||
{
|
||||
$paramTypes = [];
|
||||
foreach ($this->getParamTagValues() as $paramTagValue) {
|
||||
$paramTypes[] = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($paramTagValue, $this->node);
|
||||
}
|
||||
|
||||
return $paramTypes;
|
||||
}
|
||||
|
||||
public function getVarType(): Type
|
||||
{
|
||||
$varTagValue = $this->getVarTagValue();
|
||||
|
@ -18,10 +18,12 @@ use PhpParser\Node\Stmt\TraitUse;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use PhpParser\NodeVisitor\NameResolver;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\CodingStyle\Imports\ShortNameResolver;
|
||||
use Rector\CodingStyle\Naming\ClassNaming;
|
||||
use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PHPStan\Type\AliasedObjectType;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
@ -306,6 +308,27 @@ PHP
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var PhpDocInfo $phpDocInfo */
|
||||
$phpDocInfo = $this->getPhpDocInfo($node);
|
||||
if ($phpDocInfo->getVarType()) {
|
||||
$varType = $phpDocInfo->getVarType();
|
||||
if ($varType instanceof AliasedObjectType) {
|
||||
$possibleDocAliases[] = $varType->getClassName();
|
||||
}
|
||||
|
||||
$returnType = $phpDocInfo->getReturnType();
|
||||
if ($returnType instanceof AliasedObjectType) {
|
||||
$possibleDocAliases[] = $returnType->getClassName();
|
||||
}
|
||||
|
||||
foreach ($phpDocInfo->getParamTypes() as $paramType) {
|
||||
if ($paramType instanceof AliasedObjectType) {
|
||||
$possibleDocAliases[] = $paramType->getClassName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// e.g. "use Dotrine\ORM\Mapping as ORM" etc.
|
||||
$matches = Strings::matchAll($node->getDocComment()->getText(), '#\@(?<possible_alias>\w+)(\\\\)?#s');
|
||||
foreach ($matches as $match) {
|
||||
$possibleDocAliases[] = $match['possible_alias'];
|
||||
@ -336,6 +359,10 @@ PHP
|
||||
$shortNames = $this->shortNameResolver->resolveForNode($use);
|
||||
foreach ($shortNames as $alias => $useImport) {
|
||||
$shortName = $this->classNaming->getShortName($useImport);
|
||||
if ($shortName === $alias) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$useNamesAliasToName[$shortName][] = $alias;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Use_\RemoveUnusedAliasRector\Fixture;
|
||||
|
||||
use SplFileInfo as NativeSplFileInfo;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
|
||||
final class KeepUsedDocParam
|
||||
{
|
||||
/**
|
||||
* @param SplFileInfo $type
|
||||
*/
|
||||
public function run($type): void
|
||||
{
|
||||
$splFileInfo = new NativeSplFileInfo('tests/Posts/Year2018/Exceptions/Source/some_file.txt');
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Use_\RemoveUnusedAliasRector\Fixture;
|
||||
|
||||
use SplFileInfo as NativeSplFileInfo;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
|
||||
final class KeepUsedSplFileInfo
|
||||
{
|
||||
public function testSplFileInfo(): void
|
||||
{
|
||||
$splFileInfo = new NativeSplFileInfo('tests/Posts/Year2018/Exceptions/Source/some_file.txt');
|
||||
|
||||
/** @var SplFileInfo $file */
|
||||
$file = array_pop($files);
|
||||
}
|
||||
}
|
@ -22,6 +22,8 @@ final class RemoveUnusedAliasRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
yield [__DIR__ . '/Fixture/fixture.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/used.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/keep_used_spl_file_info.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/keep_used_doc_param.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/class_name.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/no_namespace.php.inc'];
|
||||
yield [__DIR__ . '/Fixture/no_namespace_class_name.php.inc'];
|
||||
|
Loading…
x
Reference in New Issue
Block a user