[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: SwitchExprsResolver.php
File is not writable. Editing disabled.
<?php declare (strict_types=1); namespace Rector\Php80\NodeResolver; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Case_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\Throw_; use Rector\Php80\Enum\MatchKind; use Rector\Php80\ValueObject\CondAndExpr; final class SwitchExprsResolver { /** * @return CondAndExpr[] */ public function resolve(Switch_ $switch) : array { $newSwitch = clone $switch; $condAndExpr = []; $collectionEmptyCasesCond = []; if (!$this->areCasesValid($newSwitch)) { return []; } $this->moveDefaultCaseToLast($newSwitch); foreach ($newSwitch->cases as $key => $case) { if ($case->stmts !== []) { continue; } if (!$case->cond instanceof Expr) { continue; } $collectionEmptyCasesCond[$key] = $case->cond; } foreach ($newSwitch->cases as $key => $case) { if ($case->stmts === []) { continue; } $expr = $case->stmts[0]; if ($expr instanceof Expression) { $expr = $expr->expr; } $condExprs = []; if ($case->cond instanceof Expr) { $emptyCasesCond = []; foreach ($collectionEmptyCasesCond as $i => $collectionEmptyCaseCond) { if ($i > $key) { break; } $emptyCasesCond[$i] = $collectionEmptyCaseCond; unset($collectionEmptyCasesCond[$i]); } $condExprs = $emptyCasesCond; $condExprs[] = $case->cond; } if ($expr instanceof Return_) { $returnedExpr = $expr->expr; if (!$returnedExpr instanceof Expr) { return []; } $condAndExpr[] = new CondAndExpr($condExprs, $returnedExpr, MatchKind::RETURN); } elseif ($expr instanceof Assign) { $condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::ASSIGN); } elseif ($expr instanceof Expr) { $condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::NORMAL); } elseif ($expr instanceof Throw_) { $throwExpr = new Expr\Throw_($expr->expr); $condAndExpr[] = new CondAndExpr($condExprs, $throwExpr, MatchKind::THROW); } else { return []; } } return $condAndExpr; } private function moveDefaultCaseToLast(Switch_ $switch) : void { foreach ($switch->cases as $key => $case) { if ($case->cond instanceof Expr) { continue; } // not has next? default is at the end, no need move if (!isset($switch->cases[$key + 1])) { return; } // current default has no stmt? keep as is as rely to next case if ($case->stmts === []) { return; } for ($loop = $key - 1; $loop >= 0; --$loop) { if ($switch->cases[$loop]->stmts !== []) { break; } unset($switch->cases[$loop]); } $caseToMove = $switch->cases[$key]; unset($switch->cases[$key]); $switch->cases[] = $caseToMove; break; } } private function isValidCase(Case_ $case) : bool { // prepend to previous one if ($case->stmts === []) { return \true; } if (\count($case->stmts) === 2 && $case->stmts[1] instanceof Break_) { return \true; } // default throws stmts if (\count($case->stmts) !== 1) { return \false; } // throws expressoin if ($case->stmts[0] instanceof Throw_) { return \true; } // instant return if ($case->stmts[0] instanceof Return_) { return \true; } // default value return !$case->cond instanceof Expr; } private function areCasesValid(Switch_ $newSwitch) : bool { foreach ($newSwitch->cases as $case) { if (!$this->isValidCase($case)) { return \false; } } return \true; } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: premium264.web-hosting.com
Server IP: 69.57.162.13
PHP Version: 8.1.34
Server Software: LiteSpeed
System: Linux premium264.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
HDD Total: 97.87 GB
HDD Free: 73.57 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
No
pkexec:
No
git:
Yes
User Info
Username: kidscntx
User ID (UID): 1154
Group ID (GID): 1112
Script Owner UID: 1154
Current Dir Owner: N/A