Repository navigation
Expand file tree
/
Copy pathNarrowArrayCollectionDocblockRector.php
More file actions
78 lines (65 loc) · 2.3 KB
/
Copy pathNarrowArrayCollectionDocblockRector.php
File metadata and controls
78 lines (65 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace Rector\Tests\Issues\RepeatedThreeTimes\Source;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\RuleDefinition;
use Rector\StaticTypeMapper\StaticTypeMapper;
/**
* @param array|SomeArrayCollection $items → @param array|SomeCollection $items
*/
final class NarrowArrayCollectionDocblockRector extends AbstractRector
{
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly StaticTypeMapper $staticTypeMapper
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('', []);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?ClassMethod
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if ($phpDocInfo === null) {
return null;
}
$hasChanged = false;
foreach ($phpDocInfo->getParamTagValueNodes() as $paramTagValueNode) {
if (! $paramTagValueNode->type instanceof UnionTypeNode) {
continue;
}
foreach ($paramTagValueNode->type->types as $key => $unionedTypeNode) {
$unionedType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($unionedTypeNode, $node);
if (! $unionedType->equals(new ObjectType(SomeArrayCollection::class))) {
continue;
}
$paramTagValueNode->type->types[$key] = new FullyQualifiedIdentifierTypeNode(SomeCollection::class);
$hasChanged = true;
}
}
if (! $hasChanged) {
return null;
}
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}
}