fix(parser): 支持判断 if/else 终止的 switch case #34
Closed
yurunsoft
wants to merge 1 commits from parser-support-if-else-terminated-switch-case into master
10 changed files with 276 additions and 6 deletions
@ -0,0 +1,20 @@ |
|||||||
|
--TEST-- |
||||||
|
default placed before a later matching case, both using if/else return |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 2; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
default: |
||||||
|
if (true) { echo "D\n"; return; } else { echo "De\n"; return; } |
||||||
|
case 1: |
||||||
|
if (true) { echo "1\n"; return; } else { echo "1e\n"; return; } |
||||||
|
case 2: |
||||||
|
if (true) { echo "2\n"; return; } else { echo "2e\n"; return; } |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
2 |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
--TEST-- |
||||||
|
switch case ending with if/else where both branches return (regression) |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 1; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
if (true) |
||||||
|
{ |
||||||
|
var_dump('a'); |
||||||
|
return; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
var_dump('b'); |
||||||
|
return; |
||||||
|
} |
||||||
|
default: |
||||||
|
if (true) |
||||||
|
{ |
||||||
|
var_dump('c'); |
||||||
|
return; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
var_dump('d'); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(1) "c" |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
--TEST-- |
||||||
|
switch case matching with if/else return, default not taken |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 0; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
if (true) |
||||||
|
{ |
||||||
|
var_dump('a'); |
||||||
|
return; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
var_dump('b'); |
||||||
|
return; |
||||||
|
} |
||||||
|
default: |
||||||
|
if (true) |
||||||
|
{ |
||||||
|
var_dump('c'); |
||||||
|
return; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
var_dump('d'); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(1) "a" |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
--TEST-- |
||||||
|
switch without default, if/else return case, non-matching value falls through |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 9; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 1: |
||||||
|
if (true) { echo "1\n"; return; } else { echo "1e\n"; return; } |
||||||
|
case 2: |
||||||
|
if (true) { echo "2\n"; return; } else { echo "2e\n"; return; } |
||||||
|
} |
||||||
|
echo "after\n"; |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
after |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
--TEST-- |
||||||
|
switch case ending with if/elseif/else where every branch returns |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 2; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 1: |
||||||
|
if (true) { echo "1\n"; return; } else { echo "1e\n"; return; } |
||||||
|
case 2: |
||||||
|
if ($a === 2) { echo "two\n"; return; } elseif ($a === 3) { echo "three\n"; return; } else { echo "other\n"; return; } |
||||||
|
default: |
||||||
|
if (true) { echo "D\n"; return; } else { echo "De\n"; return; } |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
two |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
--TEST-- |
||||||
|
switch mixing plain return case with if/else return case |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 2; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 1: |
||||||
|
echo "1\n"; return; |
||||||
|
case 2: |
||||||
|
if (true) { echo "2if\n"; return; } else { echo "2else\n"; return; } |
||||||
|
default: |
||||||
|
echo "D\n"; return; |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
2if |
||||||
@ -0,0 +1,22 @@ |
|||||||
|
--TEST-- |
||||||
|
switch case ending with nested if/else where all paths return |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 5; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 5: |
||||||
|
if (true) { |
||||||
|
if ($a > 0) { echo "inner\n"; return; } else { echo "inner-else\n"; return; } |
||||||
|
} else { |
||||||
|
echo "outer-else\n"; return; |
||||||
|
} |
||||||
|
default: |
||||||
|
if (true) { echo "D\n"; return; } else { echo "De\n"; return; } |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
inner |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
--TEST-- |
||||||
|
multiple case labels sharing an if/else return body |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = 1; |
||||||
|
switch ($a) |
||||||
|
{ |
||||||
|
case 1: |
||||||
|
case 2: |
||||||
|
if (true) { echo "one-or-two\n"; return; } else { echo "never\n"; return; } |
||||||
|
default: |
||||||
|
if (true) { echo "D\n"; return; } else { echo "De\n"; return; } |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
one-or-two |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
--TEST-- |
||||||
|
string switch with if/else return cases and default |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$v = 'x'; |
||||||
|
switch ($v) |
||||||
|
{ |
||||||
|
case 'a': |
||||||
|
if (true) { echo "A\n"; return; } else { echo "a\n"; return; } |
||||||
|
case 'b': |
||||||
|
if (true) { echo "B\n"; return; } else { echo "b\n"; return; } |
||||||
|
default: |
||||||
|
if (true) { echo "DEFAULT\n"; return; } else { echo "default\n"; return; } |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
DEFAULT |
||||||
Loading…
Reference in new issue