Skip to content

Commit

Permalink
[Php80] Skip no default on case collection assign on ChangeSwitchToMa…
Browse files Browse the repository at this point in the history
…tchRector (#2631)

* [Php80] Skip no default on case collection assign on ChangeSwitchToMatchRector

* Fixed 🎉

* clean up

* clean up

* final touch: clean up
  • Loading branch information
samsonasik committed Jul 4, 2022
1 parent 8c8def5 commit d7a1c70
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

final class SkipNoDefaultCaseCollectionAssign
{
function foo(string $var): void {
switch ($var) {
case 'hey':
$var = 'oh';
break;
case 'foo':
case 'bar':
$var = "baz";
break;
}

dump($var);
}
}
38 changes: 20 additions & 18 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,28 @@ public function refactor(Node $node): ?Node

$match = $this->processImplicitThrowsAfterSwitch($node, $match, $condAndExprs);

if ($isReturn) {
return $this->processReturn($match);
}

$assignVar = $this->resolveAssignVar($condAndExprs);
$hasDefaultValue = $this->matchSwitchAnalyzer->hasDefaultValue($match);

if ($assignVar instanceof Expr) {
return $this->changeToAssign($node, $match, $assignVar);
return $this->changeToAssign($node, $match, $assignVar, $hasDefaultValue);
}

if (! $hasDefaultValue) {
return null;
}

return $match;
return $isReturn
? new Return_($match)
: $match;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::MATCH_EXPRESSION;
}

private function processReturn(Match_ $match): ?Return_
{
if (! $this->matchSwitchAnalyzer->hasDefaultValue($match)) {
return null;
}

return new Return_($match);
}

private function changeToAssign(Switch_ $switch, Match_ $match, Expr $expr): ?Assign
private function changeToAssign(Switch_ $switch, Match_ $match, Expr $expr, bool $hasDefaultValue): ?Assign
{
$nextReturn = $switch->getAttribute(AttributeKey::NEXT_NODE);

Expand All @@ -163,10 +158,10 @@ private function changeToAssign(Switch_ $switch, Match_ $match, Expr $expr): ?As

$assign = new Assign($expr, $match);
if (! $prevInitializedAssign instanceof Assign) {
return $assign;
return $this->resolveCurrentAssign($hasDefaultValue, $assign);
}

if ($this->matchSwitchAnalyzer->hasDefaultValue($match)) {
if ($hasDefaultValue) {
$default = $match->arms[count($match->arms) - 1]->body;
if ($this->nodeComparator->areNodesEqual($default, $prevInitializedAssign->var)) {
return $assign;
Expand All @@ -183,6 +178,13 @@ private function changeToAssign(Switch_ $switch, Match_ $match, Expr $expr): ?As
return $assign;
}

private function resolveCurrentAssign(bool $hasDefaultValue, Assign $assign): ?Assign
{
return $hasDefaultValue
? $assign
: null;
}

/**
* @param CondAndExpr[] $condAndExprs
*/
Expand Down

0 comments on commit d7a1c70

Please sign in to comment.