|
5 | 5 | namespace Rector\CodeQuality\Rector\Empty_; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr; |
9 | 8 | use PhpParser\Node\Expr\Array_; |
10 | 9 | use PhpParser\Node\Expr\BinaryOp\Identical; |
11 | 10 | use PhpParser\Node\Expr\BinaryOp\NotIdentical; |
@@ -79,43 +78,36 @@ public function getNodeTypes(): array |
79 | 78 | */ |
80 | 79 | public function refactor(Node $node): ?Node |
81 | 80 | { |
82 | | - $scope = ScopeFetcher::fetch($node); |
83 | | - if ($node instanceof BooleanNot) { |
84 | | - if ($node->expr instanceof Empty_ && $this->isAllowedExpr($node->expr->expr, $scope)) { |
85 | | - return new NotIdentical($node->expr->expr, new Array_()); |
86 | | - } |
87 | | - |
| 81 | + $empty = $node instanceof BooleanNot ? $node->expr : $node; |
| 82 | + if (! $empty instanceof Empty_) { |
88 | 83 | return null; |
89 | 84 | } |
90 | 85 |
|
91 | | - if (! $this->isAllowedExpr($node->expr, $scope)) { |
| 86 | + $expr = $empty->expr; |
| 87 | + if (! $expr instanceof Variable && ! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) { |
92 | 88 | return null; |
93 | 89 | } |
94 | 90 |
|
95 | | - return new Identical($node->expr, new Array_()); |
96 | | - } |
| 91 | + if ($expr instanceof Variable && is_string($expr->name) && $this->reservedKeywordAnalyzer->isNativeVariable($expr->name)) { |
| 92 | + return null; |
| 93 | + } |
97 | 94 |
|
98 | | - private function isAllowedVariable(Variable $variable): bool |
99 | | - { |
100 | | - if (is_string($variable->name) && $this->reservedKeywordAnalyzer->isNativeVariable($variable->name)) { |
101 | | - return false; |
| 95 | + $scope = ScopeFetcher::fetch($node); |
| 96 | + if (! $this->isAllowedExpr($expr, $scope)) { |
| 97 | + return null; |
102 | 98 | } |
103 | 99 |
|
104 | | - return ! $this->exprAnalyzer->isNonTypedFromParam($variable); |
| 100 | + return $node instanceof BooleanNot ? new NotIdentical($expr, new Array_()) : new Identical($expr, new Array_()); |
105 | 101 | } |
106 | 102 |
|
107 | | - private function isAllowedExpr(Expr $expr, Scope $scope): bool |
| 103 | + private function isAllowedExpr(Variable|PropertyFetch|StaticPropertyFetch $expr, Scope $scope): bool |
108 | 104 | { |
109 | | - if (! $expr instanceof Variable && ! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) { |
110 | | - return false; |
111 | | - } |
112 | | - |
113 | 105 | if (! $scope->getType($expr)->isArray()->yes()) { |
114 | 106 | return false; |
115 | 107 | } |
116 | 108 |
|
117 | 109 | if ($expr instanceof Variable) { |
118 | | - return $this->isAllowedVariable($expr); |
| 110 | + return ! $this->exprAnalyzer->isNonTypedFromParam($expr); |
119 | 111 | } |
120 | 112 |
|
121 | 113 | if (! $expr->name instanceof Identifier) { |
|
0 commit comments