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,40 @@
<?php

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

class CopySwitchComment
{
public function run()
{
// some comment here
switch ($this->lexer->lookahead['type']) {
case Lexer::T_DELETE:
$statement = $this->DeleteStatement();
break;

default:
$statement = $this->syntaxError('SELECT, UPDATE or DELETE');
break;
}
}
}

?>
-----
<?php

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

class CopySwitchComment
{
public function run()
{
// some comment here
$statement = match ($this->lexer->lookahead['type']) {
Lexer::T_DELETE => $this->DeleteStatement(),
default => $this->syntaxError('SELECT, UPDATE or DELETE'),
};
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class CopySwitchCommentWithReturn
{
public function run($value)
{
// some comment here
switch ($value) {
case 1:
return 1000;
break;

default:
return 2000;
break;
}
}
}

?>
-----
<?php

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

class CopySwitchCommentWithReturn
{
public function run($value)
{
// some comment here
return match ($value) {
1 => 1000,
default => 2000,
};
}
}

?>
2 changes: 2 additions & 0 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function refactor(Node $node): ?Node

$assign = new Assign($assignVar, $match);
$node->stmts[$key] = new Expression($assign);
$this->mirrorComments($node->stmts[$key], $stmt);

$hasChanged = true;

Expand All @@ -133,6 +134,7 @@ public function refactor(Node $node): ?Node
}

$node->stmts[$key] = $isReturn ? new Return_($match) : new Expression($match);
$this->mirrorComments($node->stmts[$key], $stmt);
$hasChanged = true;
}

Expand Down