2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
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 ;
2021-09-24 14:11:08 +00:00
use Rector\Core\ValueObject\PhpVersionFeature ;
use Rector\VersionBonding\Contract\MinPhpVersionInterface ;
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
/**
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
*/
2021-09-24 14:11:08 +00:00
final class RemoveZeroBreakContinueRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
2019-09-26 06:07:16 +00:00
{
2021-09-24 14:11:08 +00:00
public function provideMinPhpVersion () : int
{
return \Rector\Core\ValueObject\PhpVersionFeature :: NO_ZERO_BREAK ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-09-26 06:07:16 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Remove 0 from break and continue' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\Break_ :: class , \PhpParser\Node\Stmt\Continue_ :: class ];
2019-09-26 06:07:16 +00:00
}
/**
* @ param Break_ | Continue_ $node
*/
2021-07-05 22:50:18 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-09-26 06:07:16 +00:00
{
if ( $node -> num === null ) {
return null ;
}
2021-05-10 22:23:08 +00:00
if ( $node -> num instanceof \PhpParser\Node\Scalar\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 ;
}
2021-05-10 22:23:08 +00:00
if ( $node -> num instanceof \PhpParser\Node\Expr\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
*/
2021-06-29 14:24:45 +00:00
private function processVariableNum ( $stmt , \PhpParser\Node\Expr\Variable $numVariable ) : ? \PhpParser\Node
2019-09-26 06:07:16 +00:00
{
$staticType = $this -> getStaticType ( $numVariable );
2021-05-10 22:23:08 +00:00
if ( $staticType instanceof \PHPStan\Type\ConstantType ) {
if ( $staticType instanceof \PHPStan\Type\Constant\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 ) {
2021-05-10 22:23:08 +00:00
$stmt -> num = new \PhpParser\Node\Scalar\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 ;
}
}