-
Notifications
You must be signed in to change notification settings - Fork 428
/
ReturnTypeRuleTest.php
583 lines (543 loc) · 17.2 KB
/
ReturnTypeRuleTest.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PHPStan\Rules\FunctionReturnTypeCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<ReturnTypeRule>
*/
class ReturnTypeRuleTest extends RuleTestCase
{
private bool $checkExplicitMixed = false;
protected function getRule(): Rule
{
return new ReturnTypeRule(new FunctionReturnTypeCheck(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed)));
}
public function testReturnTypeRule(): void
{
$this->analyse([__DIR__ . '/data/returnTypes.php'], [
[
'Method ReturnTypes\Foo::returnInteger() should return int but returns string.',
20,
],
[
'Method ReturnTypes\Foo::returnObject() should return ReturnTypes\Bar but returns int.',
30,
],
[
'Method ReturnTypes\Foo::returnObject() should return ReturnTypes\Bar but returns ReturnTypes\Foo.',
34,
],
[
'Method ReturnTypes\Foo::returnChild() should return ReturnTypes\Foo but returns ReturnTypes\OtherInterfaceImpl.',
53,
],
[
'Method ReturnTypes\Foo::returnVoid() with return type void returns null but should not return anything.',
86,
],
[
'Method ReturnTypes\Foo::returnVoid() with return type void returns int but should not return anything.',
90,
],
[
'Method ReturnTypes\Foo::returnStatic() should return static(ReturnTypes\Foo) but returns ReturnTypes\FooParent.',
106,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns ReturnTypes\Foo.',
139,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns ReturnTypes\Bar.',
147,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns array<int, ReturnTypes\Bar>.',
151,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns int.',
155,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but empty return statement found.',
159,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns iterable<ReturnTypes\Bar>&ReturnTypes\Collection.',
166,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns iterable<ReturnTypes\Foo>&ReturnTypes\AnotherCollection.',
173,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns (iterable<ReturnTypes\Foo>&ReturnTypes\AnotherCollection)|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection).',
180,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns iterable<ReturnTypes\Bar>&ReturnTypes\AnotherCollection.',
187,
],
[
'Method ReturnTypes\Foo::returnUnionIterableType() should return array<ReturnTypes\Foo>|(iterable<ReturnTypes\Foo>&ReturnTypes\Collection) but returns null.',
191,
],
[
'Method ReturnTypes\Foo::returnThis() should return $this(ReturnTypes\Foo) but returns ReturnTypes\Foo.',
219,
],
[
'Method ReturnTypes\Foo::returnThis() should return $this(ReturnTypes\Foo) but returns int.',
222,
],
[
'Method ReturnTypes\Foo::returnThis() should return $this(ReturnTypes\Foo) but returns static(ReturnTypes\Foo).',
225,
],
[
'Method ReturnTypes\Foo::returnThis() should return $this(ReturnTypes\Foo) but returns null.',
229,
],
[
'Method ReturnTypes\Foo::returnThisOrNull() should return $this(ReturnTypes\Foo)|null but returns ReturnTypes\Foo.',
247,
],
[
'Method ReturnTypes\Foo::returnThisOrNull() should return $this(ReturnTypes\Foo)|null but returns int.',
250,
],
[
'Method ReturnTypes\Foo::returnThisOrNull() should return $this(ReturnTypes\Foo)|null but returns static(ReturnTypes\Foo).',
259,
],
[
'Method ReturnTypes\Foo::returnsParent() should return ReturnTypes\FooParent but returns int.',
282,
],
[
'Method ReturnTypes\Foo::returnsParent() should return ReturnTypes\FooParent but returns null.',
285,
],
[
'Method ReturnTypes\Foo::returnsPhpDocParent() should return ReturnTypes\FooParent but returns int.',
298,
],
[
'Method ReturnTypes\Foo::returnsPhpDocParent() should return ReturnTypes\FooParent but returns null.',
301,
],
[
'Method ReturnTypes\Foo::returnScalar() should return bool|float|int|string but returns stdClass.',
323,
],
[
'Method ReturnTypes\Foo::returnsNullInTernary() should return int but returns int|null.',
342,
],
[
'Method ReturnTypes\Foo::returnsNullInTernary() should return int but returns int|null.',
348,
],
[
'Method ReturnTypes\Foo::misleadingBoolReturnType() should return ReturnTypes\boolean but returns true.',
355,
],
[
'Method ReturnTypes\Foo::misleadingBoolReturnType() should return ReturnTypes\boolean but returns int.',
358,
],
[
'Method ReturnTypes\Foo::misleadingIntReturnType() should return ReturnTypes\integer but returns int.',
368,
],
[
'Method ReturnTypes\Foo::misleadingIntReturnType() should return ReturnTypes\integer but returns true.',
371,
],
[
'Method ReturnTypes\Stock::getAnotherStock() should return ReturnTypes\Stock but returns ReturnTypes\Stock|null.',
429,
],
[
'Method ReturnTypes\Stock::returnSelfAgainError() should return ReturnTypes\Stock but returns ReturnTypes\Stock|null.',
484,
],
[
'Method ReturnTypes\Stock::returnYetSelfAgainError() should return ReturnTypes\Stock but returns ReturnTypes\Stock|null.',
508,
],
[
'Method ReturnTypes\ReturningSomethingFromConstructor::__construct() with return type void returns ReturnTypes\Foo but should not return anything.',
552,
],
[
'Method ReturnTypes\ReturnTernary::returnTernary() should return ReturnTypes\Foo but returns false.',
627,
],
[
'Method ReturnTypes\TrickyVoid::returnVoidOrInt() should return int|void but returns string.',
658,
],
[
'Method ReturnTypes\TernaryWithJsonEncode::toJson() should return string but returns string|false.',
689,
],
[
'Method ReturnTypes\AppendedArrayReturnType::foo() should return array<int> but returns array<int, stdClass>.',
702,
],
[
'Method ReturnTypes\AppendedArrayReturnType::bar() should return array<int> but returns array<int|stdClass>.',
712,
],
[
'Method ReturnTypes\WrongMagicMethods::__toString() should return string but returns true.',
722,
],
[
'Method ReturnTypes\WrongMagicMethods::__isset() should return bool but returns int.',
727,
],
[
'Method ReturnTypes\WrongMagicMethods::__destruct() with return type void returns int but should not return anything.',
732,
],
[
'Method ReturnTypes\WrongMagicMethods::__unset() with return type void returns int but should not return anything.',
737,
],
[
'Method ReturnTypes\WrongMagicMethods::__sleep() should return array<int, string> but returns array<int, stdClass>.',
742,
],
[
'Method ReturnTypes\WrongMagicMethods::__wakeup() with return type void returns int but should not return anything.',
749,
],
[
'Method ReturnTypes\WrongMagicMethods::__set_state() should return object but returns array<string, string>.',
754,
],
[
'Method ReturnTypes\WrongMagicMethods::__clone() with return type void returns int but should not return anything.',
759,
],
[
'Method ReturnTypes\ArrayFillKeysIssue::getIPs2() should return array<string, array<ReturnTypes\Foo>> but returns array<string, array<int, ReturnTypes\Bar>>.',
817,
],
[
'Method ReturnTypes\AssertThisInstanceOf::doBar() should return $this(ReturnTypes\AssertThisInstanceOf) but returns ReturnTypes\AssertThisInstanceOf&ReturnTypes\FooInterface.',
840,
],
[
'Method ReturnTypes\NestedArrayCheck::doFoo() should return array<string, bool> but returns array<string, array<int, string>>.',
860,
],
[
'Method ReturnTypes\NestedArrayCheck::doBar() should return array<string, bool> but returns array<string, array<string, string>>.',
875,
],
[
'Method ReturnTypes\Foo2::returnIntFromParent() should return int but returns string.',
950,
],
[
'Method ReturnTypes\Foo2::returnIntFromParent() should return int but returns ReturnTypes\integer.',
953,
],
[
'Method ReturnTypes\VariableOverwrittenInForeach::doFoo() should return int but returns int|string.',
1011,
],
[
'Method ReturnTypes\VariableOverwrittenInForeach::doBar() should return int but returns int|string.',
1026,
],
[
'Method ReturnTypes\ReturnStaticGeneric::instanceReturnsStatic() should return static(ReturnTypes\ReturnStaticGeneric) but returns ReturnTypes\ReturnStaticGeneric.',
1066,
],
[
'Method ReturnTypes\NeverReturn::doFoo() should never return but return statement found.',
1241,
],
[
'Method ReturnTypes\NeverReturn::doBaz3() should never return but return statement found.',
1254,
],
]);
}
public function testMisleadingTypehintsInClassWithoutNamespace(): void
{
$this->analyse([__DIR__ . '/data/misleadingTypehints.php'], [
[
'Method FooWithoutNamespace::misleadingBoolReturnType() should return boolean but returns true.',
9,
],
[
'Method FooWithoutNamespace::misleadingBoolReturnType() should return boolean but returns int.',
13,
],
[
'Method FooWithoutNamespace::misleadingIntReturnType() should return integer but returns int.',
24,
],
[
'Method FooWithoutNamespace::misleadingIntReturnType() should return integer but returns true.',
28,
],
]);
}
public function testOverridenTypeFromIfConditionShouldNotBeMixedAfterBranch(): void
{
$this->analyse([__DIR__ . '/data/returnTypes-overridenTypeInIfCondition.php'], [
[
'Method ReturnTypes\OverridenTypeInIfCondition::getAnotherAnotherStock() should return ReturnTypes\Stock but returns ReturnTypes\Stock|null.',
15,
],
]);
}
public function testReturnStaticFromParent(): void
{
$this->analyse([__DIR__ . '/data/return-static-from-parent.php'], []);
}
public function testReturnIterable(): void
{
$this->analyse([__DIR__ . '/data/returnTypes-iterable.php'], [
[
'Method ReturnTypesIterable\Foo::stringIterable() should return iterable<string> but returns array<int, int>.',
27,
],
[
'Method ReturnTypesIterable\Foo::stringIterablePipe() should return iterable<string> but returns array<int, int>.',
36,
],
]);
}
public function testBug2676(): void
{
$this->analyse([__DIR__ . '/data/bug-2676.php'], []);
}
public function testBug2885(): void
{
$this->analyse([__DIR__ . '/data/bug-2885.php'], []);
}
public function testMergeInheritedPhpDocs(): void
{
$this->analyse([__DIR__ . '/data/merge-inherited-return.php'], [
[
'Method ReturnTypePhpDocMergeReturnInherited\ParentClass::method() should return ReturnTypePhpDocMergeReturnInherited\B but returns ReturnTypePhpDocMergeReturnInherited\A.',
33,
],
[
'Method ReturnTypePhpDocMergeReturnInherited\ChildClass::method() should return ReturnTypePhpDocMergeReturnInherited\B but returns ReturnTypePhpDocMergeReturnInherited\A.',
41,
],
[
'Method ReturnTypePhpDocMergeReturnInherited\ChildClass2::method() should return ReturnTypePhpDocMergeReturnInherited\D but returns ReturnTypePhpDocMergeReturnInherited\B.',
52,
],
]);
}
public function testReturnTypeRulePhp70(): void
{
if (!self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires static reflection.');
}
$this->analyse([__DIR__ . '/data/returnTypes-7.0.php'], [
[
'Method ReturnTypes\FooPhp70::returnInteger() should return int but empty return statement found.',
10,
],
]);
}
public function testBug3997(): void
{
$this->analyse([__DIR__ . '/data/bug-3997.php'], [
[
'Method Bug3997\Foo::count() should return int but returns string.',
13,
],
[
'Method Bug3997\Bar::count() should return int but returns string.',
24,
],
[
'Method Bug3997\Baz::count() should return int but returns string.',
38,
],
[
'Method Bug3997\Lorem::count() should return int but returns string.',
52,
],
[
'Method Bug3997\Dolor::count() should return int<0, max> but returns -1.',
78,
],
]);
}
public function testBug1903(): void
{
$this->analyse([__DIR__ . '/data/bug-1903.php'], [
[
'Method Bug1903\Test::doFoo() should return array but returns int.',
19,
],
]);
}
public function testBug3117(): void
{
$this->analyse([__DIR__ . '/data/bug-3117.php'], [
[
'Method Bug3117\SimpleTemporal::adjustInto() should return T of Bug3117\Temporal but returns $this(Bug3117\SimpleTemporal).',
35,
],
]);
}
public function testBug3034(): void
{
$this->analyse([__DIR__ . '/data/bug-3034.php'], []);
}
public function testInferArrayKey(): void
{
$this->analyse([__DIR__ . '/data/infer-array-key.php'], []);
}
public function testBug4590(): void
{
$this->analyse([__DIR__ . '/data/bug-4590.php'], [
[
'Method Bug4590\\Controller::test1() should return Bug4590\\OkResponse<array<string, string>> but returns Bug4590\\OkResponse<array{ok: string}>.',
39,
],
[
'Method Bug4590\\Controller::test2() should return Bug4590\\OkResponse<array<int, string>> but returns Bug4590\\OkResponse<array{string}>.',
47,
],
[
'Method Bug4590\\Controller::test3() should return Bug4590\\OkResponse<array<string>> but returns Bug4590\\OkResponse<array{string}>.',
55,
],
]);
}
public function testTemplateStringBound(): void
{
$this->analyse([__DIR__ . '/data/template-string-bound.php'], []);
}
public function testBug4605(): void
{
$this->analyse([__DIR__ . '/data/bug-4605.php'], []);
}
public function testReturnStatic(): void
{
$this->analyse([__DIR__ . '/data/return-static.php'], []);
}
public function testBug4648(): void
{
$this->analyse([__DIR__ . '/data/bug-4648.php'], []);
}
public function testBug3523(): void
{
$this->analyse([__DIR__ . '/data/bug-3523.php'], [
[
'Method Bug3523\Bar::deserialize() should return static(Bug3523\Bar) but returns Bug3523\Bar.',
31,
],
]);
}
public function testBug3120(): void
{
$this->analyse([__DIR__ . '/data/bug-3120.php'], []);
}
public function testBug3118(): void
{
$this->analyse([__DIR__ . '/data/bug-3118.php'], [
[
'Method Bug3118\CustomEnum2::all() should return Bug3118\EnumSet<static(Bug3118\CustomEnum2)> but returns Bug3118\CustomEnumSet.',
56,
],
]);
}
public function testBug4795(): void
{
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0');
}
$this->analyse([__DIR__ . '/data/bug-4795.php'], []);
}
public function testBug4803(): void
{
$this->analyse([__DIR__ . '/../../Analyser/data/bug-4803.php'], []);
}
public function testBug2573(): void
{
$this->analyse([__DIR__ . '/data/bug-2573-return.php'], []);
}
public function testBug4603(): void
{
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0');
}
$this->analyse([__DIR__ . '/data/bug-4603.php'], []);
}
public function testBug3151(): void
{
$this->analyse([__DIR__ . '/data/bug-3151.php'], []);
}
public function testTemplateUnion(): void
{
$this->analyse([__DIR__ . '/data/return-template-union.php'], [
[
'Method ReturnTemplateUnion\Foo::doFoo2() should return T of bool|float|int|string but returns (T of bool|float|int|string)|null.',
25,
],
]);
}
public function dataBug5218(): array
{
return [
[
true,
[
[
'Method Bug5218\IA::getIterator() should return Traversable<string, int> but returns ArrayIterator<string, mixed>.',
14,
],
],
],
[
false,
[],
],
];
}
/**
* @dataProvider dataBug5218
* @param mixed[] $errors
*/
public function testBug5218(bool $checkExplicitMixed, array $errors): void
{
$this->checkExplicitMixed = $checkExplicitMixed;
$this->analyse([__DIR__ . '/data/bug-5218.php'], $errors);
}
public function testBug5979(): void
{
$this->analyse([__DIR__ . '/data/bug-5979.php'], []);
}
public function testBug4165(): void
{
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->analyse([__DIR__ . '/data/bug-4165.php'], []);
}
public function testBug6053(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-6053.php'], []);
}
}