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\CodeQuality\Rector\If_;
|
2018-12-31 11:49:32 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
|
|
|
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\Expression;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
|
|
|
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\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\SimplifyIfElseToTernaryRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyIfElseToTernaryRector extends AbstractRector
|
2018-12-31 11:49:32 +00:00
|
|
|
{
|
2022-04-09 22:42:24 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Printer\BetterStandardPrinter
|
2022-04-09 22:42:24 +00:00
|
|
|
*/
|
2023-06-08 14:23:03 +00:00
|
|
|
private $betterStandardPrinter;
|
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;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private const LINE_LENGTH_LIMIT = 120;
|
2023-09-20 12:55:35 +00:00
|
|
|
public function __construct(BetterStandardPrinter $betterStandardPrinter, BetterNodeFinder $betterNodeFinder)
|
2022-04-09 22:42:24 +00:00
|
|
|
{
|
2023-06-08 14:23:03 +00:00
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2022-04-09 22:42:24 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-31 11:49:32 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes if/else for same value as assign to ternary', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-31 11:49:32 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (empty($value)) {
|
|
|
|
$this->arrayBuilt[][$key] = true;
|
|
|
|
} else {
|
|
|
|
$this->arrayBuilt[][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-31 11:49:32 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->arrayBuilt[][$key] = empty($value) ? true : $value;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-31 11:49:32 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-31 11:49:32 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-31 11:49:32 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2018-12-31 11:49:32 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2018-12-31 11:49:32 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-31 11:49:32 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$node->else instanceof Else_) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-10-29 08:59:39 +00:00
|
|
|
if ($node->elseifs !== []) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 14:00:49 +00:00
|
|
|
$ifAssignVarExpr = $this->resolveOnlyStmtAssignVar($node->stmts);
|
|
|
|
if (!$ifAssignVarExpr instanceof Expr) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 14:00:49 +00:00
|
|
|
$elseAssignExpr = $this->resolveOnlyStmtAssignVar($node->else->stmts);
|
|
|
|
if (!$elseAssignExpr instanceof Expr) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 14:00:49 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($ifAssignVarExpr, $elseAssignExpr)) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 21:28:23 +00:00
|
|
|
$ternaryIfExpr = $this->resolveOnlyStmtAssignExpr($node->stmts);
|
|
|
|
$expr = $this->resolveOnlyStmtAssignExpr($node->else->stmts);
|
|
|
|
if (!$ternaryIfExpr instanceof Expr) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 21:28:23 +00:00
|
|
|
if (!$expr instanceof Expr) {
|
2019-04-01 11:12:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// has nested ternary → skip, it's super hard to read
|
2023-05-26 21:28:23 +00:00
|
|
|
if ($this->haveNestedTernary([$node->cond, $ternaryIfExpr, $expr])) {
|
2019-01-14 18:21:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-26 21:28:23 +00:00
|
|
|
$ternary = new Ternary($node->cond, $ternaryIfExpr, $expr);
|
2023-05-26 14:00:49 +00:00
|
|
|
$assign = new Assign($ifAssignVarExpr, $ternary);
|
2019-04-01 11:04:50 +00:00
|
|
|
// do not create super long lines
|
2019-04-01 11:17:18 +00:00
|
|
|
if ($this->isNodeTooLong($assign)) {
|
2019-04-01 11:04:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$expression = new Expression($assign);
|
2021-05-25 16:38:38 +00:00
|
|
|
$this->mirrorComments($expression, $node);
|
|
|
|
return $expression;
|
2018-12-31 11:49:32 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveOnlyStmtAssignVar(array $stmts) : ?Expr
|
2018-12-31 11:49:32 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($stmts) !== 1) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-28 21:55:05 +00:00
|
|
|
$stmt = $stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmt instanceof Expression) {
|
2022-04-28 21:55:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$stmtExpr = $stmt->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmtExpr instanceof Assign) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-28 21:55:05 +00:00
|
|
|
return $stmtExpr->var;
|
2018-12-31 11:49:32 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveOnlyStmtAssignExpr(array $stmts) : ?Expr
|
2018-12-31 11:49:32 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($stmts) !== 1) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-28 21:55:05 +00:00
|
|
|
$stmt = $stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmt instanceof Expression) {
|
2022-04-28 21:55:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$stmtExpr = $stmt->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmtExpr instanceof Assign) {
|
2018-12-31 11:49:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-28 21:55:05 +00:00
|
|
|
return $stmtExpr->expr;
|
2018-12-31 11:49:32 +00:00
|
|
|
}
|
2019-04-01 11:17:18 +00:00
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function haveNestedTernary(array $nodes) : bool
|
2019-04-01 11:12:20 +00:00
|
|
|
{
|
2019-04-01 11:17:18 +00:00
|
|
|
foreach ($nodes as $node) {
|
2023-05-01 13:51:24 +00:00
|
|
|
$ternary = $this->betterNodeFinder->findFirstInstanceOf($node, Ternary::class);
|
|
|
|
if ($ternary instanceof Ternary) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-04-01 11:17:18 +00:00
|
|
|
}
|
2019-04-01 11:12:20 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-04-01 11:17:18 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isNodeTooLong(Assign $assign) : bool
|
2019-04-01 11:17:18 +00:00
|
|
|
{
|
2023-06-08 14:23:03 +00:00
|
|
|
$assignContent = $this->betterStandardPrinter->print($assign);
|
2023-09-10 14:40:14 +00:00
|
|
|
return \strlen($assignContent) > self::LINE_LENGTH_LIMIT;
|
2019-04-01 11:12:20 +00:00
|
|
|
}
|
2018-12-31 11:49:32 +00:00
|
|
|
}
|