Skip to content

Commit 306b852

Browse files
authored
[DeadCode] Skip PHPStan type alias on RemoveReturnTagIncompatibleWithNativeTypeRector (#8179)
1 parent 0e269a0 commit 306b852

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveReturnTagIncompatibleWithNativeTypeRector\Fixture;
4+
5+
/**
6+
* @phpstan-type Config array{
7+
* key: string,
8+
* hash: string,
9+
* }
10+
*/
11+
final class SkipPHPStanTypeAlias
12+
{
13+
/**
14+
* @return Config
15+
*/
16+
public function run(bool $param): array
17+
{
18+
return [
19+
'key' => 'my-key',
20+
'hash' => 'my-hash',
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveReturnTagIncompatibleWithNativeTypeRector\Fixture;
4+
5+
/**
6+
* @phpstan-type Config array{key: string}
7+
*/
8+
final class SkipPHPStanTypeAliasInUnion
9+
{
10+
/**
11+
* @return Config|null
12+
*/
13+
public function provide(): ?array
14+
{
15+
return null;
16+
}
17+
}

‎rules/DeadCode/Rector/ClassMethod/RemoveReturnTagIncompatibleWithNativeTypeRector.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PhpParser\Node\Stmt\Function_;
10+
use PHPStan\Analyser\Scope;
11+
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
1012
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
13+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1114
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1215
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1316
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
17+
use Rector\NodeTypeResolver\Node\AttributeKey;
1418
use Rector\Rector\AbstractRector;
1519
use Rector\StaticTypeMapper\StaticTypeMapper;
1620
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -99,6 +103,10 @@ public function refactor(Node $node): ?Node
99103
return null;
100104
}
101105

106+
if ($this->isClassTypeAlias($node, $returnTagValueNode)) {
107+
return null;
108+
}
109+
102110
$nativeReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->returnType);
103111
$docReturnType = $phpDocInfo->getReturnType();
104112

@@ -115,4 +123,26 @@ public function refactor(Node $node): ?Node
115123

116124
return $node;
117125
}
126+
127+
private function isClassTypeAlias(Node $node, ReturnTagValueNode $returnTagValueNode): bool
128+
{
129+
$scope = $node->getAttribute(AttributeKey::SCOPE);
130+
if (! $scope instanceof Scope || ! $scope->isInClass()) {
131+
return false;
132+
}
133+
134+
$resolvedPhpDocBlock = $scope->getClassReflection()
135+
->getResolvedPhpDoc();
136+
if (! $resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) {
137+
return false;
138+
}
139+
140+
$typeAliases = $resolvedPhpDocBlock->getTypeAliasTags() + $resolvedPhpDocBlock->getTypeAliasImportTags();
141+
if ($typeAliases === []) {
142+
return false;
143+
}
144+
145+
return $returnTagValueNode->type instanceof IdentifierTypeNode
146+
&& isset($typeAliases[$returnTagValueNode->type->name]);
147+
}
118148
}

0 commit comments

Comments
 (0)