mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
Merge pull request #1076 from rectorphp/switch-continue
[PHP] Add ContinueToBreakInSwitchRector
This commit is contained in:
commit
6fdcfcb8fb
@ -1,2 +1,3 @@
|
||||
services:
|
||||
Rector\Php\Rector\Property\VarToPublicPropertyRector: ~
|
||||
Rector\Php\Rector\Switch_\ContinueToBreakInSwitchRector: ~
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user