Skip to content

Commit 31adca8

Browse files
authored
[transform] tidy up array dim fetch to method call rector (#7694)
* [transform] tidy up array dim fetch to method call rector * fix fixture autoload by using unique function names
1 parent 35b9d1c commit 31adca8

File tree

8 files changed

+31
-34
lines changed

8 files changed

+31
-34
lines changed

rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_first_class_callable.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;
44

5-
function run($foo = null, $bar = null, $baz = null) {}
5+
function skip_first_class_callable($foo = null, $bar = null, $baz = null) {}
66

7-
run(...);
7+
skip_first_class_callable(...);
88

99
?>

rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_no_arg.php.inc

Lines changed: 0 additions & 9 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_single_arg.php.inc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;
44

5-
function run(?string $foo = null) {}
5+
function skip_single_arg(?string $foo = null) {}
66

7-
run(foo: 'test');
8-
9-
?>
7+
skip_single_arg(foo: 'test');
8+
skip_single_arg();

rules-tests/CodeQuality/Rector/FuncCall/SortNamedParamRector/Fixture/skip_sorted.php.inc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;
44

5-
function run($foo = null, $bar = null, $baz = null) {}
5+
function skip_sorted($foo = null, $bar = null, $baz = null) {}
66

7-
run(foo: $foo, bar: $bar);
7+
skip_sorted(foo: $foo, bar: $bar);
88

9-
run($foo, bar: $bar, baz: $baz);
10-
11-
?>
9+
skip_sorted($foo, bar: $bar, baz: $baz);

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: 14 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;
@@ -86,12 +87,15 @@ public function refactor(Node $node): array|Expr|null|int
8687
if (! $node->var instanceof ArrayDimFetch) {
8788
return null;
8889
}
90+
return $this->createExplicitMethodCall($node->var, 'set', $node->expr);
91+
}
8992

90-
return $this->getMethodCall($node->var, 'set', $node->expr)
91-
?? NodeVisitor::DONT_TRAVERSE_CHILDREN;
93+
// is part of assign, skip
94+
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED)) {
95+
return null;
9296
}
9397

94-
return $this->getMethodCall($node, 'get');
98+
return $this->createExplicitMethodCall($node, 'get');
9599
}
96100

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

109113
foreach ($isset->vars as $var) {
110114
if ($var instanceof ArrayDimFetch) {
111-
$methodCall = $this->getMethodCall($var, 'exists');
115+
$methodCall = $this->createExplicitMethodCall($var, 'exists');
112116

113117
if ($methodCall instanceof MethodCall) {
114118
$exprs[] = $methodCall;
@@ -147,7 +151,7 @@ private function handleUnset(Unset_ $unset): array|int
147151

148152
foreach ($unset->vars as $var) {
149153
if ($var instanceof ArrayDimFetch) {
150-
$methodCall = $this->getMethodCall($var, 'unset');
154+
$methodCall = $this->createExplicitMethodCall($var, 'unset');
151155

152156
if ($methodCall instanceof MethodCall) {
153157
$stmts[] = new Expression($methodCall);
@@ -173,8 +177,11 @@ private function handleUnset(Unset_ $unset): array|int
173177
/**
174178
* @param 'get'|'set'|'exists'|'unset' $action
175179
*/
176-
private function getMethodCall(ArrayDimFetch $arrayDimFetch, string $action, ?Expr $expr = null): ?MethodCall
177-
{
180+
private function createExplicitMethodCall(
181+
ArrayDimFetch $arrayDimFetch,
182+
string $action,
183+
?Expr $expr = null
184+
): ?MethodCall {
178185
if (! $arrayDimFetch->dim instanceof Node) {
179186
return null;
180187
}

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)