forked from vimeo/psalm
/
GeneratorTest.php
414 lines (383 loc) · 14.1 KB
/
GeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
namespace Psalm\Tests;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class GeneratorTest extends TestCase
{
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
/**
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
*/
public function providerValidCodeParse(): iterable
{
return [
'generator' => [
'code' => '<?php
/**
* @param int $start
* @param int $limit
* @param int $step
* @return Generator<int>
*/
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' => '<?php
/** @return Generator<int, stdClass> */
function g():Generator { yield new stdClass; }
$g = g();',
'assertions' => [
'$g' => 'Generator<int, stdClass, mixed, mixed>',
],
],
'generatorWithReturn' => [
'code' => '<?php
/**
* @return Generator<int,int>
* @psalm-generator-return string
*/
function fooFoo(int $i): Generator {
if ($i === 1) {
return "bash";
}
yield 1;
}',
],
'generatorSend' => [
'code' => '<?php
/** @return Generator<int, string, DateTimeInterface, void> */
function g(): Generator {
$date = yield 1 => "string";
$date->format("m");
}
g()->send(new \DateTime("now"));
',
],
'generatorSendInvalidArgument' => [
'code' => '<?php
/** @return Generator<int, string, DateTimeInterface, void> */
function g(): Generator {
yield 1 => "string";
}
g()->send(1);
',
'assertions' => [],
'ignored_issues' => ['InvalidArgument'],
],
'generatorDelegation' => [
'code' => '<?php
/**
* @return Generator<int, int, mixed, int>
*/
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<int, int>
*/
function seven_eight(): Generator {
yield 7;
yield from eight();
}
/**
* @return Generator<int,int>
*/
function eight(): Generator {
yield 8;
}
/**
* @return Generator<int,int, mixed, int>
*/
function nine_ten(): Generator {
yield 9;
return 10;
}
$gen = count_to_ten();
foreach ($gen as $num) {
echo "$num ";
}
$gen2 = $gen->getReturn();',
'assertions' => [
'$gen' => 'Generator<int, int, mixed, int>',
'$gen2' => 'int',
],
'ignored_issues' => ['MixedAssignment'],
],
'yieldFromArray' => [
'code' => '<?php
/**
* @return Generator<int, int, mixed, void>
*/
function Bar() : Generator {
yield from [1];
}',
],
'generatorWithNestedYield' => [
'code' => '<?php
function other_generator(): Generator {
yield "traffic";
return 1;
}
function foo(): Generator {
/** @var int */
$value = yield from other_generator();
var_export($value);
}',
],
'generatorVoidReturn' => [
'code' => '<?php
/**
* @return Generator
*/
function generator2() : Generator {
if (rand(0,1)) {
return;
}
yield 2;
}',
],
'returnType' => [
'code' => '<?php
function takesInt(int $i) : void {
echo $i;
}
function takesString(string $s) : void {
echo $s;
}
/**
* @return Generator<int, string, mixed, int>
*/
function other_generator() : Generator {
yield "traffic";
return 1;
}
/**
* @return Generator<int, string>
*/
function foo() : Generator {
$a = yield from other_generator();
takesInt($a);
}
foreach (foo() as $s) {
takesString($s);
}',
],
'expectNonNullableTypeWithYield' => [
'code' => '<?php
function example() : Generator {
yield from [2];
return null;
}',
],
'yieldFromIterable' => [
'code' => '<?php
/**
* @param iterable<int, string> $s
* @return Generator<int, string>
*/
function foo(iterable $s) : Traversable {
yield from $s;
}',
],
'yieldWithReturn' => [
'code' => '<?php
/** @return Generator<int, string, int, int> */
function gen(): Generator {
yield 3 => "abc";
$foo = 4;
return $foo;
}'
],
'echoYield' => [
'code' => '<?php
/** @return Generator<void, void, string, void> */
function gen(): Generator {
echo yield;
}'
],
'yieldFromTwiceWithVoidSend' => [
'code' => '<?php
/**
* @return \Generator<int, string, void, string>
*/
function test(): \Generator {
return yield "value";
}
function load(string $rsa_key): \Generator {
echo (yield from test()) . (yield from test());
return 5;
}'
],
'iteratorUnion' => [
'code' => '<?php
/** @return Iterator|IteratorAggregate */
function getIteratorOrAggregate() {
yield 2;
}
echo json_encode(iterator_to_array(getIteratorOrAggregate()));'
],
'yieldNonExistentClass' => [
'code' => '<?php
class T {
private const FACTORIES = [
ClassNotExisting::class,
];
function f() : Generator {
foreach (self::FACTORIES as $f) {
if (class_exists($f)) {
yield new $f();
}
}
}
}',
'assertions' => [],
'ignored_issues' => ['UndefinedClass']
],
'fillTemplatesForIteratorFromGenerator' => [
'code' => '<?php
/**
* @return Generator<int, string>
*/
function generator(): Generator
{
yield "test";
}
$iterator = new NoRewindIterator(generator());
',
'assertions' => [
'$iterator' => 'NoRewindIterator<int, string, Generator<int, string, mixed, mixed>>',
]
],
'detectYieldInNew' => [
'code' => '<?php
/** @psalm-suppress MissingClosureReturnType */
$_a = function() { return new RuntimeException(yield "a"); };
',
'assertions' => [
'$_a' => 'pure-Closure():Generator<int, string, mixed, RuntimeException>',
]
],
'detectYieldInArray' => [
'code' => '<?php
/** @psalm-suppress MissingClosureReturnType */
$_a = function() { return [yield "a"]; };
',
'assertions' => [
'$_a' => 'pure-Closure():Generator<int, string, mixed, array{string}>',
]
],
];
}
/**
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
*/
public function providerInvalidCodeParse(): iterable
{
return [
'shouldWarnAboutNoGeneratorReturn' => [
'code' => '<?php
function generator2() : Generator {
if (rand(0,1)) {
return;
}
yield 2;
}
/**
* @psalm-suppress InvalidNullableReturnType
*/
function notagenerator() : Generator {
if (rand(0, 1)) {
return;
}
return generator2();
}',
'error_message' => 'InvalidReturnStatement',
],
'expectNonNullableTypeWithNullReturn' => [
'code' => '<?php
function example() : Generator {
yield from [2];
return null;
}
function example2() : Generator {
if (rand(0, 1)) {
return example();
}
return null;
}',
'error_message' => 'NullableReturnStatement',
],
'invalidIterator' => [
'code' => '<?php
function example() : int {
return 0;
}
function example2() : Generator {
yield from example();
}',
'error_message' => 'InvalidIterator',
],
'rawObjectIteration' => [
'code' => '<?php
class A {
/** @var ?string */
public $foo;
}
function example() : Generator {
$arr = new A;
yield from $arr;
}',
'error_message' => 'RawObjectIteration',
],
'possibleRawObjectIteration' => [
'code' => '<?php
class A {
/** @var ?string */
public $foo;
}
class B extends A {}
function bar(A $a): void {}
function gen() : Generator {
$arr = [];
if (rand(0, 10) > 5) {
$arr[] = new A;
} else {
$arr = new B;
}
yield from $arr;
}',
'error_message' => 'PossibleRawObjectIteration',
],
'possibleRawObjectIterationFromIsset' => [
'code' => '<?php
function foo(array $a) : Generator {
if (isset($a["a"]["b"])) {
yield from $a["a"];
}
}',
'error_message' => 'PossibleRawObjectIteration',
],
];
}
}