2020-01-05 21:19:26 +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_ ;
2020-01-05 21:19:26 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\Assign ;
2021-05-06 08:23:37 +00:00
use PhpParser\Node\Stmt ;
2020-12-21 02:12:42 +00:00
use PhpParser\Node\Stmt\Else_ ;
2020-01-05 21:19:26 +00:00
use PhpParser\Node\Stmt\If_ ;
use PhpParser\Node\Stmt\Return_ ;
2020-12-21 02:12:42 +00:00
use Rector\Core\Exception\ShouldNotHappenException ;
2021-02-08 12:33:17 +00:00
use Rector\Core\NodeManipulator\IfManipulator ;
use Rector\Core\NodeManipulator\StmtsManipulator ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2020-01-05 21:19:26 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2020-01-05 21:19:26 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// engineering . helpscout . com / reducing - complexity - with - guard - clauses - in - php - and - javascript - 74600 fd865c7
2020-01-05 21:19:26 +00:00
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector\ChangeIfElseValueAssignToEarlyReturnRectorTest
2020-01-05 21:19:26 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ChangeIfElseValueAssignToEarlyReturnRector extends \Rector\Core\Rector\AbstractRector
2020-01-05 21:19:26 +00:00
{
/**
* @ var IfManipulator
*/
private $ifManipulator ;
/**
* @ var StmtsManipulator
*/
private $stmtsManipulator ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Core\NodeManipulator\IfManipulator $ifManipulator , \Rector\Core\NodeManipulator\StmtsManipulator $stmtsManipulator )
2020-01-05 21:19:26 +00:00
{
$this -> ifManipulator = $ifManipulator ;
$this -> stmtsManipulator = $stmtsManipulator ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-01-05 21:19:26 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change if/else value to early return' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2020-01-05 21:19:26 +00:00
class SomeClass
{
public function run ()
{
if ( $this -> hasDocBlock ( $tokens , $index )) {
$docToken = $tokens [ $this -> getDocBlockIndex ( $tokens , $index )];
} else {
$docToken = null ;
}
return $docToken ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2020-01-05 21:19:26 +00:00
class SomeClass
{
public function run ()
{
if ( $this -> hasDocBlock ( $tokens , $index )) {
return $tokens [ $this -> getDocBlockIndex ( $tokens , $index )];
}
return null ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2020-01-05 21:19:26 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-01-05 21:19:26 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-01-05 21:19:26 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\If_ :: class ];
2020-01-05 21:19:26 +00:00
}
/**
* @ param If_ $node
2021-05-06 08:23:37 +00:00
* @ return Stmt [] | Node | null
2020-01-05 21:19:26 +00:00
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node )
2020-01-05 21:19:26 +00:00
{
2021-05-10 22:23:08 +00:00
$nextNode = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
if ( ! $nextNode instanceof \PhpParser\Node\Stmt\Return_ ) {
2020-01-05 21:19:26 +00:00
return null ;
}
if ( $nextNode -> expr === null ) {
return null ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> ifManipulator -> isIfAndElseWithSameVariableAssignAsLastStmts ( $node , $nextNode -> expr )) {
2020-01-05 21:19:26 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
\end ( $node -> stmts );
$lastIfStmtKey = \key ( $node -> stmts );
2020-01-05 21:19:26 +00:00
/** @var Assign $assign */
$assign = $this -> stmtsManipulator -> getUnwrappedLastStmt ( $node -> stmts );
2021-05-10 22:23:08 +00:00
$return = new \PhpParser\Node\Stmt\Return_ ( $assign -> expr );
2020-12-20 12:56:43 +00:00
$this -> mirrorComments ( $return , $assign );
2020-11-27 16:38:07 +00:00
$node -> stmts [ $lastIfStmtKey ] = $return ;
2020-12-21 02:12:42 +00:00
$else = $node -> else ;
2021-05-10 22:23:08 +00:00
if ( ! $else instanceof \PhpParser\Node\Stmt\Else_ ) {
throw new \Rector\Core\Exception\ShouldNotHappenException ();
2020-12-21 02:12:42 +00:00
}
2020-12-25 00:22:45 +00:00
$elseStmts = $else -> stmts ;
2020-12-21 02:12:42 +00:00
/** @var Assign $assign */
$assign = $this -> stmtsManipulator -> getUnwrappedLastStmt ( $elseStmts );
2021-05-09 20:15:43 +00:00
\end ( $elseStmts );
$lastElseStmtKey = \key ( $elseStmts );
2021-05-10 22:23:08 +00:00
$return = new \PhpParser\Node\Stmt\Return_ ( $assign -> expr );
2020-12-20 12:56:43 +00:00
$this -> mirrorComments ( $return , $assign );
2020-11-27 16:38:07 +00:00
$elseStmts [ $lastElseStmtKey ] = $return ;
2020-01-05 21:19:26 +00:00
$node -> else = null ;
$this -> removeNode ( $nextNode );
2021-05-09 20:15:43 +00:00
return \array_merge ([ $node ], $elseStmts );
2020-01-05 21:19:26 +00:00
}
}