Merge pull request #1076 from rectorphp/switch-continue

[PHP] Add ContinueToBreakInSwitchRector
This commit is contained in:
Tomáš Votruba 2019-02-20 03:50:04 -08:00 committed by GitHub
commit 6fdcfcb8fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 153 additions and 0 deletions

View File

@ -1,2 +1,3 @@
services: services:
Rector\Php\Rector\Property\VarToPublicPropertyRector: ~ Rector\Php\Rector\Property\VarToPublicPropertyRector: ~
Rector\Php\Rector\Switch_\ContinueToBreakInSwitchRector: ~

View File

@ -0,0 +1,76 @@
<?php declare(strict_types=1);
namespace Rector\Php\Rector\Switch_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Switch_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://stackoverflow.com/a/12349889/1348344
*/
final class ContinueToBreakInSwitchRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Use break instead of continue in switch statements', [
new CodeSample(
<<<'CODE_SAMPLE'
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
continue;
case 2:
echo 'Hello';
break;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
break;
case 2:
echo 'Hello';
break;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Switch_::class];
}
/**
* @param Switch_ $node
*/
public function refactor(Node $node): ?Node
{
foreach ($node->cases as $case) {
foreach ($case->stmts as $key => $caseStmt) {
if ($caseStmt instanceof Continue_) {
$case->stmts[$key] = new Break_();
}
}
}
return $node;
}
}

View File

@ -0,0 +1,22 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\Switch_\ContinueToBreakInSwitchRector;
use Rector\Php\Rector\Switch_\ContinueToBreakInSwitchRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class ContinueToBreakInSwitchRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFilesWithoutAutoload([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/skip_nested.php.inc',
]);
}
protected function getRectorClass(): string
{
return ContinueToBreakInSwitchRector::class;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\Php\Tests\Rector\Switch_\ContinueToBreakInSwitchRector\Fixture;
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
continue;
case 2:
echo 'Hello';
break;
}
}
?>
-----
<?php
namespace Rector\Php\Tests\Rector\Switch_\ContinueToBreakInSwitchRector\Fixture;
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
break;
case 2:
echo 'Hello';
break;
}
}
?>

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Php\Tests\Rector\Switch_\ContinueToBreakInSwitchRector\Fixture;
function skip_nested($value, array $array)
{
switch ($value) {
case 1:
foreach ($array as $item) {
if ($item == 5) {
continue;
}
}
break;
case 2:
echo 'Hello';
break;
}
}