Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ parameters:
- rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php
- rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php
- rules/DowngradePhp73/Rector/String_/DowngradeFlexibleHeredocSyntaxRector.php
- rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php

# local attribute key
- '#Parameter \#2 \$attributeKey of static method Rector\\PhpParser\\NodeTraverser\\SimpleNodeTraverser\:\:decorateWithAttributeValue\(\) expects#'
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class InArrayDimFetch
{
public function run($c, $n, $v)
{
$properties[match ($c) {
'Error' => 'TypeError',
'Exception' => 'ErrorException',
default => $c,
}][$n] = $v;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class InArrayDimFetch
{
public function run($c, $n, $v)
{
switch ($c) {
case 'Error':
$match = 'TypeError';
break;
case 'Exception':
$match = 'ErrorException';
break;
default:
$match = $c;
break;
}
$properties[$match][$n] = $v;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp;
Expand All @@ -19,6 +20,7 @@
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
Expand All @@ -29,6 +31,7 @@
use PhpParser\Node\Stmt\Switch_;
use PhpParser\NodeVisitor;
use PHPStan\Analyser\Scope;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
use Rector\PHPStan\ScopeFetcher;
Expand All @@ -44,7 +47,8 @@
final class DowngradeMatchToSwitchRector extends AbstractRector
{
public function __construct(
private readonly AnonymousFunctionFactory $anonymousFunctionFactory
private readonly AnonymousFunctionFactory $anonymousFunctionFactory,
private readonly VariableNaming $variableNaming
) {
}

Expand Down Expand Up @@ -102,14 +106,37 @@ public function getNodeTypes(): array
/**
* @param Echo_|Expression|Return_ $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): null|Node|array
{
/** @var Match_|null $match */
$match = null;
$hasChanged = false;

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

if ($node instanceof Expression
&& $node->expr instanceof Assign
&& $node->expr->var instanceof ArrayDimFetch
&& $node->expr->var->var instanceof ArrayDimFetch
&& $node->expr->var->var->dim instanceof Match_) {
$matchVariable = new Variable($this->variableNaming->createCountedValueName('match', $scope));
$expression = new Expression(new Assign($matchVariable, $node->expr->var->var->dim));
$expression->setAttribute(AttributeKey::SCOPE, $scope);
$refactored = $this->refactor($expression);

if ($refactored === null) {
return null;
}

$node->expr->var->var->dim = $matchVariable;

$stmts = is_array($refactored)
? $refactored :
[$refactored];

return [...$stmts, $node];
}

$this->traverseNodesWithCallable(
$node,
function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
Expand Down
Loading