2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-09-26 06:07:16 +00:00
|
|
|
|
|
|
|
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;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-26 06:07:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-09-26 06:07:16 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Remove 0 from break and continue', [
|
2019-09-26 06:07:16 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-09-26 06:07:16 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($random)
|
|
|
|
{
|
|
|
|
continue 0;
|
|
|
|
break 0;
|
|
|
|
|
|
|
|
$five = 5;
|
|
|
|
continue $five;
|
|
|
|
|
|
|
|
break $random;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-09-26 06:07:16 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-09-26 06:07:16 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($random)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
$five = 5;
|
|
|
|
continue 5;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-09-26 06:07:16 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|