Merge pull request '修复三元表达式 issue' (#45) from fix-bool-ternary-expr into master
Reviewed-on: #45pull/47/head
commit
dbd12d249d
4 changed files with 109 additions and 8 deletions
@ -0,0 +1,27 @@ |
||||
--TEST-- |
||||
Ternary with captured statements materializes reference returns as values |
||||
--FILE-- |
||||
<?php |
||||
function &ternary_ref_value(mixed &$value): mixed |
||||
{ |
||||
return $value; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$first = 1; |
||||
$second = 2; |
||||
$values = [1]; |
||||
|
||||
$result = count($values) > 0 |
||||
? ternary_ref_value($first) |
||||
: ternary_ref_value($second); |
||||
$result = 9; |
||||
|
||||
var_dump($first, $second, $result); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(1) |
||||
int(2) |
||||
int(9) |
||||
@ -0,0 +1,29 @@ |
||||
--TEST-- |
||||
Ternary with captured statements keeps its static type for typed arguments |
||||
--FILE-- |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
class TernaryBoolArg |
||||
{ |
||||
public function takeBool(bool $flag): bool |
||||
{ |
||||
return $flag; |
||||
} |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$range = [1, 2, 3]; |
||||
$obj = new TernaryBoolArg(); |
||||
|
||||
// The condition contains a function call, which forces the ternary into a |
||||
// captured-statement lambda. The lambda must still yield php::Bool so it |
||||
// can feed the typed parameter. |
||||
var_dump($obj->takeBool(count($range) > 1 ? true : false)); |
||||
var_dump($obj->takeBool(count($range) > 5 ? true : false)); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
bool(true) |
||||
bool(false) |
||||
@ -0,0 +1,26 @@ |
||||
--TEST-- |
||||
Ternary with captured statements preserves void expression semantics |
||||
--FILE-- |
||||
<?php |
||||
function ternary_void_side_effect(string $value): void |
||||
{ |
||||
echo $value, "\n"; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$values = [1, 2]; |
||||
|
||||
var_dump(count($values) > 1 |
||||
? ternary_void_side_effect('if') |
||||
: ternary_void_side_effect('else')); |
||||
var_dump(count($values) > 5 |
||||
? ternary_void_side_effect('if') |
||||
: ternary_void_side_effect('else')); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
if |
||||
NULL |
||||
else |
||||
NULL |
||||
Loading…
Reference in new issue