[ 'code' => ' */ function xrange($start, $limit, $step = 1) { for ($i = $start; $i <= $limit; $i += $step) { yield $i; } } $a = null; /* * Note that an array is never created or returned, * which saves memory. */ foreach (xrange(1, 9, 2) as $number) { $a = $number; }', 'assertions' => [ '$a' => 'int|null', ], ], 'generatorReturnType' => [ 'code' => ' */ function g():Generator { yield new stdClass; } $g = g();', 'assertions' => [ '$g' => 'Generator', ], ], 'generatorSend' => [ 'code' => ' */ function g(): Generator { $date = yield 1 => "string"; $date->format("m"); } g()->send(new \DateTime("now")); ', ], 'generatorSendInvalidArgument' => [ 'code' => ' */ function g(): Generator { yield 1 => "string"; } g()->send(1); ', 'assertions' => [], 'ignored_issues' => ['InvalidArgument'], ], 'generatorDelegation' => [ 'code' => ' */ function count_to_ten(): Generator { yield 1; yield 2; yield from [3, 4]; yield from new ArrayIterator([5, 6]); yield from seven_eight(); return yield from nine_ten(); } /** * @return Generator */ function seven_eight(): Generator { yield 7; yield from eight(); } /** * @return Generator */ function eight(): Generator { yield 8; } /** * @return Generator */ function nine_ten(): Generator { yield 9; return 10; } $gen = count_to_ten(); foreach ($gen as $num) { echo "$num "; } $gen2 = $gen->getReturn();', 'assertions' => [ '$gen' => 'Generator', '$gen2' => 'int', ], 'ignored_issues' => ['MixedAssignment'], ], 'yieldFromArray' => [ 'code' => ' */ function Bar() : Generator { yield from [1]; }', ], 'generatorWithNestedYield' => [ 'code' => ' [ 'code' => ' [ 'code' => ' */ function other_generator() : Generator { yield "traffic"; return 1; } /** * @return Generator */ function foo() : Generator { $a = yield from other_generator(); takesInt($a); } foreach (foo() as $s) { takesString($s); }', ], 'expectNonNullableTypeWithYield' => [ 'code' => ' [ 'code' => ' $s * @return Generator */ function foo(iterable $s) : Traversable { yield from $s; }', ], 'yieldWithReturn' => [ 'code' => ' */ function gen(): Generator { yield 3 => "abc"; $foo = 4; return $foo; }', ], 'echoYield' => [ 'code' => ' */ function gen(): Generator { echo yield; }', ], 'SKIPPED-yieldFromTwiceWithVoidSend' => [ 'code' => ' */ function test(): \Generator { return yield "value"; } function load(string $rsa_key): \Generator { echo (yield from test()) . (yield from test()); return 5; }', ], 'iteratorUnion' => [ 'code' => ' [ 'code' => ' [], 'ignored_issues' => ['UndefinedClass'], ], 'fillTemplatesForIteratorFromGenerator' => [ 'code' => ' */ function generator(): Generator { yield "test"; } $iterator = new NoRewindIterator(generator()); ', 'assertions' => [ '$iterator' => 'NoRewindIterator>', ], ], 'detectYieldInNew' => [ 'code' => ' [ '$_a' => 'pure-Closure():Generator', ], ], 'detectYieldInArray' => [ 'code' => ' [ '$_a' => 'pure-Closure():Generator', ], ], ]; } public function providerInvalidCodeParse(): iterable { return [ 'shouldWarnAboutNoGeneratorReturn' => [ 'code' => ' 'InvalidReturnStatement', ], 'expectNonNullableTypeWithNullReturn' => [ 'code' => ' 'NullableReturnStatement', ], 'invalidIterator' => [ 'code' => ' 'InvalidIterator', ], 'rawObjectIteration' => [ 'code' => ' 'RawObjectIteration', ], 'possibleRawObjectIteration' => [ 'code' => ' 5) { $arr[] = new A; } else { $arr = new B; } yield from $arr; }', 'error_message' => 'PossibleRawObjectIteration', ], 'possibleRawObjectIterationFromIsset' => [ 'code' => ' 'PossibleRawObjectIteration', ], ]; } }