2020-01-12 23:06:46 +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_;
|
2020-01-12 23:06:46 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
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 13:42:47 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\DeadCode\ConditionEvaluator;
|
|
|
|
use Rector\DeadCode\ConditionResolver;
|
|
|
|
use Rector\DeadCode\Contract\ConditionInterface;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-01-12 23:06:46 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://www.php.net/manual/en/function.version-compare.php
|
2020-01-12 23:06:46 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector\UnwrapFutureCompatibleIfPhpVersionRectorTest
|
2020-01-12 23:06:46 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class UnwrapFutureCompatibleIfPhpVersionRector extends AbstractRector
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\DeadCode\ConditionEvaluator
|
2020-01-12 23:06:46 +00:00
|
|
|
*/
|
|
|
|
private $conditionEvaluator;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\DeadCode\ConditionResolver
|
2020-01-12 23:06:46 +00:00
|
|
|
*/
|
|
|
|
private $conditionResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ConditionEvaluator $conditionEvaluator, ConditionResolver $conditionResolver)
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
|
|
|
$this->conditionEvaluator = $conditionEvaluator;
|
|
|
|
$this->conditionResolver = $conditionResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove php version checks if they are passed', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-01-12 23:06:46 +00:00
|
|
|
// current PHP: 7.2
|
|
|
|
if (version_compare(PHP_VERSION, '7.2', '<')) {
|
|
|
|
return 'is PHP 7.1-';
|
|
|
|
} else {
|
|
|
|
return 'is PHP 7.2+';
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-01-12 23:06:46 +00:00
|
|
|
// current PHP: 7.2
|
|
|
|
return 'is PHP 7.2+';
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-01-12 23:06:46 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2023-06-05 13:42:47 +00:00
|
|
|
* @return Stmt[]|null|int
|
2020-01-12 23:06:46 +00:00
|
|
|
*/
|
2023-06-05 13:42:47 +00:00
|
|
|
public function refactor(Node $node)
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
2021-11-28 17:35:55 +00:00
|
|
|
if ($node->elseifs !== []) {
|
2020-01-12 23:06:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$condition = $this->conditionResolver->resolveFromExpr($node->cond);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$condition instanceof ConditionInterface) {
|
2020-01-12 23:06:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$result = $this->conditionEvaluator->evaluate($condition);
|
|
|
|
if ($result === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// if is skipped
|
2021-11-28 19:07:44 +00:00
|
|
|
if ($result === \true) {
|
|
|
|
return $this->refactorIsMatch($node);
|
|
|
|
}
|
|
|
|
if ($result > 0) {
|
2021-05-16 16:22:09 +00:00
|
|
|
return $this->refactorIsMatch($node);
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
2021-05-16 16:22:09 +00:00
|
|
|
return $this->refactorIsNotMatch($node);
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
2021-05-16 16:22:09 +00:00
|
|
|
/**
|
|
|
|
* @return Stmt[]|null
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorIsMatch(If_ $if) : ?array
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
2021-11-28 17:35:55 +00:00
|
|
|
if ($if->elseifs !== []) {
|
2021-05-16 16:22:09 +00:00
|
|
|
return null;
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
2021-05-16 16:22:09 +00:00
|
|
|
return $if->stmts;
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
2021-05-16 16:22:09 +00:00
|
|
|
/**
|
2023-06-05 13:42:47 +00:00
|
|
|
* @return Stmt[]|int
|
2021-05-16 16:22:09 +00:00
|
|
|
*/
|
2023-06-05 13:42:47 +00:00
|
|
|
private function refactorIsNotMatch(If_ $if)
|
2020-01-12 23:06:46 +00:00
|
|
|
{
|
2020-02-01 16:04:38 +00:00
|
|
|
// no else → just remove the node
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$if->else instanceof Else_) {
|
2023-06-05 13:42:47 +00:00
|
|
|
return NodeTraverser::REMOVE_NODE;
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
2020-02-01 16:04:38 +00:00
|
|
|
// else is always used
|
2021-05-16 16:22:09 +00:00
|
|
|
return $if->else->stmts;
|
2020-01-12 23:06:46 +00:00
|
|
|
}
|
|
|
|
}
|