REDROOM
PHP 8.2.31
Path:
Logout
Edit File
Size: 4.91 KB
Close
/opt/cpanel/ea-wappspector/vendor/rector/rector/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php
Text
Base64
<?php declare (strict_types=1); namespace Rector\CodeQuality\Rector\If_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector\SimplifyIfElseToTernaryRectorTest */ final class SimplifyIfElseToTernaryRector extends AbstractRector { /** * @readonly * @var \Rector\PhpParser\Printer\BetterStandardPrinter */ private $betterStandardPrinter; /** * @readonly * @var \Rector\PhpParser\Node\BetterNodeFinder */ private $betterNodeFinder; /** * @var int */ private const LINE_LENGTH_LIMIT = 120; public function __construct(BetterStandardPrinter $betterStandardPrinter, BetterNodeFinder $betterNodeFinder) { $this->betterStandardPrinter = $betterStandardPrinter; $this->betterNodeFinder = $betterNodeFinder; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Changes if/else for same value as assign to ternary', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { if (empty($value)) { $this->arrayBuilt[][$key] = true; } else { $this->arrayBuilt[][$key] = $value; } } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { $this->arrayBuilt[][$key] = empty($value) ? true : $value; } } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [If_::class]; } /** * @param If_ $node */ public function refactor(Node $node) : ?Node { if (!$node->else instanceof Else_) { return null; } if ($node->elseifs !== []) { return null; } $ifAssignVarExpr = $this->resolveOnlyStmtAssignVar($node->stmts); if (!$ifAssignVarExpr instanceof Expr) { return null; } $elseAssignExpr = $this->resolveOnlyStmtAssignVar($node->else->stmts); if (!$elseAssignExpr instanceof Expr) { return null; } if (!$this->nodeComparator->areNodesEqual($ifAssignVarExpr, $elseAssignExpr)) { return null; } $ternaryIfExpr = $this->resolveOnlyStmtAssignExpr($node->stmts); $expr = $this->resolveOnlyStmtAssignExpr($node->else->stmts); if (!$ternaryIfExpr instanceof Expr) { return null; } if (!$expr instanceof Expr) { return null; } // has nested ternary → skip, it's super hard to read if ($this->haveNestedTernary([$node->cond, $ternaryIfExpr, $expr])) { return null; } $ternary = new Ternary($node->cond, $ternaryIfExpr, $expr); $assign = new Assign($ifAssignVarExpr, $ternary); // do not create super long lines if ($this->isNodeTooLong($assign)) { return null; } $expression = new Expression($assign); $this->mirrorComments($expression, $node); return $expression; } /** * @param Stmt[] $stmts */ private function resolveOnlyStmtAssignVar(array $stmts) : ?Expr { if (\count($stmts) !== 1) { return null; } $stmt = $stmts[0]; if (!$stmt instanceof Expression) { return null; } $stmtExpr = $stmt->expr; if (!$stmtExpr instanceof Assign) { return null; } return $stmtExpr->var; } /** * @param Stmt[] $stmts */ private function resolveOnlyStmtAssignExpr(array $stmts) : ?Expr { if (\count($stmts) !== 1) { return null; } $stmt = $stmts[0]; if (!$stmt instanceof Expression) { return null; } $stmtExpr = $stmt->expr; if (!$stmtExpr instanceof Assign) { return null; } return $stmtExpr->expr; } /** * @param Node[] $nodes */ private function haveNestedTernary(array $nodes) : bool { foreach ($nodes as $node) { $ternary = $this->betterNodeFinder->findFirstInstanceOf($node, Ternary::class); if ($ternary instanceof Ternary) { return \true; } } return \false; } private function isNodeTooLong(Assign $assign) : bool { $assignContent = $this->betterStandardPrinter->print($assign); return \strlen($assignContent) > self::LINE_LENGTH_LIMIT; } }
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 9
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
CombineIfRector.php
3.19 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
CompleteMissingIfElseBracketRector.php
3.04 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php
5.16 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
ExplicitBoolCompareRector.php
9.05 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
ShortenElseIfRector.php
2.57 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyIfElseToTernaryRector.php
4.91 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyIfNotNullReturnRector.php
2.80 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyIfNullableReturnRector.php
8.33 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
SimplifyIfReturnBoolRector.php
7.49 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).