Skip to content

Commit 67d2b14

Browse files
committed
[transform] tidy up array dim fetch to method call rector
1 parent 35b9d1c commit 67d2b14

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/partial_transformation.php.inc renamed to rules-tests/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/handle_container_get.php.inc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallR
44

55
/** @var \Container $object */
66
$object['key'];
7-
$object['key'] = 'value';
8-
isset($object['key']);
9-
unset($object['key']);
107

118
?>
129
-----
@@ -16,8 +13,5 @@ namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallR
1613

1714
/** @var \Container $object */
1815
$object->get('key');
19-
$object['key'] = 'value';
20-
isset($object['key']);
21-
unset($object['key']);
2216

2317
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\Fixture;
4+
5+
/** @var \Container $object */
6+
$object['key'] = 'value';
7+
isset($object['key']);
8+
unset($object['key']);

rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\NodeVisitor;
2020
use PHPStan\Type\ObjectType;
2121
use Rector\Contract\Rector\ConfigurableRectorInterface;
22+
use Rector\NodeTypeResolver\Node\AttributeKey;
2223
use Rector\Rector\AbstractRector;
2324
use Rector\Transform\ValueObject\ArrayDimFetchToMethodCall;
2425
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
@@ -87,11 +88,20 @@ public function refactor(Node $node): array|Expr|null|int
8788
return null;
8889
}
8990

90-
return $this->getMethodCall($node->var, 'set', $node->expr)
91-
?? NodeVisitor::DONT_TRAVERSE_CHILDREN;
91+
$setMethodCall = $this->createExplicitMethodCall($node->var, 'set', $node->expr);
92+
if ($setMethodCall instanceof MethodCall) {
93+
return $setMethodCall;
94+
}
95+
96+
return null;
9297
}
9398

94-
return $this->getMethodCall($node, 'get');
99+
// is part of assign, skip
100+
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED)) {
101+
return null;
102+
}
103+
104+
return $this->createExplicitMethodCall($node, 'get');
95105
}
96106

97107
public function configure(array $configuration): void
@@ -108,7 +118,7 @@ private function handleIsset(Isset_ $isset): Expr|int|null
108118

109119
foreach ($isset->vars as $var) {
110120
if ($var instanceof ArrayDimFetch) {
111-
$methodCall = $this->getMethodCall($var, 'exists');
121+
$methodCall = $this->createExplicitMethodCall($var, 'exists');
112122

113123
if ($methodCall instanceof MethodCall) {
114124
$exprs[] = $methodCall;
@@ -147,7 +157,7 @@ private function handleUnset(Unset_ $unset): array|int
147157

148158
foreach ($unset->vars as $var) {
149159
if ($var instanceof ArrayDimFetch) {
150-
$methodCall = $this->getMethodCall($var, 'unset');
160+
$methodCall = $this->createExplicitMethodCall($var, 'unset');
151161

152162
if ($methodCall instanceof MethodCall) {
153163
$stmts[] = new Expression($methodCall);
@@ -173,8 +183,11 @@ private function handleUnset(Unset_ $unset): array|int
173183
/**
174184
* @param 'get'|'set'|'exists'|'unset' $action
175185
*/
176-
private function getMethodCall(ArrayDimFetch $arrayDimFetch, string $action, ?Expr $expr = null): ?MethodCall
177-
{
186+
private function createExplicitMethodCall(
187+
ArrayDimFetch $arrayDimFetch,
188+
string $action,
189+
?Expr $expr = null
190+
): ?MethodCall {
178191
if (! $arrayDimFetch->dim instanceof Node) {
179192
return null;
180193
}

src/Reporting/DeprecatedRulesReporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Rector\Reporting;
66

7-
use Rector\PhpParser\Enum\NodeGroup;
87
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
98
use Rector\Configuration\Option;
109
use Rector\Configuration\Parameter\SimpleParameterProvider;
1110
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1211
use Rector\Contract\Rector\RectorInterface;
12+
use Rector\PhpParser\Enum\NodeGroup;
1313
use Symfony\Component\Console\Style\SymfonyStyle;
1414

1515
final readonly class DeprecatedRulesReporter

0 commit comments

Comments
 (0)