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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

/**
* @see https://3v4l.org/7CEb0
*/
final class SkipByRefMethod {
private int $monday = 0;
private int $tuesday = 0;
// ...

private function &getRef(int $index): int {
switch ($index) {
case 0:
return $this->monday;
case 1:
return $this->tuesday;
// ...
default:
throw new LogicException();
}
}

public function setAtIndex(int $index, int $value) {
$ref = &$this->getRef($index);
$ref = $value;
}
}

?>
9 changes: 8 additions & 1 deletion rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Loading