Skip to content

Commit 7cc3c7c

Browse files
committed
cleanup ArrayDimFetchToMethodCallRector to use direct attributes
1 parent 7bb5249 commit 7cc3c7c

4 files changed

Lines changed: 54 additions & 17 deletions

File tree

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: 26 additions & 16 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;
@@ -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 = [];
@@ -122,9 +131,9 @@ private function handleIsset(Isset_ $isset): Expr|int|null
122131
$issets[] = $var;
123132
}
124133

125-
// @todo fix this
126134
if ($exprs === []) {
127-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
135+
// nothing to handle
136+
return null;
128137
}
129138

130139
if ($issets !== []) {
@@ -142,9 +151,9 @@ private function handleIsset(Isset_ $isset): Expr|int|null
142151
}
143152

144153
/**
145-
* @return Stmt[]|int
154+
* @return Stmt[]|null
146155
*/
147-
private function handleUnset(Unset_ $unset): array|int
156+
private function handleUnset(Unset_ $unset): ?array
148157
{
149158
$unsets = [];
150159
$stmts = [];
@@ -162,8 +171,9 @@ private function handleUnset(Unset_ $unset): array|int
162171
$unsets[] = $var;
163172
}
164173

174+
// nothing to change
165175
if ($stmts === []) {
166-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
176+
return null;
167177
}
168178

169179
if ($unsets !== []) {
@@ -175,11 +185,11 @@ private function handleUnset(Unset_ $unset): array|int
175185
}
176186

177187
/**
178-
* @param 'get'|'set'|'exists'|'unset' $action
188+
* @param MagicPropertyHandler::* $magicPropertyHandler
179189
*/
180190
private function createExplicitMethodCall(
181191
ArrayDimFetch $arrayDimFetch,
182-
string $action,
192+
string $magicPropertyHandler,
183193
?Expr $expr = null
184194
): ?MethodCall {
185195
if (! $arrayDimFetch->dim instanceof Node) {
@@ -191,11 +201,11 @@ private function createExplicitMethodCall(
191201
continue;
192202
}
193203

194-
$method = match ($action) {
195-
'get' => $arrayDimFetchToMethodCall->getMethod(),
196-
'set' => $arrayDimFetchToMethodCall->getSetMethod(),
197-
'exists' => $arrayDimFetchToMethodCall->getExistsMethod(),
198-
'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(),
199209
};
200210

201211
if ($method === null) {

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ final class AttributeKey
196196
*/
197197
public const IS_UNSET_VAR = 'is_unset_var';
198198

199+
public const IS_ISSET_VAR = 'is_isset_var';
200+
199201
/**
200202
* @var string
201203
*/

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ public function enterNode(Node $node): ?Node
5858
}
5959

6060
if ($node instanceof Unset_) {
61-
$this->processContextInUnset($node);
61+
foreach ($node->vars as $var) {
62+
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
63+
}
64+
return null;
65+
}
66+
67+
if ($node instanceof Node\Expr\Isset_) {
68+
foreach ($node->vars as $var) {
69+
$var->setAttribute(AttributeKey::IS_ISSET_VAR, true);
70+
}
6271
return null;
6372
}
6473

0 commit comments

Comments
 (0)