Skip to content

Commit f05a1bc

Browse files
committed
[DowngradePhp80] Handle in array dim fetch on DowngradeMatchToSwitchRector
1 parent f02926e commit f05a1bc

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
4+
5+
class InArrayDimFetch
6+
{
7+
public function run($c, $n, $v)
8+
{
9+
$properties[match ($c) {
10+
'Error' => 'TypeError',
11+
'Exception' => 'ErrorException',
12+
default => $c,
13+
}][$n] = $v;
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
22+
23+
class InArrayDimFetch
24+
{
25+
public function run($c, $n, $v)
26+
{
27+
switch ($c) {
28+
case 'Error':
29+
$match = 'TypeError';
30+
break;
31+
case 'Exception':
32+
$match = 'ErrorException';
33+
break;
34+
default:
35+
$match = $c;
36+
break;
37+
}
38+
$properties[$match][$n] = $v;
39+
}
40+
}
41+
42+
?>

rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\ArrayItem;
1010
use PhpParser\Node\Expr;
11+
use PhpParser\Node\Expr\ArrayDimFetch;
1112
use PhpParser\Node\Expr\ArrowFunction;
1213
use PhpParser\Node\Expr\Assign;
1314
use PhpParser\Node\Expr\BinaryOp;
@@ -19,6 +20,7 @@
1920
use PhpParser\Node\Expr\NullsafeMethodCall;
2021
use PhpParser\Node\Expr\StaticCall;
2122
use PhpParser\Node\Expr\Throw_;
23+
use PhpParser\Node\Expr\Variable;
2224
use PhpParser\Node\MatchArm;
2325
use PhpParser\Node\Stmt;
2426
use PhpParser\Node\Stmt\Break_;
@@ -29,6 +31,7 @@
2931
use PhpParser\Node\Stmt\Switch_;
3032
use PhpParser\NodeVisitor;
3133
use PHPStan\Analyser\Scope;
34+
use Rector\Naming\Naming\VariableNaming;
3235
use Rector\NodeTypeResolver\Node\AttributeKey;
3336
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
3437
use Rector\PHPStan\ScopeFetcher;
@@ -44,7 +47,8 @@
4447
final class DowngradeMatchToSwitchRector extends AbstractRector
4548
{
4649
public function __construct(
47-
private readonly AnonymousFunctionFactory $anonymousFunctionFactory
50+
private readonly AnonymousFunctionFactory $anonymousFunctionFactory,
51+
private readonly VariableNaming $variableNaming
4852
) {
4953
}
5054

@@ -101,15 +105,31 @@ public function getNodeTypes(): array
101105

102106
/**
103107
* @param Echo_|Expression|Return_ $node
108+
* @return null|Node|Node[]
104109
*/
105-
public function refactor(Node $node): ?Node
110+
public function refactor(Node $node): null|Node|array
106111
{
107112
/** @var Match_|null $match */
108113
$match = null;
109114
$hasChanged = false;
110115

111116
$scope = ScopeFetcher::fetch($node);
112117

118+
if ($node instanceof Expression
119+
&& $node->expr instanceof Assign
120+
&& $node->expr->var instanceof ArrayDimFetch
121+
&& $node->expr->var->var instanceof ArrayDimFetch
122+
&& $node->expr->var->var->dim instanceof Match_) {
123+
$matchVariable = new Variable($this->variableNaming->createCountedValueName('match', $scope));
124+
$expression = new Expression(new Assign($matchVariable, $node->expr->var->var->dim));
125+
$expression->setAttribute(AttributeKey::SCOPE, $scope);
126+
$expression = $this->refactor($expression);
127+
128+
$node->expr->var->var->dim = $matchVariable;
129+
130+
return [$expression, $node];
131+
}
132+
113133
$this->traverseNodesWithCallable(
114134
$node,
115135
function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {

0 commit comments

Comments
 (0)