2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php54\Rector\Break_;
|
2019-09-26 06:07:16 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
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;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-26 06:07:16 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://www.php.net/manual/en/control-structures.continue.php https://www.php.net/manual/en/control-structures.break.php
|
2019-09-26 06:07:16 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php54\Rector\Break_\RemoveZeroBreakContinueRector\RemoveZeroBreakContinueRectorTest
|
2019-09-26 06:07:16 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveZeroBreakContinueRector extends AbstractRector implements MinPhpVersionInterface
|
2019-09-26 06:07:16 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NO_ZERO_BREAK;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-09-26 06:07:16 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove 0 from break and continue', [new CodeSample(<<<'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
|
2021-05-09 20:15:43 +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
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-09-26 06:07:16 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-09-26 06:07:16 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-09-26 06:07:16 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Break_::class, Continue_::class];
|
2019-09-26 06:07:16 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Break_|Continue_ $node
|
2019-09-26 06:07:16 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-09-26 06:07:16 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$node->num instanceof Expr) {
|
2019-09-26 06:07:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->num instanceof LNumber) {
|
2021-01-30 23:20:05 +00:00
|
|
|
$number = $this->valueResolver->getValue($node->num);
|
2019-09-26 06:07:16 +00:00
|
|
|
if ($number > 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($number === 0) {
|
|
|
|
$node->num = null;
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->num instanceof Variable) {
|
2019-09-26 06:07:16 +00:00
|
|
|
return $this->processVariableNum($node, $node->num);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Break_|\PhpParser\Node\Stmt\Continue_ $stmt
|
2019-09-26 06:07:16 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processVariableNum($stmt, Variable $numVariable) : ?Node
|
2019-09-26 06:07:16 +00:00
|
|
|
{
|
2021-10-07 17:46:41 +00:00
|
|
|
$staticType = $this->getType($numVariable);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($staticType instanceof ConstantType) {
|
|
|
|
if ($staticType instanceof ConstantIntegerType) {
|
2019-09-26 06:07:16 +00:00
|
|
|
if ($staticType->getValue() === 0) {
|
2021-03-07 14:27:17 +00:00
|
|
|
$stmt->num = null;
|
|
|
|
return $stmt;
|
2019-09-26 06:07:16 +00:00
|
|
|
}
|
|
|
|
if ($staticType->getValue() > 0) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$stmt->num = new LNumber($staticType->getValue());
|
2021-03-07 14:27:17 +00:00
|
|
|
return $stmt;
|
2019-09-26 06:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-07 14:27:17 +00:00
|
|
|
return $stmt;
|
2019-09-26 06:07:16 +00:00
|
|
|
}
|
|
|
|
// remove variable
|
2021-03-07 14:27:17 +00:00
|
|
|
$stmt->num = null;
|
2019-09-26 06:07:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|