|
20 | 20 | use PHPStan\Type\ArrayType; |
21 | 21 | use PHPStan\Type\MixedType; |
22 | 22 | use Rector\Core\NodeAnalyzer\ExprAnalyzer; |
| 23 | +use Rector\Core\Php\ReservedKeywordAnalyzer; |
23 | 24 | use Rector\Core\PhpParser\AstResolver; |
24 | 25 | use Rector\Core\Rector\AbstractScopeAwareRector; |
25 | 26 | use Rector\Core\Reflection\ReflectionResolver; |
@@ -51,12 +52,18 @@ final class SimplifyEmptyCheckOnEmptyArrayRector extends AbstractScopeAwareRecto |
51 | 52 | * @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer |
52 | 53 | */ |
53 | 54 | private $allAssignNodePropertyTypeInferer; |
54 | | - public function __construct(ExprAnalyzer $exprAnalyzer, ReflectionResolver $reflectionResolver, AstResolver $astResolver, AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer) |
| 55 | + /** |
| 56 | + * @readonly |
| 57 | + * @var \Rector\Core\Php\ReservedKeywordAnalyzer |
| 58 | + */ |
| 59 | + private $reservedKeywordAnalyzer; |
| 60 | + public function __construct(ExprAnalyzer $exprAnalyzer, ReflectionResolver $reflectionResolver, AstResolver $astResolver, AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, ReservedKeywordAnalyzer $reservedKeywordAnalyzer) |
55 | 61 | { |
56 | 62 | $this->exprAnalyzer = $exprAnalyzer; |
57 | 63 | $this->reflectionResolver = $reflectionResolver; |
58 | 64 | $this->astResolver = $astResolver; |
59 | 65 | $this->allAssignNodePropertyTypeInferer = $allAssignNodePropertyTypeInferer; |
| 66 | + $this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer; |
60 | 67 | } |
61 | 68 | public function getRuleDefinition() : RuleDefinition |
62 | 69 | { |
@@ -97,13 +104,20 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node |
97 | 104 | } |
98 | 105 | return new Identical($node->expr, new Array_()); |
99 | 106 | } |
| 107 | + private function isAllowedVariable(Variable $variable) : bool |
| 108 | + { |
| 109 | + if (\is_string($variable->name) && $this->reservedKeywordAnalyzer->isNativeVariable($variable->name)) { |
| 110 | + return \false; |
| 111 | + } |
| 112 | + return !$this->exprAnalyzer->isNonTypedFromParam($variable); |
| 113 | + } |
100 | 114 | private function isAllowedExpr(Expr $expr, Scope $scope) : bool |
101 | 115 | { |
102 | 116 | if (!$scope->getType($expr) instanceof ArrayType) { |
103 | 117 | return \false; |
104 | 118 | } |
105 | 119 | if ($expr instanceof Variable) { |
106 | | - return !$this->exprAnalyzer->isNonTypedFromParam($expr); |
| 120 | + return $this->isAllowedVariable($expr); |
107 | 121 | } |
108 | 122 | if (!$expr instanceof PropertyFetch && !$expr instanceof StaticPropertyFetch) { |
109 | 123 | return \false; |
|
0 commit comments