-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathNonexistentOffsetInArrayDimFetchRuleTest.php
838 lines (751 loc) · 18.2 KB
/
NonexistentOffsetInArrayDimFetchRuleTest.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<NonexistentOffsetInArrayDimFetchRule>
*/
class NonexistentOffsetInArrayDimFetchRuleTest extends RuleTestCase
{
private bool $checkExplicitMixed = false;
private bool $checkImplicitMixed = false;
private bool $reportPossiblyNonexistentGeneralArrayOffset = false;
private bool $reportPossiblyNonexistentConstantArrayOffset = false;
protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false);
return new NonexistentOffsetInArrayDimFetchRule(
$ruleLevelHelper,
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, $this->reportPossiblyNonexistentGeneralArrayOffset, $this->reportPossiblyNonexistentConstantArrayOffset),
true,
);
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/nonexistent-offset.php'], [
[
'Offset \'b\' does not exist on array{a: stdClass, 0: 2}.',
17,
],
[
'Offset 1 does not exist on array{a: stdClass, 0: 2}.',
18,
],
[
'Offset \'a\' does not exist on array{b: 1}.',
55,
],
[
'Access to offset \'bar\' on an unknown class NonexistentOffset\Bar.',
101,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'Access to an offset on an unknown class NonexistentOffset\Bar.',
102,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'Offset 0 does not exist on array<string, string>.',
111,
],
[
'Offset \'0\' does not exist on array<string, string>.',
112,
],
[
'Offset int does not exist on array<string, string>.',
114,
],
[
'Offset \'test\' does not exist on null.',
126,
],
[
'Cannot access offset 42 on int.',
142,
],
[
'Cannot access offset 42 on float.',
143,
],
[
'Cannot access offset 42 on bool.',
144,
],
[
'Cannot access offset 42 on resource.',
145,
],
[
'Offset \'c\' might not exist on array{c: false}|array{c: true}|array{e: true}.',
171,
],
[
'Offset int might not exist on array{}|array{1: 1, 2: 2}|array{3: 3, 4: 4}.',
190,
],
[
'Offset int might not exist on array{}|array{1: 1, 2: 2}|array{3: 3, 4: 4}.',
193,
],
[
'Offset \'b\' does not exist on array{a: \'blabla\'}.',
225,
],
[
'Offset \'b\' does not exist on array{a: \'blabla\'}.',
228,
],
[
'Cannot access offset \'a\' on Closure(): void.',
253,
],
[
'Cannot access offset \'a\' on array{a: 1, b: 1}|(Closure(): void).',
258,
],
[
'Offset null does not exist on array<int, string>.',
310,
],
[
'Offset int does not exist on array<string, string>.',
312,
],
[
'Offset \'baz\' might not exist on array{bar: 1, baz?: 2}.',
344,
],
[
'Offset \'foo\' does not exist on ArrayAccess<int, stdClass>.',
411,
],
[
'Cannot access offset \'foo\' on stdClass.',
423,
],
[
'Cannot access offset \'foo\' on true.',
426,
],
[
'Cannot access offset \'foo\' on false.',
429,
],
[
'Cannot access offset \'foo\' on resource.',
433,
],
[
'Cannot access offset \'foo\' on 42.',
436,
],
[
'Cannot access offset \'foo\' on 4.141.',
439,
],
[
'Cannot access offset \'foo\' on array|int.',
443,
],
[
'Offset \'feature_pretty_version\' might not exist on array{version: non-falsy-string, commit: string|null, pretty_version: string|null, feature_version: non-falsy-string, feature_pretty_version?: string|null}.',
504,
],
[
"Cannot access offset 'foo' on bool.",
517,
],
]);
}
public function testStrings(): void
{
$this->analyse([__DIR__ . '/data/strings-offset-access.php'], [
[
'Offset \'foo\' does not exist on \'foo\'.',
10,
],
[
'Offset 12.34 does not exist on \'foo\'.',
13,
],
[
'Offset \'foo\' might not exist on array|string.',
24,
],
[
'Offset 12.34 might not exist on array|string.',
28,
],
]);
}
public function testAssignOp(): void
{
$this->analyse([__DIR__ . '/data/offset-access-assignop.php'], [
[
'Offset \'foo\' does not exist on array{}.',
4,
],
[
'Offset \'foo\' does not exist on \'Foo\'.',
10,
],
[
'Cannot access offset \'foo\' on stdClass.',
13,
],
[
'Cannot access offset \'foo\' on true.',
16,
],
[
'Cannot access offset \'foo\' on false.',
19,
],
[
'Cannot access offset \'foo\' on resource.',
23,
],
[
'Cannot access offset \'foo\' on 4.141.',
26,
],
[
'Cannot access offset \'foo\' on array|int.',
30,
],
[
'Cannot access offset \'foo\' on 42.',
33,
],
]);
}
public function testCoalesceAssign(): void
{
$this->analyse([__DIR__ . '/data/nonexistent-offset-coalesce-assign.php'], []);
}
public function testIntersection(): void
{
$this->analyse([__DIR__ . '/data/nonexistent-offset-intersection.php'], []);
}
public function testBug3782(): void
{
$this->analyse([__DIR__ . '/data/bug-3782.php'], [
[
'Cannot access offset (int|string) on $this(Bug3782\HelloWorld)|(ArrayAccess&Bug3782\HelloWorld).',
11,
],
]);
}
public function testBug4432(): void
{
$this->analyse([__DIR__ . '/data/bug-4432.php'], []);
}
public function testBug1664(): void
{
$this->analyse([__DIR__ . '/data/bug-1664.php'], []);
}
public function testBug2689(): void
{
$this->analyse([__DIR__ . '/data/bug-2689.php'], [
[
'Cannot access an offset on callable.',
14,
],
]);
}
public function testBug5169(): void
{
$this->analyse([__DIR__ . '/data/bug-5169.php'], [
[
'Cannot access offset mixed on (float|int).',
29,
],
]);
}
public function testBug3297(): void
{
$this->analyse([__DIR__ . '/data/bug-3297.php'], []);
}
public function testBug4829(): void
{
$this->analyse([__DIR__ . '/data/bug-4829.php'], []);
}
public function testBug3784(): void
{
$this->analyse([__DIR__ . '/data/bug-3784.php'], []);
}
public function testBug3700(): void
{
$this->analyse([__DIR__ . '/data/bug-3700.php'], []);
}
public function testBug4842(): void
{
$this->analyse([__DIR__ . '/data/bug-4842.php'], []);
}
public function testBug5669(): void
{
$this->analyse([__DIR__ . '/data/bug-5669.php'], [
[
'Access to offset \'%customer…\' on an unknown class Bug5669\arr.',
26,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
]);
}
public function testBug5744(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-5744.php'], [
[
'Cannot access offset \'permission\' on mixed.',
16,
],
[
'Cannot access offset \'permission\' on mixed.',
29,
],
[
'Cannot access offset \'permission\' on mixed.',
39,
],
]);
}
public function testRuleWithNullsafeVariant(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->analyse([__DIR__ . '/data/nonexistent-offset-nullsafe.php'], [
[
'Offset 1 does not exist on array{a: int}.',
18,
],
]);
}
public function testBug4926(): void
{
$this->analyse([__DIR__ . '/data/bug-4926.php'], []);
}
public function testBug3171(): void
{
$this->analyse([__DIR__ . '/data/bug-3171.php'], []);
}
public function testBug4747(): void
{
$this->analyse([__DIR__ . '/data/bug-4747.php'], []);
}
public function testBug6379(): void
{
$this->analyse([__DIR__ . '/data/bug-6379.php'], []);
}
public function testBug4885(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->analyse([__DIR__ . '/data/bug-4885.php'], []);
}
public function testBug7000(): void
{
$this->analyse([__DIR__ . '/data/bug-7000.php'], [
[
"Offset 'require'|'require-dev' might not exist on array{require?: array<string, string>, require-dev?: array<string, string>}.",
16,
],
]);
}
public function testBug6508(): void
{
$this->analyse([__DIR__ . '/data/bug-6508.php'], []);
}
public function testBug7229(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-7229.php'], [
[
'Cannot access offset string on mixed.',
24,
],
]);
}
public function testBug7142(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-7142.php'], []);
}
public function testBug6000(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-6000.php'], []);
}
public function testBug5743(): void
{
$this->analyse([__DIR__ . '/../Comparison/data/bug-5743.php'], [
[
'Offset 1|int<3, max> does not exist on array{}.',
10,
],
]);
}
public function testBug6364(): void
{
$this->analyse([__DIR__ . '/data/bug-6364.php'], []);
}
public function testBug5758(): void
{
$this->analyse([__DIR__ . '/data/bug-5758.php'], []);
}
public function testBug5223(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-5223.php'], [
[
'Offset \'something\' does not exist on array{categoryKeys: array<string>, tagNames: array<string>}.',
26,
],
[
'Offset \'something\' does not exist on array{categoryKeys: array<string>, tagNames: array<string>}.',
27,
],
[
'Offset \'something\' does not exist on array{categoryKeys: array<string>, tagNames: array<string>}.',
41,
],
[
'Offset \'something\' does not exist on array{categoryKeys: array<string>, tagNames: array<string>}.',
42,
],
]);
}
public function testBug7469(): void
{
$expected = [];
if (PHP_VERSION_ID < 80000) {
$expected = [
[
"Cannot access offset 'languages' on array<'address'|'bankAccount'|'birthDate'|'email'|'firstName'|'ic'|'invoicing'|'invoicingAddress'|'languages'|'lastName'|'note'|'phone'|'radio'|'videoOnline'|'videoTvc'|'voiceExample', mixed>|false.",
31,
],
[
"Cannot access offset 'languages' on array<'address'|'bankAccount'|'birthDate'|'email'|'firstName'|'ic'|'invoicing'|'invoicingAddress'|'languages'|'lastName'|'note'|'phone'|'radio'|'videoOnline'|'videoTvc'|'voiceExample', mixed>|false.",
31,
],
];
}
$this->analyse([__DIR__ . '/data/bug-7469.php'], $expected);
}
public function testBug7763(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}
$this->analyse([__DIR__ . '/data/bug-7763.php'], []);
}
public function testSpecifyExistentOffsetWhenEnteringForeach(): void
{
$this->analyse([__DIR__ . '/data/specify-existent-offset-when-entering-foreach.php'], []);
}
public function testBug3872(): void
{
$this->analyse([__DIR__ . '/data/bug-3872.php'], []);
}
public function testBug6783(): void
{
$this->analyse([__DIR__ . '/data/bug-6783.php'], []);
}
public function testSlevomatForeachUnsetBug(): void
{
$this->analyse([__DIR__ . '/data/slevomat-foreach-unset-bug.php'], []);
}
public function testSlevomatForeachArrayKeyExistsBug(): void
{
$this->analyse([__DIR__ . '/data/slevomat-foreach-array-key-exists-bug.php'], []);
}
public function testBug7954(): void
{
$this->analyse([__DIR__ . '/data/bug-7954.php'], []);
}
public function testBug8097(): void
{
$this->analyse([__DIR__ . '/data/bug-8097.php'], []);
}
public function testBug8068(): void
{
$this->analyse([__DIR__ . '/data/bug-8068.php'], [
[
"Cannot access offset 'path' on Closure.",
18,
],
[
"Cannot access offset 'path' on iterable<int|string, object>.",
26,
],
]);
}
public function testBug6243(): void
{
$this->analyse([__DIR__ . '/data/bug-6243.php'], []);
}
public function testBug8356(): void
{
$this->analyse([__DIR__ . '/data/bug-8356.php'], [
[
"Offset 'x' might not exist on array<mixed>|string.",
7,
],
]);
}
public function testBug6605(): void
{
$this->analyse([__DIR__ . '/data/bug-6605.php'], [
[
"Cannot access offset 'invalidoffset' on Bug6605\\X.",
11,
],
[
"Offset 'invalid' does not exist on array{a: array{b: array{5}}}.",
16,
],
[
"Offset 'invalid' does not exist on array{b: array{5}}.",
17,
],
]);
}
public function testBug9991(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-9991.php'], [
[
'Cannot access offset \'title\' on mixed.',
9,
],
]);
}
public function testBug8166(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-8166.php'], [
[
'Offset \'b\' does not exist on array{a: 1}.',
22,
],
[
'Offset \'b\' does not exist on array<\'a\', string>.',
23,
],
]);
}
public function testBug10926(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-10926.php'], [
[
'Cannot access offset \'a\' on stdClass.',
10,
],
]);
}
public function testMixed(): void
{
if (PHP_VERSION_ID < 80000) {
self::markTestSkipped('Test requires PHP 8.0.');
}
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/offset-access-mixed.php'], [
[
'Cannot access offset 5 on T of mixed.',
11,
],
[
'Cannot access offset 5 on mixed.',
16,
],
[
'Cannot access offset 5 on mixed.',
21,
],
]);
}
public function testOffsetAccessLegal(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/offset-access-legal.php'], [
[
'Cannot access offset 0 on Closure(): void.',
7,
],
[
'Cannot access offset 0 on stdClass.',
12,
],
[
'Cannot access offset 0 on array{\'test\'}|stdClass.',
96,
],
[
'Cannot access offset 0 on array{\'test\'}|(Closure(): void).',
98,
],
]);
}
public function testNonExistentParentOffsetAccessLegal(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/offset-access-legal-non-existent-parent.php'], [
[
'Cannot access offset 0 on parent.',
9,
],
]);
}
public function dataReportPossiblyNonexistentArrayOffset(): iterable
{
yield [false, false, []];
yield [false, true, [
[
'Offset string might not exist on array{foo: 1}.',
20,
],
]];
yield [true, false, [
[
"Offset 'foo' might not exist on array.",
9,
],
]];
yield [true, true, [
[
"Offset 'foo' might not exist on array.",
9,
],
[
'Offset string might not exist on array{foo: 1}.',
20,
],
]];
}
/**
* @dataProvider dataReportPossiblyNonexistentArrayOffset
* @param list<array{0: string, 1: int, 2?: string|null}> $errors
*/
public function testReportPossiblyNonexistentArrayOffset(bool $reportPossiblyNonexistentGeneralArrayOffset, bool $reportPossiblyNonexistentConstantArrayOffset, array $errors): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = $reportPossiblyNonexistentGeneralArrayOffset;
$this->reportPossiblyNonexistentConstantArrayOffset = $reportPossiblyNonexistentConstantArrayOffset;
$this->analyse([__DIR__ . '/data/report-possibly-nonexistent-array-offset.php'], $errors);
}
public function testBug10997(): void
{
$this->reportPossiblyNonexistentConstantArrayOffset = true;
$this->analyse([__DIR__ . '/data/bug-10997.php'], [
[
'Offset int<0, 4> might not exist on array{1, 2, 3, 4}.',
15,
],
]);
}
public function testBug11572(): void
{
$this->analyse([__DIR__ . '/data/bug-11572.php'], [
[
'Cannot access an offset on int.',
45,
],
[
'Cannot access an offset on int<3, 4>.',
46,
],
]);
}
public function testBug2313(): void
{
$this->analyse([__DIR__ . '/data/bug-2313.php'], []);
}
public function testBug11655(): void
{
$this->analyse([__DIR__ . '/data/bug-11655.php'], [
[
"Offset 3 does not exist on array{string, 'x', array{string, 'x'}}.",
15,
],
]);
}
public function testBug2634(): void
{
$this->analyse([__DIR__ . '/data/bug-2634.php'], []);
}
public function testInternalClassesWithOverloadedOffsetAccess(): void
{
$this->analyse([__DIR__ . '/data/internal-classes-overload-offset-access.php'], []);
}
public function testInternalClassesWithOverloadedOffsetAccess84(): void
{
if (PHP_VERSION_ID < 80400) {
$this->markTestSkipped('Test requires PHP 8.4.');
}
$this->analyse([__DIR__ . '/data/internal-classes-overload-offset-access-php84.php'], []);
}
public function testInternalClassesWithOverloadedOffsetAccessInvalid(): void
{
$this->analyse([__DIR__ . '/data/internal-classes-overload-offset-access-invalid.php'], []);
}
public function testInternalClassesWithOverloadedOffsetAccessInvalid84(): void
{
if (PHP_VERSION_ID < 80400) {
$this->markTestSkipped('Test requires PHP 8.4.');
}
$this->analyse([__DIR__ . '/data/internal-classes-overload-offset-access-invalid-php84.php'], []);
}
public function testBug12122(): void
{
$this->analyse([__DIR__ . '/data/bug-12122.php'], []);
}
public function testArrayDimFetchAfterArrayKeyFirstOrLast(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
$this->analyse([__DIR__ . '/data/array-dim-after-array-key-first-or-last.php'], [
[
'Offset null does not exist on array{}.',
19,
],
]);
}
public function testArrayDimFetchAfterCount(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
$this->analyse([__DIR__ . '/data/array-dim-after-count.php'], [
[
'Offset int<0, max> might not exist on list<string>.',
26,
],
[
'Offset int<-1, max> might not exist on array<string>.',
35,
],
[
'Offset int<0, max> might not exist on non-empty-array<string>.',
42,
],
]);
}
public function testArrayDimFetchAfterArraySearch(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
$this->analyse([__DIR__ . '/data/array-dim-after-array-search.php'], [
[
'Offset int|string might not exist on array.',
20,
],
]);
}
public function testBug11679(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
$this->analyse([__DIR__ . '/data/bug-11679.php'], []);
}
}