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\DeadCode\Rector\If_;
|
2019-08-29 20:35:22 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2022-07-27 18:51:59 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2024-04-03 12:13:43 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
2023-03-04 08:51:42 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2024-03-21 11:23:24 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
2022-07-27 18:51:59 +00:00
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
2023-02-22 18:49:35 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
2023-06-05 09:32:57 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\Constant\ConstantBooleanType;
|
2024-03-21 18:38:10 +00:00
|
|
|
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\ExprAnalyzer;
|
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\RemoveAlwaysTrueIfConditionRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveAlwaysTrueIfConditionRector extends AbstractRector
|
2019-08-29 20:35:22 +00:00
|
|
|
{
|
2023-02-22 18:49:35 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\ExprAnalyzer
|
2023-02-22 18:49:35 +00:00
|
|
|
*/
|
|
|
|
private $exprAnalyzer;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2024-03-21 18:38:10 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer
|
|
|
|
*/
|
|
|
|
private $safeLeftTypeBooleanAndOrAnalyzer;
|
2024-04-03 12:13:43 +00:00
|
|
|
public function __construct(ExprAnalyzer $exprAnalyzer, BetterNodeFinder $betterNodeFinder, SafeLeftTypeBooleanAndOrAnalyzer $safeLeftTypeBooleanAndOrAnalyzer)
|
2022-07-27 18:51:59 +00:00
|
|
|
{
|
2023-02-22 18:49:35 +00:00
|
|
|
$this->exprAnalyzer = $exprAnalyzer;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2024-03-21 18:38:10 +00:00
|
|
|
$this->safeLeftTypeBooleanAndOrAnalyzer = $safeLeftTypeBooleanAndOrAnalyzer;
|
2022-07-27 18:51:59 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-08-29 20:35:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove if condition that is always true', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-08-29 20:35:22 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
if (1 === 1) {
|
|
|
|
return 'yes';
|
|
|
|
}
|
2020-11-16 17:50:38 +00:00
|
|
|
|
2019-08-29 20:35:22 +00:00
|
|
|
return 'no';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-08-29 20:35:22 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function go()
|
|
|
|
{
|
|
|
|
return 'yes';
|
2020-11-16 17:50:38 +00:00
|
|
|
|
2019-08-29 20:35:22 +00:00
|
|
|
return 'no';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-08-29 20:35:22 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-08-29 20:35:22 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-08-29 20:35:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2019-08-29 20:35:22 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2024-03-21 11:23:24 +00:00
|
|
|
* @return int|null|Stmt[]|If_
|
2019-08-29 20:35:22 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node)
|
2019-08-29 20:35:22 +00:00
|
|
|
{
|
2024-03-21 11:23:24 +00:00
|
|
|
if ($node->cond instanceof BooleanAnd) {
|
|
|
|
return $this->refactorIfWithBooleanAnd($node);
|
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($node->else instanceof Else_) {
|
2019-08-29 20:35:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// just one if
|
2021-06-03 22:54:09 +00:00
|
|
|
if ($node->elseifs !== []) {
|
2019-08-29 20:35:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 17:46:41 +00:00
|
|
|
$conditionStaticType = $this->getType($node->cond);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$conditionStaticType instanceof ConstantBooleanType) {
|
2019-08-29 20:35:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$conditionStaticType->getValue()) {
|
2019-08-29 20:35:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-03 12:13:43 +00:00
|
|
|
if ($this->shouldSkipExpr($node->cond)) {
|
2022-07-27 18:51:59 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-02-22 18:49:35 +00:00
|
|
|
if ($this->shouldSkipFromParam($node->cond)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-03-04 08:51:42 +00:00
|
|
|
$hasAssign = (bool) $this->betterNodeFinder->findFirstInstanceOf($node->cond, Assign::class);
|
|
|
|
if ($hasAssign) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-28 21:50:14 +00:00
|
|
|
if ($node->stmts === []) {
|
2023-06-05 09:32:57 +00:00
|
|
|
return NodeTraverser::REMOVE_NODE;
|
2019-08-29 20:35:22 +00:00
|
|
|
}
|
2022-04-28 21:50:14 +00:00
|
|
|
return $node->stmts;
|
2019-08-29 20:35:22 +00:00
|
|
|
}
|
2023-02-22 18:49:35 +00:00
|
|
|
private function shouldSkipFromParam(Expr $expr) : bool
|
|
|
|
{
|
|
|
|
/** @var Variable[] $variables */
|
|
|
|
$variables = $this->betterNodeFinder->findInstancesOf($expr, [Variable::class]);
|
|
|
|
foreach ($variables as $variable) {
|
|
|
|
if ($this->exprAnalyzer->isNonTypedFromParam($variable)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2024-04-03 12:13:43 +00:00
|
|
|
private function shouldSkipExpr(Expr $expr) : bool
|
2022-07-27 18:51:59 +00:00
|
|
|
{
|
2024-04-03 12:13:43 +00:00
|
|
|
return (bool) $this->betterNodeFinder->findInstancesOf($expr, [PropertyFetch::class, StaticPropertyFetch::class, ArrayDimFetch::class]);
|
2022-07-27 18:51:59 +00:00
|
|
|
}
|
2024-03-21 11:23:24 +00:00
|
|
|
private function refactorIfWithBooleanAnd(If_ $if) : ?If_
|
|
|
|
{
|
|
|
|
if (!$if->cond instanceof BooleanAnd) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$booleanAnd = $if->cond;
|
|
|
|
$leftType = $this->getType($booleanAnd->left);
|
|
|
|
if (!$leftType instanceof ConstantBooleanType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$leftType->getValue()) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-03-21 18:38:10 +00:00
|
|
|
if (!$this->safeLeftTypeBooleanAndOrAnalyzer->isSafe($booleanAnd)) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-03-21 11:23:24 +00:00
|
|
|
$if->cond = $booleanAnd->right;
|
|
|
|
return $if;
|
|
|
|
}
|
2019-08-29 20:35:22 +00:00
|
|
|
}
|