2020-12-19 08:56:24 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-12-19 08:56:24 +00:00
namespace Rector\EarlyReturn\Rector\Return_ ;
use PhpParser\Node ;
use PhpParser\Node\Expr ;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd ;
use PhpParser\Node\Stmt\If_ ;
use PhpParser\Node\Stmt\Return_ ;
2021-02-25 08:41:43 +00:00
use Rector\Core\NodeAnalyzer\CallAnalyzer ;
2021-02-09 22:43:04 +00:00
use Rector\Core\NodeManipulator\IfManipulator ;
2021-02-25 08:41:43 +00:00
use Rector\Core\PhpParser\Node\AssignAndBinaryMap ;
2020-12-19 08:56:24 +00:00
use Rector\Core\Rector\AbstractRector ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector\ReturnBinaryAndToEarlyReturnRectorTest
2020-12-19 08:56:24 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ReturnBinaryAndToEarlyReturnRector extends \Rector\Core\Rector\AbstractRector
2020-12-19 08:56:24 +00:00
{
2021-02-09 19:07:20 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\NodeManipulator\IfManipulator
2021-02-09 19:07:20 +00:00
*/
private $ifManipulator ;
2021-02-25 08:41:43 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\PhpParser\Node\AssignAndBinaryMap
2021-02-25 08:41:43 +00:00
*/
private $assignAndBinaryMap ;
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\NodeAnalyzer\CallAnalyzer
2021-02-25 08:41:43 +00:00
*/
private $callAnalyzer ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Core\NodeManipulator\IfManipulator $ifManipulator , \Rector\Core\PhpParser\Node\AssignAndBinaryMap $assignAndBinaryMap , \Rector\Core\NodeAnalyzer\CallAnalyzer $callAnalyzer )
2021-05-09 20:15:43 +00:00
{
2021-02-09 19:07:20 +00:00
$this -> ifManipulator = $ifManipulator ;
2021-02-25 08:41:43 +00:00
$this -> assignAndBinaryMap = $assignAndBinaryMap ;
$this -> callAnalyzer = $callAnalyzer ;
2021-02-09 19:07:20 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-12-19 08:56:24 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Changes Single return of && to early returns' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2020-12-19 08:56:24 +00:00
class SomeClass
{
2021-02-25 08:41:43 +00:00
public function accept ()
2020-12-19 08:56:24 +00:00
{
2021-02-25 08:41:43 +00:00
return $this -> something () && $this -> somethingelse ();
2020-12-19 08:56:24 +00:00
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2020-12-19 08:56:24 +00:00
class SomeClass
{
2021-02-25 08:41:43 +00:00
public function accept ()
2020-12-19 08:56:24 +00:00
{
2021-02-25 08:41:43 +00:00
if ( ! $this -> something ()) {
2020-12-19 08:56:24 +00:00
return false ;
}
2021-02-25 08:41:43 +00:00
return ( bool ) $this -> somethingelse ();
2020-12-19 08:56:24 +00:00
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2020-12-19 08:56:24 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-12-19 08:56:24 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-12-19 08:56:24 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\Return_ :: class ];
2020-12-19 08:56:24 +00:00
}
/**
2021-12-10 09:57:54 +00:00
* @ param \PhpParser\Node $node
2021-09-09 02:08:49 +00:00
* @ return null | Node []
2020-12-19 08:56:24 +00:00
*/
2021-12-10 09:57:54 +00:00
public function refactor ( $node ) : ? array
2020-12-19 08:56:24 +00:00
{
2021-05-10 22:23:08 +00:00
if ( ! $node -> expr instanceof \PhpParser\Node\Expr\BinaryOp\BooleanAnd ) {
2020-12-19 08:56:24 +00:00
return null ;
}
$left = $node -> expr -> left ;
$ifNegations = $this -> createMultipleIfsNegation ( $left , $node , []);
2021-09-09 02:08:49 +00:00
// ensure ifs not removed by other rules
if ( $ifNegations === []) {
return null ;
}
2021-08-13 20:43:09 +00:00
if ( ! $this -> callAnalyzer -> doesIfHasObjectCall ( $ifNegations )) {
return null ;
}
2021-02-25 08:41:43 +00:00
$this -> mirrorComments ( $ifNegations [ 0 ], $node );
2021-04-04 09:01:11 +00:00
/** @var BooleanAnd $booleanAnd */
$booleanAnd = $node -> expr ;
$lastReturnExpr = $this -> assignAndBinaryMap -> getTruthyExpr ( $booleanAnd -> right );
2021-09-09 02:08:49 +00:00
return \array_merge ( $ifNegations , [ new \PhpParser\Node\Stmt\Return_ ( $lastReturnExpr )]);
2020-12-19 08:56:24 +00:00
}
/**
* @ param If_ [] $ifNegations
* @ return If_ []
*/
2021-05-10 22:23:08 +00:00
private function createMultipleIfsNegation ( \PhpParser\Node\Expr $expr , \PhpParser\Node\Stmt\Return_ $return , array $ifNegations ) : array
2020-12-19 08:56:24 +00:00
{
2021-05-10 22:23:08 +00:00
while ( $expr instanceof \PhpParser\Node\Expr\BinaryOp\BooleanAnd ) {
2021-05-09 20:15:43 +00:00
$ifNegations = \array_merge ( $ifNegations , $this -> collectLeftBooleanAndToIfs ( $expr , $return , $ifNegations ));
2021-05-10 22:23:08 +00:00
$ifNegations [] = $this -> ifManipulator -> createIfNegation ( $expr -> right , new \PhpParser\Node\Stmt\Return_ ( $this -> nodeFactory -> createFalse ()));
2020-12-19 08:56:24 +00:00
$expr = $expr -> right ;
}
2021-05-10 22:23:08 +00:00
return $ifNegations + [ $this -> ifManipulator -> createIfNegation ( $expr , new \PhpParser\Node\Stmt\Return_ ( $this -> nodeFactory -> createFalse ()))];
2020-12-19 08:56:24 +00:00
}
/**
* @ param If_ [] $ifNegations
* @ return If_ []
*/
2021-05-10 22:23:08 +00:00
private function collectLeftBooleanAndToIfs ( \PhpParser\Node\Expr\BinaryOp\BooleanAnd $booleanAnd , \PhpParser\Node\Stmt\Return_ $return , array $ifNegations ) : array
2020-12-19 08:56:24 +00:00
{
$left = $booleanAnd -> left ;
2021-05-10 22:23:08 +00:00
if ( ! $left instanceof \PhpParser\Node\Expr\BinaryOp\BooleanAnd ) {
return [ $this -> ifManipulator -> createIfNegation ( $left , new \PhpParser\Node\Stmt\Return_ ( $this -> nodeFactory -> createFalse ()))];
2020-12-19 08:56:24 +00:00
}
return $this -> createMultipleIfsNegation ( $left , $return , $ifNegations );
}
}