Skip to content

Commit

Permalink
[Php80] Mirror switch comment to match to ChangeSwitchToMatchRector (#…
Browse files Browse the repository at this point in the history
…5072)

* [Php80] Mirror switch comment to match to ChangeSwitchToMatchRector

* [Php80] Mirror switch comment to match to ChangeSwitchToMatchRector
  • Loading branch information
samsonasik authored Sep 23, 2023
1 parent defd57b commit 8bfaf45
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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

0 comments on commit 8bfaf45

Please sign in to comment.