diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3bd7252d..52c88080 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3398,11 +3398,11 @@ class CompilerBase extends \PhpAot\Core\Translator $code = 'do {' . PHP_EOL; $this->indentLevel++; $condList = []; + $defaultCase = false; + $defaultOnly = true; foreach ($v->cases as $case) { if (empty($case->cond)) { - if ($condList) { - $this->fatalError($case, 'switch case must be first'); - } + $defaultCase = true; } else { $condList[] = $tmp_var . '==' . $this->parseIdentifier($case->cond); } @@ -3423,11 +3423,13 @@ class CompilerBase extends \PhpAot\Core\Translator $this->fatalError($case, 'switch case must end with return/break/exit/throw, ' . $lastExpr->getType() . ' given'); } - if ($condList) { + if ($defaultCase) { + $else = $defaultOnly ? '' : 'else'; + $code .= $this->getIndent() . $else . ' {' . PHP_EOL; + } else { $code .= $this->getIndent() . 'if (' . implode(' || ', $condList) . ') {' . PHP_EOL; + $defaultOnly = false; $condList = []; - } else { - $code .= $this->getIndent() . 'else {' . PHP_EOL; } $code .= $this->parseStmts($stmts); diff --git a/tests/aot/switch-001.phpt b/tests/aot/switch/001.phpt similarity index 100% rename from tests/aot/switch-001.phpt rename to tests/aot/switch/001.phpt diff --git a/tests/aot/switch-002.phpt b/tests/aot/switch/002.phpt similarity index 100% rename from tests/aot/switch-002.phpt rename to tests/aot/switch/002.phpt diff --git a/tests/aot/switch/003.phpt b/tests/aot/switch/003.phpt new file mode 100644 index 00000000..ccdff2fb --- /dev/null +++ b/tests/aot/switch/003.phpt @@ -0,0 +1,18 @@ +--TEST-- +switch +--FILE-- + +--EXPECT-- +default diff --git a/tests/aot/switch/004.phpt b/tests/aot/switch/004.phpt new file mode 100644 index 00000000..34d3a2d9 --- /dev/null +++ b/tests/aot/switch/004.phpt @@ -0,0 +1,27 @@ +--TEST-- +switch +--FILE-- + +--EXPECT-- +case 1 +default