diff --git a/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_by_ref_method.php.inc b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_by_ref_method.php.inc new file mode 100644 index 00000000000..07aaa8fbb08 --- /dev/null +++ b/rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_by_ref_method.php.inc @@ -0,0 +1,31 @@ +monday; + case 1: + return $this->tuesday; + // ... + default: + throw new LogicException(); + } + } + + public function setAtIndex(int $index, int $value) { + $ref = &$this->getRef($index); + $ref = $value; + } +} + +?> diff --git a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php index 8fe47104481..271047249be 100644 --- a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php +++ b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php @@ -11,9 +11,11 @@ use PhpParser\Node\Expr\Cast\Int_; use PhpParser\Node\Expr\Cast\String_; use PhpParser\Node\Expr\Match_; +use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; +use PhpParser\NodeVisitor; use PHPStan\Type\ObjectType; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\NodeAnalyzer\ExprAnalyzer; @@ -81,13 +83,18 @@ public function getNodeTypes(): array /** * @param StmtsAwareInterface $node + * @return null|Node|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): null|Node|int { if (! is_array($node->stmts)) { return null; } + if ($node instanceof FunctionLike && $node->returnsByRef()) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + $hasChanged = false; foreach ($node->stmts as $key => $stmt) {