forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverridingMethodRuleTest.php
840 lines (776 loc) · 26 KB
/
OverridingMethodRuleTest.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use function array_filter;
use function array_values;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<OverridingMethodRule>
*/
class OverridingMethodRuleTest extends RuleTestCase
{
private int $phpVersionId;
private bool $checkMissingOverrideMethodAttribute = false;
protected function getRule(): Rule
{
$phpVersion = new PhpVersion($this->phpVersionId);
$phpClassReflectionExtension = self::getContainer()->getByType(PhpClassReflectionExtension::class);
return new OverridingMethodRule(
$phpVersion,
new MethodSignatureRule($phpClassReflectionExtension, true, true, true),
false,
new MethodParameterComparisonHelper($phpVersion, true),
$phpClassReflectionExtension,
true,
true,
$this->checkMissingOverrideMethodAttribute,
);
}
public function dataOverridingFinalMethod(): array
{
return [
[
70300,
'compatible',
'compatible',
],
[
70400,
'contravariant',
'covariant',
],
];
}
/**
* @dataProvider dataOverridingFinalMethod
*/
public function testOverridingFinalMethod(int $phpVersion, string $contravariantMessage): void
{
$errors = [
[
'Method OverridingFinalMethod\Bar::doFoo() overrides final method OverridingFinalMethod\Foo::doFoo().',
43,
],
[
'Private method OverridingFinalMethod\Bar::doBar() overriding public method OverridingFinalMethod\Foo::doBar() should also be public.',
48,
],
[
'Protected method OverridingFinalMethod\Bar::doBaz() overriding public method OverridingFinalMethod\Foo::doBaz() should also be public.',
53,
],
[
'Private method OverridingFinalMethod\Bar::doLorem() overriding protected method OverridingFinalMethod\Foo::doLorem() should be protected or public.',
58,
],
[
'Non-static method OverridingFinalMethod\Bar::doIpsum() overrides static method OverridingFinalMethod\Foo::doIpsum().',
63,
],
[
'Static method OverridingFinalMethod\Bar::doDolor() overrides non-static method OverridingFinalMethod\Foo::doDolor().',
68,
],
[
'Parameter #1 $s (string) of method OverridingFinalMethod\Dolor::__construct() is not ' . $contravariantMessage . ' with parameter #1 $i (int) of method OverridingFinalMethod\Ipsum::__construct().',
110,
],
[
'Method OverridingFinalMethod\Dolor::doFoo() overrides method OverridingFinalMethod\Ipsum::doFoo() but misses parameter #1 $i.',
115,
],
[
'Parameter #1 $size (int) of method OverridingFinalMethod\FixedArray::setSize() is not ' . $contravariantMessage . ' with parameter #1 $size (mixed) of method SplFixedArray<mixed>::setSize().',
125,
],
[
'Parameter #1 $j of method OverridingFinalMethod\Amet::doBar() is not variadic but parameter #1 $j of method OverridingFinalMethod\Sit::doBar() is variadic.',
160,
],
[
'Parameter #1 $j of method OverridingFinalMethod\Amet::doBaz() is variadic but parameter #1 $j of method OverridingFinalMethod\Sit::doBaz() is not variadic.',
165,
],
[
'Parameter #2 $j of method OverridingFinalMethod\Consecteur::doFoo() is required but parameter #2 $j of method OverridingFinalMethod\Sit::doFoo() is optional.',
175,
],
[
'Parameter #1 $i of method OverridingFinalMethod\Lacus::doFoo() is not passed by reference but parameter #1 $i of method OverridingFinalMethod\Etiam::doFoo() is passed by reference.',
195,
],
[
'Parameter #2 $j of method OverridingFinalMethod\Lacus::doFoo() is passed by reference but parameter #2 $j of method OverridingFinalMethod\Etiam::doFoo() is not passed by reference.',
195,
],
[
'Parameter #1 $i of method OverridingFinalMethod\BazBaz::doBar() is not optional.',
205,
],
[
'Parameter #2 $j of method OverridingFinalMethod\FooFoo::doFoo() is not optional.',
225,
],
[
'Method OverridingFinalMethod\SomeOtherException::__construct() overrides final method OverridingFinalMethod\OtherException::__construct().',
280,
],
[
'Method OverridingFinalMethod\ExtendsFinalWithAnnotation::doFoo() overrides @final method OverridingFinalMethod\FinalWithAnnotation::doFoo().',
303,
],
[
'Parameter #1 $index (int) of method OverridingFinalMethod\FixedArrayOffsetExists::offsetExists() is not ' . $contravariantMessage . ' with parameter #1 $index (mixed) of method SplFixedArray<mixed>::offsetExists().',
313,
],
];
if (PHP_VERSION_ID >= 80000) {
$errors = array_values(array_filter($errors, static fn (array $error): bool => $error[1] !== 125));
}
$this->phpVersionId = $phpVersion;
$this->analyse([__DIR__ . '/data/overriding-method.php'], $errors);
}
public function dataParameterContravariance(): array
{
return [
[
__DIR__ . '/data/parameter-contravariance-array.php',
70300,
[
[
'Parameter #1 $a (iterable) of method ParameterContravarianceArray\Baz::doBar() is not compatible with parameter #1 $a (array|null) of method ParameterContravarianceArray\Foo::doBar().',
43,
],
],
],
[
__DIR__ . '/data/parameter-contravariance-array.php',
70400,
[
[
'Parameter #1 $a (iterable) of method ParameterContravarianceArray\Baz::doBar() is not contravariant with parameter #1 $a (array|null) of method ParameterContravarianceArray\Foo::doBar().',
43,
],
],
],
[
__DIR__ . '/data/parameter-contravariance-traversable.php',
70300,
[
[
'Parameter #1 $a (iterable) of method ParameterContravarianceTraversable\Baz::doBar() is not compatible with parameter #1 $a (Traversable|null) of method ParameterContravarianceTraversable\Foo::doBar().',
43,
],
],
],
[
__DIR__ . '/data/parameter-contravariance-traversable.php',
70400,
[
[
'Parameter #1 $a (iterable) of method ParameterContravarianceTraversable\Baz::doBar() is not contravariant with parameter #1 $a (Traversable|null) of method ParameterContravarianceTraversable\Foo::doBar().',
43,
],
],
],
[
__DIR__ . '/data/parameter-contravariance.php',
70300,
[
[
'Parameter #1 $e (Exception) of method ParameterContravariance\Bar::doBar() is not compatible with parameter #1 $e (InvalidArgumentException) of method ParameterContravariance\Foo::doBar().',
28,
],
[
'Parameter #1 $e (Exception|null) of method ParameterContravariance\Baz::doBar() is not compatible with parameter #1 $e (InvalidArgumentException) of method ParameterContravariance\Foo::doBar().',
38,
],
[
'Parameter #1 $e (InvalidArgumentException) of method ParameterContravariance\Lorem::doFoo() is not compatible with parameter #1 $e (Exception) of method ParameterContravariance\Foo::doFoo().',
48,
],
[
'Parameter #1 $e (InvalidArgumentException|null) of method ParameterContravariance\Ipsum::doFoo() is not compatible with parameter #1 $e (Exception) of method ParameterContravariance\Foo::doFoo().',
58,
],
],
],
[
__DIR__ . '/data/parameter-contravariance.php',
70400,
[
[
'Parameter #1 $e (InvalidArgumentException) of method ParameterContravariance\Lorem::doFoo() is not contravariant with parameter #1 $e (Exception) of method ParameterContravariance\Foo::doFoo().',
48,
],
[
'Parameter #1 $e (InvalidArgumentException|null) of method ParameterContravariance\Ipsum::doFoo() is not contravariant with parameter #1 $e (Exception) of method ParameterContravariance\Foo::doFoo().',
58,
],
],
],
];
}
/**
* @dataProvider dataParameterContravariance
* @param list<array{0: string, 1: int, 2?: string}> $expectedErrors
*/
public function testParameterContravariance(
string $file,
int $phpVersion,
array $expectedErrors,
): void
{
$this->phpVersionId = $phpVersion;
$this->analyse([$file], $expectedErrors);
}
public function dataReturnTypeCovariance(): array
{
return [
[
70300,
[
[
'Return type iterable of method ReturnTypeCovariance\Bar::doBar() is not compatible with return type array of method ReturnTypeCovariance\Foo::doBar().',
38,
],
[
'Return type InvalidArgumentException of method ReturnTypeCovariance\Bar::doBaz() is not compatible with return type Exception of method ReturnTypeCovariance\Foo::doBaz().',
43,
],
[
'Return type Exception of method ReturnTypeCovariance\Bar::doLorem() is not compatible with return type InvalidArgumentException of method ReturnTypeCovariance\Foo::doLorem().',
48,
],
[
'Return type mixed of method ReturnTypeCovariance\B::foo() is not compatible with return type stdClass|null of method ReturnTypeCovariance\A::foo().',
66,
],
],
],
[
70400,
[
[
'Return type iterable of method ReturnTypeCovariance\Bar::doBar() is not covariant with return type array of method ReturnTypeCovariance\Foo::doBar().',
38,
],
[
'Return type Exception of method ReturnTypeCovariance\Bar::doLorem() is not covariant with return type InvalidArgumentException of method ReturnTypeCovariance\Foo::doLorem().',
48,
],
[
'Return type mixed of method ReturnTypeCovariance\B::foo() is not covariant with return type stdClass|null of method ReturnTypeCovariance\A::foo().',
66,
],
],
],
];
}
/**
* @dataProvider dataReturnTypeCovariance
* @param list<array{0: string, 1: int, 2?: string}> $expectedErrors
*/
public function testReturnTypeCovariance(
int $phpVersion,
array $expectedErrors,
): void
{
$this->phpVersionId = $phpVersion;
$this->analyse([__DIR__ . '/data/return-type-covariance.php'], $expectedErrors);
}
/**
* @dataProvider dataOverridingFinalMethod
*/
public function testParle(int $phpVersion, string $contravariantMessage, string $covariantMessage): void
{
$this->phpVersionId = $phpVersion;
$this->analyse([__DIR__ . '/data/parle.php'], [
[
'Parameter #1 $state (int) of method OverridingParle\Foo::pushState() is not ' . $contravariantMessage . ' with parameter #1 $state (string) of method Parle\RLexer::pushState().',
8,
],
[
'Return type string of method OverridingParle\Foo::pushState() is not ' . $covariantMessage . ' with return type int of method Parle\RLexer::pushState().',
8,
],
]);
}
public function testVariadicParameterIsAlwaysOptional(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/variadic-always-optional.php'], []);
}
/**
* @dataProvider dataOverridingFinalMethod
*/
public function testBug3403(int $phpVersion): void
{
$this->phpVersionId = $phpVersion;
$this->analyse([__DIR__ . '/data/bug-3403.php'], []);
}
public function testBug3443(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-3443.php'], []);
}
public function testBug3478(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-3478.php'], []);
}
public function testBug3629(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-3629.php'], []);
}
public function testVariadics(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$errors = [
[
'Parameter #2 $lang of method OverridingVariadics\OtherTranslator::translate() is not optional.',
34,
],
[
'Parameter #2 $lang of method OverridingVariadics\AnotherTranslator::translate() is not optional.',
44,
],
[
'Parameter #3 $parameters of method OverridingVariadics\AnotherTranslator::translate() is not variadic.',
44,
],
[
'Parameter #2 $lang of method OverridingVariadics\YetAnotherTranslator::translate() is not variadic.',
54,
],
];
$this->analyse([__DIR__ . '/data/overriding-variadics.php'], $errors);
}
public function dataLessOverridenParametersWithVariadic(): array
{
return [
[
70400,
[
[
'Parameter #1 $everything of method LessParametersVariadics\Bar::doFoo() is variadic but parameter #1 $many of method LessParametersVariadics\Foo::doFoo() is not variadic.',
18,
],
[
'Method LessParametersVariadics\Bar::doFoo() overrides method LessParametersVariadics\Foo::doFoo() but misses parameter #2 $parameters.',
18,
],
[
'Method LessParametersVariadics\Bar::doFoo() overrides method LessParametersVariadics\Foo::doFoo() but misses parameter #3 $here.',
18,
],
[
'Parameter #1 $everything of method LessParametersVariadics\Baz::doFoo() is variadic but parameter #1 $many of method LessParametersVariadics\Foo::doFoo() is not variadic.',
28,
],
[
'Method LessParametersVariadics\Baz::doFoo() overrides method LessParametersVariadics\Foo::doFoo() but misses parameter #2 $parameters.',
28,
],
[
'Method LessParametersVariadics\Baz::doFoo() overrides method LessParametersVariadics\Foo::doFoo() but misses parameter #3 $here.',
28,
],
[
'Parameter #2 $everything of method LessParametersVariadics\Lorem::doFoo() is variadic but parameter #2 $parameters of method LessParametersVariadics\Foo::doFoo() is not variadic.',
38,
],
[
'Method LessParametersVariadics\Lorem::doFoo() overrides method LessParametersVariadics\Foo::doFoo() but misses parameter #3 $here.',
38,
],
],
],
[
80000,
[
[
'Parameter #1 ...$everything (int) of method LessParametersVariadics\Baz::doFoo() is not contravariant with parameter #2 $parameters (string) of method LessParametersVariadics\Foo::doFoo().',
28,
],
[
'Parameter #1 ...$everything (int) of method LessParametersVariadics\Baz::doFoo() is not contravariant with parameter #3 $here (string) of method LessParametersVariadics\Foo::doFoo().',
28,
],
],
],
];
}
/**
* @dataProvider dataLessOverridenParametersWithVariadic
* @param list<array{0: string, 1: int, 2?: string}> $errors
*/
public function testLessOverridenParametersWithVariadic(int $phpVersionId, array $errors): void
{
$this->phpVersionId = $phpVersionId;
$this->analyse([__DIR__ . '/data/less-parameters-variadics.php'], $errors);
}
public function dataParameterTypeWidening(): array
{
return [
[
70100,
[
[
'Parameter #1 $foo (mixed) of method ParameterTypeWidening\Bar::doFoo() does not match parameter #1 $foo (string) of method ParameterTypeWidening\Foo::doFoo().',
18,
],
],
],
[
70200,
[],
],
];
}
/**
* @dataProvider dataParameterTypeWidening
* @param list<array{0: string, 1: int, 2?: string}> $errors
*/
public function testParameterTypeWidening(int $phpVersionId, array $errors): void
{
$this->phpVersionId = $phpVersionId;
$this->analyse([__DIR__ . '/data/parameter-type-widening.php'], $errors);
}
public function testBug4516(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-4516.php'], []);
}
public function dataTentativeReturnTypes(): array
{
return [
[70400, []],
[80000, []],
[
80100,
[
[
'Return type mixed of method TentativeReturnTypes\Foo::getIterator() is not covariant with tentative return type Traversable of method IteratorAggregate<mixed,mixed>::getIterator().',
8,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
[
'Return type string of method TentativeReturnTypes\Lorem::getIterator() is not covariant with tentative return type Traversable of method IteratorAggregate<mixed,mixed>::getIterator().',
40,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
[
'Return type mixed of method TentativeReturnTypes\UntypedIterator::current() is not covariant with tentative return type mixed of method Iterator<mixed,mixed>::current().',
75,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
[
'Return type mixed of method TentativeReturnTypes\UntypedIterator::next() is not covariant with tentative return type void of method Iterator<mixed,mixed>::next().',
79,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
[
'Return type mixed of method TentativeReturnTypes\UntypedIterator::key() is not covariant with tentative return type mixed of method Iterator<mixed,mixed>::key().',
83,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
[
'Return type mixed of method TentativeReturnTypes\UntypedIterator::valid() is not covariant with tentative return type bool of method Iterator<mixed,mixed>::valid().',
87,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
[
'Return type mixed of method TentativeReturnTypes\UntypedIterator::rewind() is not covariant with tentative return type void of method Iterator<mixed,mixed>::rewind().',
91,
'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.',
],
],
],
];
}
/**
* @dataProvider dataTentativeReturnTypes
* @param list<array{0: string, 1: int, 2?: string}> $errors
*/
public function testTentativeReturnTypes(int $phpVersionId, array $errors): void
{
if (PHP_VERSION_ID < 80100) {
$errors = [];
}
if ($phpVersionId > PHP_VERSION_ID) {
$this->markTestSkipped();
}
$this->phpVersionId = $phpVersionId;
$this->analyse([__DIR__ . '/data/tentative-return-types.php'], $errors);
}
public function testCountableBug(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/countable-bug.php'], []);
}
public function testBug6264(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-6264.php'], []);
}
public function testBug7717(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-7717.php'], []);
}
public function testBug6104(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-6104.php'], []);
}
public function testBug9391(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-9391.php'], []);
}
public function testBugWithIndirectPrototype(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/overriding-indirect-prototype.php'], [
[
'Return type mixed of method OverridingIndirectPrototype\Baz::doFoo() is not covariant with return type string of method OverridingIndirectPrototype\Bar::doFoo().',
28,
],
]);
}
public function testBug10043(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-10043.php'], [
[
'Method Bug10043\C::foo() overrides final method Bug10043\B::foo().',
17,
],
]);
}
public function testBug7859(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-7859.php'], [
[
'Method Bug7859\ExtendingClassImplementingSomeInterface::getList() overrides method Bug7859\ImplementingSomeInterface::getList() but misses parameter #2 $b.',
21,
],
[
'Method Bug7859\ExtendingClassNotImplementingSomeInterface::getList() overrides method Bug7859\NotImplementingSomeInterface::getList() but misses parameter #2 $b.',
37,
],
]);
}
public function testBug8081(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-8081.php'], [
[
'Return type mixed of method Bug8081\three::foo() is not covariant with return type array of method Bug8081\two::foo().',
21,
],
]);
}
public function testBug8500(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-8500.php'], [
[
'Return type mixed of method Bug8500\DBOHB::test() is not covariant with return type Bug8500\DBOA of method Bug8500\DBOHA::test().',
30,
],
]);
}
public function testBug9014(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-9014.php'], [
[
'Method Bug9014\Bar::test() overrides method Bug9014\Foo::test() but misses parameter #2 $test.',
16,
],
[
'Return type mixed of method Bug9014\extended::renderForUser() is not covariant with return type string of method Bug9014\middle::renderForUser().',
42,
],
]);
}
public function testBug9135(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-9135.php'], [
[
'Method Bug9135\Sub::sayHello() overrides @final method Bug9135\HelloWorld::sayHello().',
15,
],
]);
}
public function testBug10101(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-10101.php'], [
[
'Return type mixed of method Bug10101\B::next() is not covariant with return type void of method Bug10101\A::next().',
10,
],
]);
}
public function testBug9615(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}
$tipText = 'Make it covariant, or use the #[\ReturnTypeWillChange] attribute to temporarily suppress the error.';
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-9615.php'], [
[
'Return type mixed of method Bug9615\ExpectComplaintsHere::accept() is not covariant with tentative return type bool of method FilterIterator<mixed,mixed,Traversable<mixed, mixed>>::accept().',
19,
$tipText,
],
[
'Return type mixed of method Bug9615\ExpectComplaintsHere::getChildren() is not covariant with tentative return type RecursiveIterator|null of method RecursiveIterator<mixed,mixed>::getChildren().',
20,
$tipText,
],
]);
}
public function testBug10149(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$errors = [];
if (PHP_VERSION_ID >= 80300) {
$errors = [
[
'Method Bug10149\StdSat::__get() has #[\Override] attribute but does not override any method.',
10,
],
];
}
$this->analyse([__DIR__ . '/data/bug-10149.php'], $errors);
}
public function testTraits(): void
{
$errors = [];
if (PHP_VERSION_ID >= 80000) {
$errors = [
[
'Parameter #1 $i (int) of method OverridingTraitMethods\Bar::doBar() is not contravariant with parameter #1 $i (string) of method OverridingTraitMethods\Foo::doBar().',
27,
],
[
'Parameter #1 $i (int) of method OverridingTraitMethods\Baz::doBar() is not contravariant with parameter #1 $i (string) of method OverridingTraitMethods\FooPrivate::doBar().',
45,
],
[
'Static method OverridingTraitMethods\Ipsum::doBar() overrides non-static method OverridingTraitMethods\Foo::doBar().',
65,
],
[
'Non-static method OverridingTraitMethods\Dolor::doBar() overrides static method OverridingTraitMethods\FooStatic::doBar().',
80,
],
];
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/overriding-trait-methods.php'], $errors);
}
public function testOverrideAttribute(): void
{
if (PHP_VERSION_ID < 80300) {
$this->markTestSkipped('Test requires PHP 8.3.');
}
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/override-attribute.php'], [
[
'Method OverrideAttribute\Bar::test2() has #[\Override] attribute but does not override any method.',
24,
],
[
'Method OverrideAttribute\ChildOfParentWithConstructor::__construct() has #[\Override] attribute but does not override any method.',
42,
],
]);
}
public function dataCheckMissingOverrideAttribute(): iterable
{
yield [false, 80000, []];
yield [true, 80000, []];
yield [false, 80300, []];
yield [true, 80300, [
[
'Method CheckMissingOverrideAttr\Bar::doFoo() overrides method CheckMissingOverrideAttr\Foo::doFoo() but is missing the #[\Override] attribute.',
18,
],
[
'Method CheckMissingOverrideAttr\ChildOfParentWithAbstractConstructor::__construct() overrides method CheckMissingOverrideAttr\ParentWithAbstractConstructor::__construct() but is missing the #[\Override] attribute.',
49,
],
]];
}
/**
* @dataProvider dataCheckMissingOverrideAttribute
* @param list<array{0: string, 1: int, 2?: string}> $errors
*/
public function testCheckMissingOverrideAttribute(bool $checkMissingOverrideMethodAttribute, int $phpVersionId, array $errors): void
{
$this->checkMissingOverrideMethodAttribute = $checkMissingOverrideMethodAttribute;
$this->phpVersionId = $phpVersionId;
$this->analyse([__DIR__ . '/data/check-missing-override-attr.php'], $errors);
}
public function testBug10153(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$errors = [];
if (PHP_VERSION_ID >= 80000) {
$errors = [
[
'Return type Bug10153\MyClass2|null of method Bug10153\MyClass2::drc() is not covariant with return type Bug10153\MyClass2 of method Bug10153\MyTrait::drc().',
24,
],
];
}
$this->analyse([__DIR__ . '/data/bug-10153.php'], $errors);
}
public function testBug12471(): void
{
if (PHP_VERSION_ID < 80300) {
$this->markTestSkipped('Test requires PHP 8.3.');
}
$this->checkMissingOverrideMethodAttribute = true;
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-12471.php'], []);
}
public function testBug10165(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-10165.php'], []);
}
}