2019-12-29 21:11:24 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-12-12 16:59:09 +00:00
namespace Rector\EarlyReturn\Rector\If_ ;
2019-12-29 21:11:24 +00:00
use PhpParser\Node ;
2020-01-15 20:13:29 +00:00
use PhpParser\Node\Expr\Exit_ ;
use PhpParser\Node\Stmt\Continue_ ;
2021-06-16 17:49:09 +00:00
use PhpParser\Node\Stmt\Else_ ;
2020-01-15 20:13:29 +00:00
use PhpParser\Node\Stmt\ElseIf_ ;
use PhpParser\Node\Stmt\Expression ;
2019-12-29 21:11:24 +00:00
use PhpParser\Node\Stmt\If_ ;
2020-01-15 20:13:29 +00:00
use PhpParser\Node\Stmt\Return_ ;
use PhpParser\Node\Stmt\Throw_ ;
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-12-29 21:11:24 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\RemoveAlwaysElseRectorTest
2019-12-29 21:11:24 +00:00
*/
2021-05-10 22:23:08 +00:00
final class RemoveAlwaysElseRector extends \Rector\Core\Rector\AbstractRector
2019-12-29 21:11:24 +00:00
{
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-12-29 21:11:24 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Split if statement, when if condition always break execution flow' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2019-12-29 21:11:24 +00:00
class SomeClass
{
public function run ( $value )
{
if ( $value ) {
2019-12-29 21:55:54 +00:00
throw new \InvalidStateException ;
2019-12-29 21:11:24 +00:00
} else {
return 10 ;
}
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-12-29 21:11:24 +00:00
class SomeClass
{
public function run ( $value )
{
if ( $value ) {
2019-12-29 21:55:54 +00:00
throw new \InvalidStateException ;
2019-12-29 21:11:24 +00:00
}
return 10 ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-12-29 21:11:24 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-12-29 21:11:24 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-12-29 21:11:24 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\If_ :: class ];
2019-12-29 21:11:24 +00:00
}
/**
2019-12-29 21:55:54 +00:00
* @ param If_ $node
2019-12-29 21:11:24 +00:00
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-12-29 21:11:24 +00:00
{
2020-03-28 23:06:05 +00:00
if ( $this -> doesLastStatementBreakFlow ( $node )) {
2019-12-29 21:55:54 +00:00
return null ;
}
2020-01-15 20:13:29 +00:00
if ( $node -> elseifs !== []) {
2021-06-16 17:49:09 +00:00
$originalNode = clone $node ;
2021-05-10 22:23:08 +00:00
$if = new \PhpParser\Node\Stmt\If_ ( $node -> cond );
2020-06-29 21:19:37 +00:00
$if -> stmts = $node -> stmts ;
$this -> addNodeBeforeNode ( $if , $node );
2021-06-16 17:49:09 +00:00
$this -> mirrorComments ( $if , $node );
2020-01-15 20:13:29 +00:00
/** @var ElseIf_ $firstElseIf */
2021-05-09 20:15:43 +00:00
$firstElseIf = \array_shift ( $node -> elseifs );
2020-01-15 20:13:29 +00:00
$node -> cond = $firstElseIf -> cond ;
$node -> stmts = $firstElseIf -> stmts ;
2020-12-20 12:56:43 +00:00
$this -> mirrorComments ( $node , $firstElseIf );
2021-06-16 17:49:09 +00:00
$statements = $this -> getStatementsElseIfs ( $node );
if ( $statements !== []) {
$this -> addNodesAfterNode ( $statements , $node );
}
if ( $originalNode -> else instanceof \PhpParser\Node\Stmt\Else_ ) {
$node -> else = null ;
$this -> addNodeAfterNode ( $originalNode -> else , $node );
}
2020-01-15 20:13:29 +00:00
return $node ;
}
if ( $node -> else !== null ) {
2020-12-25 00:22:45 +00:00
$this -> addNodesAfterNode ( $node -> else -> stmts , $node );
2020-01-15 20:13:29 +00:00
$node -> else = null ;
return $node ;
2019-12-29 21:55:54 +00:00
}
2020-01-15 20:13:29 +00:00
return null ;
}
2021-06-16 17:49:09 +00:00
/**
* @ return ElseIf_ []
*/
private function getStatementsElseIfs ( \PhpParser\Node\Stmt\If_ $if ) : array
{
$statements = [];
foreach ( $if -> elseifs as $key => $elseif ) {
if ( $this -> doesLastStatementBreakFlow ( $elseif ) && $elseif -> stmts !== []) {
continue ;
}
$statements [] = $elseif ;
unset ( $if -> elseifs [ $key ]);
}
return $statements ;
}
/**
* @ param If_ | ElseIf_ $node
*/
private function doesLastStatementBreakFlow ( \PhpParser\Node $node ) : bool
2020-01-15 20:13:29 +00:00
{
2021-06-16 17:49:09 +00:00
$lastStmt = \end ( $node -> stmts );
2021-05-10 22:23:08 +00:00
return ! ( $lastStmt instanceof \PhpParser\Node\Stmt\Return_ || $lastStmt instanceof \PhpParser\Node\Stmt\Throw_ || $lastStmt instanceof \PhpParser\Node\Stmt\Continue_ || $lastStmt instanceof \PhpParser\Node\Stmt\Expression && $lastStmt -> expr instanceof \PhpParser\Node\Expr\Exit_ );
2019-12-29 21:11:24 +00:00
}
}