-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.0] Add ChangeSwitchToMatchRector
- Loading branch information
1 parent
10e024b
commit eec13d9
Showing
13 changed files
with
407 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# see https://github.com/rectorphp/rector/pull/3788#issue-456723123 | ||
name: PHP-Parser conflict | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
code_coverage: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: shivammathur/setup-php@v1 | ||
with: | ||
php-version: 7.4 | ||
coverage: none | ||
|
||
- run: composer install --no-progress --ansi | ||
|
||
# https://kizu514.com/blog/pcov-is-better-than-phpdbg-and-xdebug-for-code-coverage/ | ||
- run: bin/rector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
rules/php80/src/Rector/Switch_/ChangeSwitchToMatchRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\Rector\Switch_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Match_; | ||
use PhpParser\Node\MatchArm; | ||
use PhpParser\Node\Stmt\Break_; | ||
use PhpParser\Node\Stmt\Case_; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Switch_; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://wiki.php.net/rfc/match_expression_v2 | ||
* | ||
* @see \Rector\Php80\Tests\Rector\Switch_\ChangeSwitchToMatchRector\ChangeSwitchToMatchRectorTest | ||
*/ | ||
final class ChangeSwitchToMatchRector extends AbstractRector | ||
{ | ||
/** | ||
* @var Node\Expr|null | ||
*/ | ||
private $assignVariable; | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Change switch() to match()', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$statement = switch ($this->lexer->lookahead['type']) { | ||
case Lexer::T_SELECT: | ||
$statement = $this->SelectStatement(); | ||
break; | ||
case Lexer::T_UPDATE: | ||
$statement = $this->UpdateStatement(); | ||
break; | ||
case Lexer::T_DELETE: | ||
$statement = $this->DeleteStatement(); | ||
break; | ||
default: | ||
$this->syntaxError('SELECT, UPDATE or DELETE'); | ||
break; | ||
} | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$statement = match ($this->lexer->lookahead['type']) { | ||
Lexer::T_SELECT => $this->SelectStatement(), | ||
Lexer::T_UPDATE => $this->UpdateStatement(), | ||
Lexer::T_DELETE => $this->DeleteStatement(), | ||
default => $this->syntaxError('SELECT, UPDATE or DELETE'), | ||
}; | ||
} | ||
} | ||
PHP | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Switch_::class]; | ||
} | ||
|
||
/** | ||
* @param Switch_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$this->assignVariable = null; | ||
|
||
if (! $this->hasEachCaseBreak($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->hasSingleStmtCases($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->hasSingleAssignVariableInStmtCase($node)) { | ||
return null; | ||
} | ||
|
||
$matchArms = $this->createMatchArmsFromCases($node->cases); | ||
$match = new Match_($node->cond, $matchArms); | ||
if ($this->assignVariable) { | ||
return new Assign($this->assignVariable, $match); | ||
} | ||
|
||
return $match; | ||
} | ||
|
||
private function hasEachCaseBreak(Switch_ $switch): bool | ||
{ | ||
foreach ($switch->cases as $case) { | ||
foreach ($case->stmts as $caseStmt) { | ||
if (! $caseStmt instanceof Break_) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param Case_[] $cases | ||
* @return MatchArm[] | ||
*/ | ||
private function createMatchArmsFromCases(array $cases): array | ||
{ | ||
$matchArms = []; | ||
foreach ($cases as $case) { | ||
$stmt = $case->stmts[0]; | ||
if (! $stmt instanceof Expression) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$expr = $stmt->expr; | ||
|
||
if ($expr instanceof Assign) { | ||
$this->assignVariable = $expr->var; | ||
$expr = $expr->expr; | ||
} | ||
|
||
if ($case->cond === null) { | ||
$condList = null; | ||
} else { | ||
$condList = [$case->cond]; | ||
} | ||
|
||
$matchArms[] = new MatchArm($condList, $expr); | ||
} | ||
|
||
return $matchArms; | ||
} | ||
|
||
private function hasSingleStmtCases(Switch_ $switch): bool | ||
{ | ||
foreach ($switch->cases as $case) { | ||
$stmtsWithoutBreak = array_filter($case->stmts, function (Node $node) { | ||
return ! $node instanceof Break_; | ||
}); | ||
|
||
if (count($stmtsWithoutBreak) !== 1) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function hasSingleAssignVariableInStmtCase(Switch_ $switch): bool | ||
{ | ||
$assignVariableNames = []; | ||
|
||
foreach ($switch->cases as $case) { | ||
/** @var Expression $onlyStmt */ | ||
$onlyStmt = $case->stmts[0]; | ||
$expr = $onlyStmt->expr; | ||
|
||
if (! $expr instanceof Assign) { | ||
continue; | ||
} | ||
|
||
$assignVariableNames[] = $this->getName($expr->var); | ||
} | ||
|
||
$assignVariableNames = array_unique($assignVariableNames); | ||
|
||
return count($assignVariableNames) <= 1; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
rules/php80/tests/Rector/Switch_/ChangeSwitchToMatchRector/ChangeSwitchToMatchRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\Tests\Rector\Switch_\ChangeSwitchToMatchRector; | ||
|
||
use Iterator; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ChangeSwitchToMatchRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return ChangeSwitchToMatchRector::class; | ||
} | ||
} |
Oops, something went wrong.