-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
SimplifyEmptyCheckOnEmptyArrayRector.php
154 lines (150 loc) · 5.3 KB
/
SimplifyEmptyCheckOnEmptyArrayRector.php
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
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\Empty_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\AstResolver;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Reflection\ReflectionResolver;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\SimplifyEmptyCheckOnEmptyArrayRectorTest
*/
final class SimplifyEmptyCheckOnEmptyArrayRector extends AbstractScopeAwareRector
{
/**
* @readonly
* @var \Rector\NodeAnalyzer\ExprAnalyzer
*/
private $exprAnalyzer;
/**
* @readonly
* @var \Rector\Reflection\ReflectionResolver
*/
private $reflectionResolver;
/**
* @readonly
* @var \Rector\PhpParser\AstResolver
*/
private $astResolver;
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer
*/
private $allAssignNodePropertyTypeInferer;
/**
* @readonly
* @var \Rector\Php\ReservedKeywordAnalyzer
*/
private $reservedKeywordAnalyzer;
public function __construct(ExprAnalyzer $exprAnalyzer, ReflectionResolver $reflectionResolver, AstResolver $astResolver, AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, ReservedKeywordAnalyzer $reservedKeywordAnalyzer)
{
$this->exprAnalyzer = $exprAnalyzer;
$this->reflectionResolver = $reflectionResolver;
$this->astResolver = $astResolver;
$this->allAssignNodePropertyTypeInferer = $allAssignNodePropertyTypeInferer;
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Simplify empty() functions calls on empty arrays', [new CodeSample(<<<'CODE_SAMPLE'
$array = [];
if (empty($values)) {
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$array = [];
if ([] === $values) {
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Empty_::class, BooleanNot::class];
}
/**
* @param Empty_|BooleanNot $node $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if ($node instanceof BooleanNot) {
if ($node->expr instanceof Empty_ && $this->isAllowedExpr($node->expr->expr, $scope)) {
return new NotIdentical($node->expr->expr, new Array_());
}
return null;
}
if (!$this->isAllowedExpr($node->expr, $scope)) {
return null;
}
return new Identical($node->expr, new Array_());
}
private function isAllowedVariable(Variable $variable) : bool
{
if (\is_string($variable->name) && $this->reservedKeywordAnalyzer->isNativeVariable($variable->name)) {
return \false;
}
return !$this->exprAnalyzer->isNonTypedFromParam($variable);
}
private function isAllowedExpr(Expr $expr, Scope $scope) : bool
{
if (!$scope->getType($expr) instanceof ArrayType) {
return \false;
}
if ($expr instanceof Variable) {
return $this->isAllowedVariable($expr);
}
if (!$expr instanceof PropertyFetch && !$expr instanceof StaticPropertyFetch) {
return \false;
}
if (!$expr->name instanceof Identifier) {
return \false;
}
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($expr);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
$propertyName = $expr->name->toString();
if (!$classReflection->hasNativeProperty($propertyName)) {
return \false;
}
$phpPropertyReflection = $classReflection->getNativeProperty($propertyName);
$nativeType = $phpPropertyReflection->getNativeType();
if (!$nativeType instanceof MixedType) {
return $nativeType instanceof ArrayType;
}
$property = $this->astResolver->resolvePropertyFromPropertyReflection($phpPropertyReflection);
/**
* Skip property promotion mixed type for now, as:
*
* - require assign in default param check
* - check all assign of property promotion params under the class
*/
if (!$property instanceof Property) {
return \false;
}
$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection, $this->file);
return $type instanceof ArrayType;
}
}