-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDowngradeUnionIntersectionRector.php
More file actions
146 lines (125 loc) · 4 KB
/
Copy pathDowngradeUnionIntersectionRector.php
File metadata and controls
146 lines (125 loc) · 4 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp82\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType;
use Rector\NodeManipulator\PropertyDecorator;
use Rector\PhpDocDecorator\PhpDocFromTypeDeclarationDecorator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @changelog https://php.watch/versions/8.2/dnf-types
*
* @see \Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\DowngradeUnionIntersectionRectorTest
*/
final class DowngradeUnionIntersectionRector extends AbstractRector
{
public function __construct(
private readonly PropertyDecorator $propertyDecorator,
private readonly PhpDocFromTypeDeclarationDecorator $phpDocFromTypeDeclarationDecorator
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove the union type with intersection, use docblock based',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public (A&B)|C $foo;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
/**
* @var (A&B)|C
*/
public $foo;
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassLike::class, ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
}
/**
* @param ClassLike|ClassMethod|Function_|Closure|ArrowFunction $node
*/
public function refactor(Node $node): ?Node
{
if (! $node instanceof ClassLike) {
return $this->processFunctionLike($node);
}
return $this->processClassLike($node);
}
private function processFunctionLike(
ClassMethod|Function_|Closure|ArrowFunction $functionLike
): null|ClassMethod|Function_|Closure|ArrowFunction {
$paramDecorated = false;
foreach ($functionLike->getParams() as $param) {
if (! $this->isUnionIntersection($param->type)) {
continue;
}
Assert::isInstanceOf($param->type, UnionType::class);
$this->phpDocFromTypeDeclarationDecorator->decorateParam(
$param,
$functionLike,
[\PHPStan\Type\UnionType::class]
);
$paramDecorated = true;
}
if (! $this->isUnionIntersection($functionLike->returnType)) {
if ($paramDecorated) {
return $functionLike;
}
return null;
}
Assert::isInstanceOf($functionLike->returnType, UnionType::class);
$this->phpDocFromTypeDeclarationDecorator->decorateReturn($functionLike);
return $functionLike;
}
private function processClassLike(ClassLike $classLike): ?ClassLike
{
$hasChanged = false;
foreach ($classLike->getProperties() as $property) {
if (! $this->isUnionIntersection($property->type)) {
continue;
}
Assert::isInstanceOf($property->type, UnionType::class);
$this->propertyDecorator->decorateWithDocBlock($property, $property->type);
$property->type = null;
$hasChanged = true;
}
return $hasChanged ? $classLike : null;
}
private function isUnionIntersection(?Node $node): bool
{
if (! $node instanceof UnionType) {
return false;
}
foreach ($node->types as $type) {
if ($type instanceof IntersectionType) {
return true;
}
}
return false;
}
}