Skip to content

Commit 6a6047c

Browse files
authored
[Php80] Skip inside by ref method on ChangeSwitchToMatchRector (#7443)
* [Php80] Skip inside by ref method on ChangeSwitchToMatchRector * typo fix
1 parent 974a73f commit 6a6047c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;
4+
5+
/**
6+
* @see https://3v4l.org/7CEb0
7+
*/
8+
final class SkipByRefMethod {
9+
private int $monday = 0;
10+
private int $tuesday = 0;
11+
// ...
12+
13+
private function &getRef(int $index): int {
14+
switch ($index) {
15+
case 0:
16+
return $this->monday;
17+
case 1:
18+
return $this->tuesday;
19+
// ...
20+
default:
21+
throw new LogicException();
22+
}
23+
}
24+
25+
public function setAtIndex(int $index, int $value) {
26+
$ref = &$this->getRef($index);
27+
$ref = $value;
28+
}
29+
}
30+
31+
?>

rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use PhpParser\Node\Expr\Cast\Int_;
1212
use PhpParser\Node\Expr\Cast\String_;
1313
use PhpParser\Node\Expr\Match_;
14+
use PhpParser\Node\FunctionLike;
1415
use PhpParser\Node\Stmt\Expression;
1516
use PhpParser\Node\Stmt\Return_;
1617
use PhpParser\Node\Stmt\Switch_;
18+
use PhpParser\NodeVisitor;
1719
use PHPStan\Type\ObjectType;
1820
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1921
use Rector\NodeAnalyzer\ExprAnalyzer;
@@ -81,13 +83,18 @@ public function getNodeTypes(): array
8183

8284
/**
8385
* @param StmtsAwareInterface $node
86+
* @return null|Node|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
8487
*/
85-
public function refactor(Node $node): ?Node
88+
public function refactor(Node $node): null|Node|int
8689
{
8790
if (! is_array($node->stmts)) {
8891
return null;
8992
}
9093

94+
if ($node instanceof FunctionLike && $node->returnsByRef()) {
95+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
96+
}
97+
9198
$hasChanged = false;
9299

93100
foreach ($node->stmts as $key => $stmt) {

0 commit comments

Comments
 (0)