Skip to content

Commit 8f66e97

Browse files
authored
[cleanup] cleanup ArrayDimFetchToMethodCallRector to use direct nodes (#7713)
1 parent 338bd14 commit 8f66e97

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

phpstan.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,9 @@ parameters:
385385

386386
# handles full file
387387
-
388-
path: rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
388+
paths:
389+
- rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
390+
- rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
389391
identifier: rector.noOnlyNullReturnInRefactor
390392

391393
-
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Transform\Enum;
6+
7+
final class MagicPropertyHandler
8+
{
9+
public const GET = 'get';
10+
11+
public const SET = 'set';
12+
13+
public const ISSET_ = 'exists';
14+
15+
public const UNSET = 'unset';
16+
}

rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use PhpParser\Node\Stmt;
1616
use PhpParser\Node\Stmt\Expression;
1717
use PhpParser\Node\Stmt\Unset_;
18-
use PhpParser\NodeVisitor;
1918
use PHPStan\Type\ObjectType;
2019
use Rector\Contract\Rector\ConfigurableRectorInterface;
2120
use Rector\NodeTypeResolver\Node\AttributeKey;
2221
use Rector\Rector\AbstractRector;
22+
use Rector\Transform\Enum\MagicPropertyHandler;
2323
use Rector\Transform\ValueObject\ArrayDimFetchToMethodCall;
2424
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2525
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -65,9 +65,9 @@ public function getNodeTypes(): array
6565

6666
/**
6767
* @param ArrayDimFetch|Assign|Isset_|Unset_ $node
68-
* @return ($node is Unset_ ? Stmt[]|int : ($node is Isset_ ? Expr|int : MethodCall|int|null))
68+
* @return ($node is Unset_ ? Stmt[] : ($node is Isset_ ? Expr : MethodCall|null))
6969
*/
70-
public function refactor(Node $node): array|Expr|null|int
70+
public function refactor(Node $node): array|Expr|null
7171
{
7272
if ($node instanceof Unset_) {
7373
return $this->handleUnset($node);
@@ -82,7 +82,7 @@ public function refactor(Node $node): array|Expr|null|int
8282
return null;
8383
}
8484

85-
return $this->createExplicitMethodCall($node->var, 'set', $node->expr);
85+
return $this->createExplicitMethodCall($node->var, MagicPropertyHandler::SET, $node->expr);
8686
}
8787

8888
// is part of assign, skip
@@ -94,7 +94,16 @@ public function refactor(Node $node): array|Expr|null|int
9494
return null;
9595
}
9696

97-
return $this->createExplicitMethodCall($node, 'get');
97+
// should be skipped as handled above
98+
if ($node->getAttribute(AttributeKey::IS_UNSET_VAR)) {
99+
return null;
100+
}
101+
102+
if ($node->getAttribute(AttributeKey::IS_ISSET_VAR)) {
103+
return null;
104+
}
105+
106+
return $this->createExplicitMethodCall($node, MagicPropertyHandler::GET);
98107
}
99108

100109
public function configure(array $configuration): void
@@ -104,7 +113,7 @@ public function configure(array $configuration): void
104113
$this->arrayDimFetchToMethodCalls = $configuration;
105114
}
106115

107-
private function handleIsset(Isset_ $isset): Expr|int|null
116+
private function handleIsset(Isset_ $isset): Expr|null
108117
{
109118
$issets = [];
110119
$exprs = [];
@@ -123,7 +132,8 @@ private function handleIsset(Isset_ $isset): Expr|int|null
123132
}
124133

125134
if ($exprs === []) {
126-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
135+
// nothing to handle
136+
return null;
127137
}
128138

129139
if ($issets !== []) {
@@ -141,9 +151,9 @@ private function handleIsset(Isset_ $isset): Expr|int|null
141151
}
142152

143153
/**
144-
* @return Stmt[]|int
154+
* @return Stmt[]|null
145155
*/
146-
private function handleUnset(Unset_ $unset): array|int
156+
private function handleUnset(Unset_ $unset): ?array
147157
{
148158
$unsets = [];
149159
$stmts = [];
@@ -161,8 +171,9 @@ private function handleUnset(Unset_ $unset): array|int
161171
$unsets[] = $var;
162172
}
163173

174+
// nothing to change
164175
if ($stmts === []) {
165-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
176+
return null;
166177
}
167178

168179
if ($unsets !== []) {
@@ -174,11 +185,11 @@ private function handleUnset(Unset_ $unset): array|int
174185
}
175186

176187
/**
177-
* @param 'get'|'set'|'exists'|'unset' $action
188+
* @param MagicPropertyHandler::* $magicPropertyHandler
178189
*/
179190
private function createExplicitMethodCall(
180191
ArrayDimFetch $arrayDimFetch,
181-
string $action,
192+
string $magicPropertyHandler,
182193
?Expr $expr = null
183194
): ?MethodCall {
184195
if (! $arrayDimFetch->dim instanceof Node) {
@@ -190,11 +201,11 @@ private function createExplicitMethodCall(
190201
continue;
191202
}
192203

193-
$method = match ($action) {
194-
'get' => $arrayDimFetchToMethodCall->getMethod(),
195-
'set' => $arrayDimFetchToMethodCall->getSetMethod(),
196-
'exists' => $arrayDimFetchToMethodCall->getExistsMethod(),
197-
'unset' => $arrayDimFetchToMethodCall->getUnsetMethod(),
204+
$method = match ($magicPropertyHandler) {
205+
MagicPropertyHandler::GET => $arrayDimFetchToMethodCall->getMethod(),
206+
MagicPropertyHandler::SET => $arrayDimFetchToMethodCall->getSetMethod(),
207+
MagicPropertyHandler::ISSET_ => $arrayDimFetchToMethodCall->getExistsMethod(),
208+
MagicPropertyHandler::UNSET => $arrayDimFetchToMethodCall->getUnsetMethod(),
198209
};
199210

200211
if ($method === null) {

rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt;
99
use PhpParser\Node\Stmt\Nop;
10-
use PhpParser\NodeVisitor;
1110
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
1211
use Rector\Contract\Rector\HTMLAverseRectorInterface;
1312
use Rector\PhpParser\Enum\NodeGroup;
@@ -103,11 +102,11 @@ public function getNodeTypes(): array
103102
/**
104103
* @param StmtsAware $node
105104
*/
106-
public function refactor(Node $node): int
105+
public function refactor(Node $node): null
107106
{
108107
// workaround, as Rector now only hooks to specific nodes, not arrays
109108
// avoid traversing, as we already handled in beforeTraverse()
110-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
109+
return null;
111110
}
112111

113112
public function provideMinPhpVersion(): int

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ final class AttributeKey
202202
*/
203203
public const IS_UNSET_VAR = 'is_unset_var';
204204

205+
public const IS_ISSET_VAR = 'is_isset_var';
206+
205207
/**
206208
* @var string
207209
*/

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Expr\ArrayDimFetch;
1212
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1313
use PhpParser\Node\Expr\Closure;
14+
use PhpParser\Node\Expr\Isset_;
1415
use PhpParser\Node\Expr\PostDec;
1516
use PhpParser\Node\Expr\PostInc;
1617
use PhpParser\Node\Expr\PreDec;
@@ -65,6 +66,14 @@ public function enterNode(Node $node): ?Node
6566
return null;
6667
}
6768

69+
if ($node instanceof Isset_) {
70+
foreach ($node->vars as $var) {
71+
$var->setAttribute(AttributeKey::IS_ISSET_VAR, true);
72+
}
73+
74+
return null;
75+
}
76+
6877
if ($node instanceof Attribute) {
6978
$this->processContextInAttribute($node);
7079
return null;

0 commit comments

Comments
 (0)