mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
[Php80] Handle return match no default on ChangeSwitchToMatchRector (#1949)
* [Php80] Handle return no default on ChangeSwitchToMatchRector
* Fixed 🎉
* [ci-review] Rector Rectify
* [ci-review] Rector Rectify
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
f82fa3182c
commit
d5c5fe3a97
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;
|
||||
|
||||
class NoDefault
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
if (strlen($value) < 3) {
|
||||
switch($value) {
|
||||
case 'A':
|
||||
case 'B':
|
||||
return 'AB';
|
||||
case 'C':
|
||||
return 'D';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;
|
||||
|
||||
class NoDefault
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
if (strlen($value) < 3) {
|
||||
return match ($value) {
|
||||
'A', 'B' => 'AB',
|
||||
'C' => 'D',
|
||||
default => $value,
|
||||
};
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr;
|
|||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Match_;
|
||||
use PhpParser\Node\Expr\Throw_;
|
||||
use PhpParser\Node\MatchArm;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\Node\Stmt\Switch_;
|
||||
|
@ -116,7 +117,7 @@ CODE_SAMPLE
|
|||
$match = $this->processImplicitThrowsAfterSwitch($node, $match, $condAndExprs);
|
||||
|
||||
if ($isReturn) {
|
||||
return new Return_($match);
|
||||
return $this->processReturn($match, $node->cond);
|
||||
}
|
||||
|
||||
$assignExpr = $this->resolveAssignExpr($condAndExprs);
|
||||
|
@ -132,6 +133,15 @@ CODE_SAMPLE
|
|||
return PhpVersionFeature::MATCH_EXPRESSION;
|
||||
}
|
||||
|
||||
private function processReturn(Match_ $match, Expr $expr): Return_
|
||||
{
|
||||
if (! $this->matchSwitchAnalyzer->hasDefaultValue($match)) {
|
||||
$match->arms[] = new MatchArm(null, $expr);
|
||||
}
|
||||
|
||||
return new Return_($match);
|
||||
}
|
||||
|
||||
private function changeToAssign(Switch_ $switch, Match_ $match, Expr $assignExpr): Assign
|
||||
{
|
||||
$prevInitializedAssign = $this->betterNodeFinder->findFirstPreviousOfNode(
|
||||
|
|
Loading…
Reference in New Issue
Block a user