Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/set/php/php54.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ services:
mysqli_param_count: 'mysqli_stmt_param_count'

Rector\Php54\Rector\FuncCall\RemoveReferenceFromCallRector: ~
Rector\Php54\Rector\Break_\RemoveZeroBreakContinueRector: ~
129 changes: 129 additions & 0 deletions packages/Php54/src/Rector/Break_/RemoveZeroBreakContinueRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php declare(strict_types=1);

namespace Rector\Php54\Rector\Break_;

use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ConstantType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see https://www.php.net/manual/en/control-structures.continue.php
* @see https://www.php.net/manual/en/control-structures.break.php
*
* @see \Rector\Php54\Tests\Rector\Break_\RemoveZeroBreakContinueRector\RemoveZeroBreakContinueRectorTest
*/
final class RemoveZeroBreakContinueRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove 0 from break and continue', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run($random)
{
continue 0;
break 0;

$five = 5;
continue $five;

break $random;
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run($random)
{
continue;
break;

$five = 5;
continue 5;

break;
}
}
PHP
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Break_::class, Continue_::class];
}

/**
* @param Break_|Continue_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->num === null) {
return null;
}

if ($node->num instanceof LNumber) {
$number = $this->getValue($node->num);
if ($number > 1) {
return null;
}

if ($number === 0) {
$node->num = null;
return $node;
}

return null;
}

if ($node->num instanceof Variable) {
return $this->processVariableNum($node, $node->num);
}

return null;
}

/**
* @param Break_|Continue_ $node
*/
private function processVariableNum(Node $node, Variable $numVariable): ?Node
{
$staticType = $this->getStaticType($numVariable);

if ($staticType instanceof ConstantType) {
if ($staticType instanceof ConstantIntegerType) {
if ($staticType->getValue() === 0) {
$node->num = null;
return $node;
}

if ($staticType->getValue() > 0) {
$node->num = new LNumber($staticType->getValue());
return $node;
}
}

return $node;
}

// remove variable
$node->num = null;

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Php54\Tests\Rector\Break_\RemoveZeroBreakContinueRector\Fixture;

function runContinueZeroes($random)
{
continue 0;
break 0;

$five = 5;
continue $five;

break $random;
}

?>
-----
<?php

namespace Rector\Php54\Tests\Rector\Break_\RemoveZeroBreakContinueRector\Fixture;

function runContinueZeroes($random)
{
continue;
break;

$five = 5;
continue 5;

break;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Rector\Php54\Tests\Rector\Break_\RemoveZeroBreakContinueRector;

use Rector\Php54\Rector\Break_\RemoveZeroBreakContinueRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveZeroBreakContinueRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
// to prevent loading PHP 5.4+ invalid code
$this->doTestFileWithoutAutoload($file);
}

/**
* @return string[]
*/
public function provideDataForTest(): iterable
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
}

protected function getRectorClass(): string
{
return RemoveZeroBreakContinueRector::class;
}
}