parent
2287695b44
commit
6418099826
3 changed files with 115 additions and 1 deletions
@ -0,0 +1,57 @@ |
||||
--TEST-- |
||||
generator re-yielding array elements via foreach with \Generator return type |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function main() |
||||
{ |
||||
$g = test([1, 2, 3]); |
||||
var_dump($g); |
||||
foreach ($g as $value) |
||||
{ |
||||
var_dump($value); |
||||
} |
||||
} |
||||
|
||||
function test(array $array): \Generator |
||||
{ |
||||
foreach ($array as $value) |
||||
{ |
||||
yield $value; |
||||
} |
||||
} |
||||
|
||||
// main(); |
||||
?> |
||||
--EXPECTF-- |
||||
object(FiberGenerator)#%d (9) { |
||||
["callback":"FiberGenerator":private]=> |
||||
object(Closure)#%d (2) { |
||||
["function"]=> |
||||
string(19) "stdClass::{closure}" |
||||
["this"]=> |
||||
object(stdClass)#%d (1) { |
||||
["box"]=> |
||||
resource(%d) of type (php::box) |
||||
} |
||||
} |
||||
["fiber":"FiberGenerator":private]=> |
||||
NULL |
||||
["current":"FiberGenerator":private]=> |
||||
NULL |
||||
["key":"FiberGenerator":private]=> |
||||
NULL |
||||
["valid":"FiberGenerator":private]=> |
||||
bool(false) |
||||
["state":"FiberGenerator":private]=> |
||||
int(0) |
||||
["yield_count":"FiberGenerator":private]=> |
||||
int(0) |
||||
["next_index":"FiberGenerator":private]=> |
||||
int(0) |
||||
["return_value":"FiberGenerator":private]=> |
||||
NULL |
||||
} |
||||
int(1) |
||||
int(2) |
||||
int(3) |
||||
@ -0,0 +1,53 @@ |
||||
--TEST-- |
||||
generator return type accepts \Generator for methods, nullable and union variants |
||||
--FILE-- |
||||
<?php |
||||
|
||||
class Box |
||||
{ |
||||
public function gen(array $array): \Generator |
||||
{ |
||||
foreach ($array as $value) { |
||||
yield $value * 2; |
||||
} |
||||
} |
||||
} |
||||
|
||||
function nullableGen(array $array): ?\Generator |
||||
{ |
||||
foreach ($array as $value) { |
||||
yield $value; |
||||
} |
||||
} |
||||
|
||||
function unionGen(array $array): \Generator|\Iterator |
||||
{ |
||||
foreach ($array as $value) { |
||||
yield $value; |
||||
} |
||||
} |
||||
|
||||
function main() |
||||
{ |
||||
$b = new Box(); |
||||
foreach ($b->gen([1, 2, 3]) as $v) { |
||||
var_dump($v); |
||||
} |
||||
$g = nullableGen([4, 5]); |
||||
foreach ($g as $v) { |
||||
var_dump($v); |
||||
} |
||||
$u = unionGen([6, 7]); |
||||
foreach ($u as $v) { |
||||
var_dump($v); |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(2) |
||||
int(4) |
||||
int(6) |
||||
int(4) |
||||
int(5) |
||||
int(6) |
||||
int(7) |
||||
Loading…
Reference in new issue