From dcc5eabb20af51e0c237ecb69393f40a65d11115 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 7 Oct 2025 21:25:39 +0700 Subject: [PATCH 1/2] [Php80] Skip inside by ref method on ChangeSwitchToMatchRector --- .../Fixture/skip_by_ref_method.php.inc | 31 +++++++++++++++++++ .../Switch_/ChangeSwitchToMatchRector.php | 9 +++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 rules-tests/Php80/Rector/Switch_/ChangeSwitchToMatchRector/Fixture/skip_by_ref_method.php.inc 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..354e893207b 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|Nodevisor::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) { From afdabacecf41d3b8309c1d8752dd9ad1bc7c01d4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 7 Oct 2025 21:29:11 +0700 Subject: [PATCH 2/2] typo fix --- rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php index 354e893207b..271047249be 100644 --- a/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php +++ b/rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php @@ -83,7 +83,7 @@ public function getNodeTypes(): array /** * @param StmtsAwareInterface $node - * @return null|Node|Nodevisor::DONT_TRAVERSE_CURRENT_AND_CHILDREN + * @return null|Node|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN */ public function refactor(Node $node): null|Node|int {