-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathRecastingRemovalRector.php
More file actions
168 lines (143 loc) · 4.55 KB
/
RecastingRemovalRector.php
File metadata and controls
168 lines (143 loc) · 4.55 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\Cast;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Cast\Object_;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\RecastingRemovalRectorTest
*/
final class RecastingRemovalRector extends AbstractRector
{
/**
* @var array<class-string<Node>, class-string<Type>>
*/
private const CAST_CLASS_TO_NODE_TYPE = [
String_::class => StringType::class,
Bool_::class => BooleanType::class,
Array_::class => ArrayType::class,
Int_::class => IntegerType::class,
Object_::class => ObjectType::class,
Double::class => FloatType::class,
];
public function __construct(
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
private readonly ExprAnalyzer $exprAnalyzer,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove recasting of the same type', [
new CodeSample(
<<<'CODE_SAMPLE'
$string = '';
$string = (string) $string;
$array = [];
$array = (array) $array;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$string = '';
$string = $string;
$array = [];
$array = $array;
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Cast::class];
}
/**
* @param Cast $node
*/
public function refactor(Node $node): ?Node
{
$nodeClass = $node::class;
if (! isset(self::CAST_CLASS_TO_NODE_TYPE[$nodeClass])) {
return null;
}
$nodeType = $this->nodeTypeResolver->getNativeType($node->expr);
if ($nodeType instanceof MixedType) {
return null;
}
if ($nodeType instanceof ConstantArrayType && $nodeClass === Array_::class) {
if ($this->shouldSkip($node->expr)) {
return null;
}
if ($this->shouldSkipCall($node->expr)) {
return null;
}
return $node->expr;
}
$sameNodeType = self::CAST_CLASS_TO_NODE_TYPE[$nodeClass];
if (! $nodeType instanceof $sameNodeType) {
return null;
}
if ($this->shouldSkip($node->expr)) {
return null;
}
if ($this->shouldSkipCall($node->expr)) {
return null;
}
return $node->expr;
}
private function shouldSkipCall(Expr $expr): bool
{
if (! $expr instanceof MethodCall && ! $expr instanceof StaticCall) {
return false;
}
$type = $this->nodeTypeResolver->getNativeType($expr);
return $type instanceof MixedType && ! $type->isExplicitMixed();
}
private function shouldSkip(Expr $expr): bool
{
$type = $this->getType($expr);
if ($type instanceof UnionType) {
foreach ($type->getTypes() as $unionedType) {
if ($unionedType instanceof MixedType) {
return true;
}
}
}
if (! $this->propertyFetchAnalyzer->isPropertyFetch($expr)) {
return $this->exprAnalyzer->isNonTypedFromParam($expr);
}
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($expr);
if (! $phpPropertyReflection instanceof PhpPropertyReflection) {
return true;
}
$nativeType = $phpPropertyReflection->getNativeType();
return $nativeType instanceof MixedType;
}
}