REDROOM
PHP 8.2.31
Path:
Logout
Edit File
Size: 2.45 KB
Close
//opt/cpanel/ea-wappspector/vendor/rector/rector/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php
Text
Base64
<?php declare (strict_types=1); namespace Rector\DeadCode\Rector\If_; use PhpParser\Node; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; use Rector\Exception\ShouldNotHappenException; use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector\SimplifyIfElseWithSameContentRectorTest */ final class SimplifyIfElseWithSameContentRector extends AbstractRector { /** * @readonly * @var \Rector\PhpParser\Printer\BetterStandardPrinter */ private $betterStandardPrinter; public function __construct(BetterStandardPrinter $betterStandardPrinter) { $this->betterStandardPrinter = $betterStandardPrinter; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Remove if/else if they have same content', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { if (true) { return 1; } else { return 1; } } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { return 1; } } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [If_::class]; } /** * @param If_ $node * @return Stmt[]|null */ public function refactor(Node $node) : ?array { if (!$node->else instanceof Else_) { return null; } if (!$this->isIfWithConstantReturns($node)) { return null; } return $node->stmts; } private function isIfWithConstantReturns(If_ $if) : bool { $possibleContents = []; $possibleContents[] = $this->betterStandardPrinter->print($if->stmts); foreach ($if->elseifs as $elseif) { $possibleContents[] = $this->betterStandardPrinter->print($elseif->stmts); } $else = $if->else; if (!$else instanceof Else_) { throw new ShouldNotHappenException(); } $possibleContents[] = $this->betterStandardPrinter->print($else->stmts); $uniqueContents = \array_unique($possibleContents); // only one content for all return \count($uniqueContents) === 1; } }
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 7
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
ReduceAlwaysFalseIfOrRector.php
2.26 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
RemoveAlwaysTrueIfConditionRector.php
5.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
RemoveDeadInstanceOfRector.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
RemoveTypedPropertyDeadInstanceOfRector.php
6.48 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
RemoveUnusedNonEmptyArrayBeforeForeachRector.php
7.79 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
SimplifyIfElseWithSameContentRector.php
2.45 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
UnwrapFutureCompatibleIfPhpVersionRector.php
2.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
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).