-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathAnalyserTest.php
774 lines (700 loc) · 24.9 KB
/
AnalyserTest.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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Lexer;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser\Php7;
use PHPStan\Analyser\Ignore\IgnoredErrorHelper;
use PHPStan\Analyser\Ignore\IgnoreLexer;
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\Dependency\DependencyResolver;
use PHPStan\Dependency\ExportedNodeResolver;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Node\Printer\Printer;
use PHPStan\Parser\RichParser;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\Rules\AlwaysFailRule;
use PHPStan\Rules\DirectRegistry as DirectRuleRegistry;
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\ParameterClosureTypeHelper;
use stdClass;
use function array_map;
use function array_merge;
use function assert;
use function count;
use function is_string;
use function sprintf;
use function str_replace;
use function strtoupper;
use function substr;
use const PHP_OS;
class AnalyserTest extends PHPStanTestCase
{
public function testReturnErrorIfIgnoredMessagesDoesNotOccur(): void
{
$result = $this->runAnalyser(['#Unknown error#'], true, __DIR__ . '/data/empty/empty.php', false);
$this->assertSame([
'Ignored error pattern #Unknown error# was not matched in reported errors.',
], $result);
}
public function testDoNotReturnErrorIfIgnoredMessagesDoesNotOccurWithReportUnmatchedIgnoredErrorsOff(): void
{
$result = $this->runAnalyser(['#Unknown error#'], false, __DIR__ . '/data/empty/empty.php', false);
$this->assertEmpty($result);
}
public function testDoNotReturnErrorIfIgnoredMessagesDoNotOccurWhileAnalysingIndividualFiles(): void
{
$result = $this->runAnalyser(['#Unknown error#'], true, __DIR__ . '/data/empty/empty.php', true);
$this->assertEmpty($result);
}
public function testFileWithAnIgnoredError(): void
{
$result = $this->runAnalyser(['#Fail\.#'], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}
public function testFileWithAnIgnoredErrorMessage(): void
{
$result = $this->runAnalyser([['message' => '#Fail\.#']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}
public function testFileWithAnIgnoredErrorMessageAndWrongIdentifier(): void
{
$result = $this->runAnalyser([['message' => '#Fail\.#', 'identifier' => 'wrong.identifier']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(2, $result);
assert($result[0] instanceof Error);
$this->assertSame('Fail.', $result[0]->getMessage());
assert(is_string($result[1]));
$this->assertSame('Ignored error pattern #Fail\.# (wrong.identifier) was not matched in reported errors.', $result[1]);
}
public function testFileWithAnIgnoredWrongIdentifier(): void
{
$result = $this->runAnalyser([['identifier' => 'wrong.identifier']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(2, $result);
assert($result[0] instanceof Error);
$this->assertSame('Fail.', $result[0]->getMessage());
assert(is_string($result[1]));
$this->assertSame('Ignored error pattern wrong.identifier was not matched in reported errors.', $result[1]);
}
public function testFileWithAnIgnoredErrorMessageAndCorrectIdentifier(): void
{
$result = $this->runAnalyser([['message' => '#Fail\.#', 'identifier' => 'tests.alwaysFail']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}
public function testFileWithAnIgnoredErrorIdentifier(): void
{
$result = $this->runAnalyser([['identifier' => 'tests.alwaysFail']], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEmpty($result);
}
public function testFileWithAnIgnoredErrorMessages(): void
{
$result = $this->runAnalyser([['messages' => ['#Fail\.#']]], true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertEquals([], $result);
}
public function testIgnoringBrokenConfigurationDoesNotWork(): void
{
$this->markTestIncomplete();
$result = $this->runAnalyser(['#was not found while trying to analyse it#'], true, __DIR__ . '/../../notAutoloaded/Baz.php', false);
$this->assertCount(2, $result);
assert($result[0] instanceof Error);
$this->assertSame('Class PHPStan\Tests\Baz was not found while trying to analyse it - autoloading is probably not configured properly.', $result[0]->getMessage());
$this->assertSame('Error message "Class PHPStan\Tests\Baz was not found while trying to analyse it - autoloading is probably not configured properly." cannot be ignored, use excludePaths instead.', $result[1]);
}
public function testIgnoreErrorByPath(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/bootstrap-error.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertNoErrors($result);
}
public function testIgnoreErrorMultiByPath(): void
{
$ignoreErrors = [
[
'messages' => [
'#First fail#',
'#Second fail#',
],
'path' => __DIR__ . '/data/two-different-fails.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-different-fails.php', false);
$this->assertNoErrors($result);
}
public function dataIgnoreErrorByPathAndCount(): iterable
{
yield [
[
[
'message' => '#Fail\.#',
'count' => 3,
'path' => __DIR__ . '/data/two-fails.php',
],
],
];
yield [
[
[
'message' => '#Fail\.#',
'count' => 2,
'path' => __DIR__ . '/data/two-fails.php',
],
[
'message' => '#Fail\.#',
'count' => 1,
'path' => __DIR__ . '/data/two-fails.php',
],
],
];
yield [
[
[
'message' => '#Fail\.#',
'count' => 2,
'path' => __DIR__ . '/data/two-fails.php',
],
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/two-fails.php',
],
],
];
}
/**
* @dataProvider dataIgnoreErrorByPathAndCount
* @param mixed[] $ignoreErrors
*/
public function testIgnoreErrorByPathAndCount(array $ignoreErrors): void
{
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', false);
$this->assertNoErrors($result);
}
public function dataTrueAndFalse(): array
{
return [
[true],
[false],
];
}
/**
* @dataProvider dataTrueAndFalse
*/
public function testIgnoreErrorByPathAndIdentifierCountsCorrectly(bool $onlyFiles): void
{
$ignoreErrors = [
[
'identifier' => 'tests.alwaysFail',
'count' => 3,
'path' => __DIR__ . '/data/two-fails.php',
],
[
'identifier' => 'tests.alwaysFail',
'count' => 2,
'path' => __DIR__ . '/data/two-different-fails.php',
],
];
$filesToAnalyze = [
__DIR__ . '/data/two-fails.php',
__DIR__ . '/data/two-different-fails.php',
];
$result = $this->runAnalyser($ignoreErrors, true, $filesToAnalyze, $onlyFiles);
$this->assertNoErrors($result);
}
/**
* @dataProvider dataTrueAndFalse
*/
public function testIgnoreErrorByPathAndCountMoreThanExpected(bool $onlyFiles): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'count' => 1,
'path' => __DIR__ . '/data/two-fails.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', $onlyFiles);
$this->assertCount(3, $result);
$this->assertInstanceOf(Error::class, $result[0]);
$this->assertSame('Fail.', $result[0]->getMessage());
$this->assertSame(6, $result[0]->getLine());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[0]->getFile());
$this->assertInstanceOf(Error::class, $result[1]);
$this->assertSame('Fail.', $result[1]->getMessage());
$this->assertSame(7, $result[1]->getLine());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[1]->getFile());
$this->assertInstanceOf(Error::class, $result[2]);
$this->assertStringContainsString('Ignored error pattern #Fail\.#', $result[2]->getMessage());
$this->assertStringContainsString('is expected to occur 1 time, but occurred 3 times.', $result[2]->getMessage());
$this->assertSame(5, $result[2]->getLine());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[2]->getFile());
}
/**
* @dataProvider dataTrueAndFalse
*/
public function testIgnoreErrorByPathAndCountLessThanExpected(bool $onlyFiles): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'count' => 4,
'path' => __DIR__ . '/data/two-fails.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', $onlyFiles);
$this->assertCount(1, $result);
$this->assertInstanceOf(Error::class, $result[0]);
$this->assertStringContainsString('Ignored error pattern #Fail\.#', $result[0]->getMessage());
$this->assertStringContainsString('is expected to occur 4 times, but occurred only 3 times.', $result[0]->getMessage());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[0]->getFile());
$this->assertSame(5, $result[0]->getLine());
}
public function testIgnoreErrorByPathAndCountMissing(): void
{
$ignoreErrors = [
[
'message' => '#Some custom error\.#',
'count' => 2,
'path' => __DIR__ . '/data/two-fails.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', false);
$this->assertCount(4, $result);
$this->assertInstanceOf(Error::class, $result[0]);
$this->assertSame('Fail.', $result[0]->getMessage());
$this->assertSame(5, $result[0]->getLine());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[0]->getFile());
$this->assertInstanceOf(Error::class, $result[1]);
$this->assertSame('Fail.', $result[1]->getMessage());
$this->assertSame(6, $result[1]->getLine());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[1]->getFile());
$this->assertInstanceOf(Error::class, $result[2]);
$this->assertSame('Fail.', $result[2]->getMessage());
$this->assertSame(7, $result[2]->getLine());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[2]->getFile());
$this->assertInstanceOf(Error::class, $result[3]);
$this->assertStringContainsString('Ignored error pattern #Some custom error\.# in path', $result[3]->getMessage());
$this->assertStringContainsString('was not matched in reported errors.', $result[3]->getMessage());
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[2]->getFile());
}
public function testIgnoreErrorByPaths(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'paths' => [__DIR__ . '/data/bootstrap-error.php'],
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertNoErrors($result);
}
public function testIgnoreErrorMultiByPaths(): void
{
$ignoreErrors = [
[
'messages' => [
'#First fail#',
'#Second fail#',
],
'paths' => [__DIR__ . '/data/two-different-fails.php'],
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-different-fails.php', false);
$this->assertNoErrors($result);
}
public function testIgnoreErrorByPathsMultipleUnmatched(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'paths' => [__DIR__ . '/data/bootstrap-error.php', __DIR__ . '/data/another-path.php', '/data/yet-another-path.php'],
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(1, $result);
$this->assertIsString($result[0]);
$this->assertStringContainsString('Ignored error pattern #Fail\.# in paths: ', $result[0]);
$this->assertStringContainsString('was not matched in reported errors', $result[0]);
}
public function testIgnoreErrorByPathsUnmatched(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'paths' => [__DIR__ . '/data/bootstrap-error.php', __DIR__ . '/data/another-path.php'],
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(1, $result);
$this->assertIsString($result[0]);
$this->assertStringContainsString('Ignored error pattern #Fail\.# in path ', $result[0]);
$this->assertStringContainsString('was not matched in reported errors', $result[0]);
}
public function testIgnoreErrorByPathsUnmatchedExplicitReportUnmatched(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'paths' => [__DIR__ . '/data/bootstrap-error.php', __DIR__ . '/data/another-path.php'],
'reportUnmatched' => true,
],
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap-error.php', false);
$this->assertCount(1, $result);
$this->assertIsString($result[0]);
$this->assertStringContainsString('Ignored error pattern #Fail\.# in path ', $result[0]);
$this->assertStringContainsString('was not matched in reported errors', $result[0]);
}
public function testIgnoreErrorNotFoundInPath(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/not-existent-path.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/empty/empty.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error pattern #Fail\.# in path ' . __DIR__ . '/data/not-existent-path.php was not matched in reported errors.', $result[0]);
}
public function testIgnoreErrorNotFoundInPathExplicitReportUnmatched(): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/not-existent-path.php',
'reportUnmatched' => true,
],
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/empty/empty.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error pattern #Fail\.# in path ' . __DIR__ . '/data/not-existent-path.php was not matched in reported errors.', $result[0]);
}
public function dataIgnoreErrorInTraitUsingClassFilePath(): array
{
return [
[
__DIR__ . '/data/traits-ignore/Foo.php',
],
[
__DIR__ . '/data/traits-ignore/FooTrait.php',
],
];
}
/**
* @dataProvider dataIgnoreErrorInTraitUsingClassFilePath
*/
public function testIgnoreErrorInTraitUsingClassFilePath(string $pathToIgnore): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => $pathToIgnore,
],
];
$result = $this->runAnalyser($ignoreErrors, true, [
__DIR__ . '/data/traits-ignore/Foo.php',
__DIR__ . '/data/traits-ignore/FooTrait.php',
], true);
$this->assertNoErrors($result);
}
public function testIgnoredErrorMissingMessage(): void
{
$ignoreErrors = [
[
'path' => __DIR__ . '/data/empty/empty.php',
],
];
$expectedPath = __DIR__;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$expectedPath = str_replace('\\', '\\\\', $expectedPath);
}
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/empty/empty.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error {"path":"' . $expectedPath . '/data/empty/empty.php"} is missing a message or an identifier.', $result[0]);
}
public function testReportMultipleParserErrorsAtOnce(): void
{
$result = $this->runAnalyser([], false, __DIR__ . '/data/multipleParseErrors.php', false);
$this->assertCount(2, $result);
/** @var Error $errorOne */
$errorOne = $result[0];
$this->assertSame('Syntax error, unexpected T_IS_EQUAL, expecting T_VARIABLE on line 3', $errorOne->getMessage());
$this->assertSame(3, $errorOne->getLine());
/** @var Error $errorTwo */
$errorTwo = $result[1];
$this->assertSame('Syntax error, unexpected EOF on line 10', $errorTwo->getMessage());
$this->assertSame(10, $errorTwo->getLine());
}
/**
* @dataProvider dataTrueAndFalse
*/
public function testDoNotReportUnmatchedIgnoredErrorsFromPathIfPathWasNotAnalysed(bool $onlyFiles): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/bootstrap-error.php',
],
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/two-fails.php',
],
];
$result = $this->runAnalyser($ignoreErrors, true, [
__DIR__ . '/data/two-fails.php',
], $onlyFiles);
$this->assertNoErrors($result);
}
/**
* @dataProvider dataTrueAndFalse
*/
public function testDoNotReportUnmatchedIgnoredErrorsFromPathWithCountIfPathWasNotAnalysed(bool $onlyFiles): void
{
$ignoreErrors = [
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/bootstrap-error.php',
'count' => 2,
],
[
'message' => '#Fail\.#',
'path' => __DIR__ . '/data/two-fails.php',
'count' => 3,
],
];
$result = $this->runAnalyser($ignoreErrors, true, [
__DIR__ . '/data/two-fails.php',
], $onlyFiles);
$this->assertNoErrors($result);
}
public function testIgnoreNextLine(): void
{
$result = $this->runAnalyser([], false, [
__DIR__ . '/data/ignore-next-line.php',
], true);
$this->assertCount(5, $result);
foreach ([10, 20, 24, 31, 50] as $i => $line) {
$this->assertArrayHasKey($i, $result);
$this->assertInstanceOf(Error::class, $result[$i]);
$this->assertSame('Fail.', $result[$i]->getMessage());
$this->assertSame($line, $result[$i]->getLine());
}
}
public function testIgnoreNextLineUnmatched(): void
{
$result = $this->runAnalyser([], true, [
__DIR__ . '/data/ignore-next-line-unmatched.php',
], true);
$this->assertCount(2, $result);
foreach ([11, 15] as $i => $line) {
$this->assertArrayHasKey($i, $result);
$this->assertInstanceOf(Error::class, $result[$i]);
$this->assertStringContainsString('No error to ignore is reported on line', $result[$i]->getMessage());
$this->assertSame($line, $result[$i]->getLine());
}
}
public function testIgnoreNextLineLegacyBehaviour(): void
{
$result = $this->runAnalyser([], false, [__DIR__ . '/data/ignore-next-line-legacy.php'], true, false);
foreach ([10, 32, 36] as $i => $line) {
$this->assertArrayHasKey($i, $result);
$this->assertInstanceOf(Error::class, $result[$i]);
$this->assertSame('Fail.', $result[$i]->getMessage());
$this->assertSame($line, $result[$i]->getLine());
}
}
/**
* @dataProvider dataTrueAndFalse
*/
public function testIgnoreLine(bool $reportUnmatchedIgnoredErrors): void
{
$result = $this->runAnalyser([], $reportUnmatchedIgnoredErrors, [
__DIR__ . '/data/ignore-line.php',
], true);
$this->assertCount($reportUnmatchedIgnoredErrors ? 4 : 3, $result);
foreach ([10, 19, 22] as $i => $line) {
$this->assertArrayHasKey($i, $result);
$this->assertInstanceOf(Error::class, $result[$i]);
$this->assertSame('Fail.', $result[$i]->getMessage());
$this->assertSame($line, $result[$i]->getLine());
}
if (!$reportUnmatchedIgnoredErrors) {
return;
}
$this->assertArrayHasKey(3, $result);
$this->assertInstanceOf(Error::class, $result[3]);
$this->assertSame('No error to ignore is reported on line 26.', $result[3]->getMessage());
$this->assertSame(26, $result[3]->getLine());
}
public function testIgnoreErrorExplicitReportUnmatchedDisable(): void
{
$ignoreErrors = [
[
'message' => '#Fail#',
'reportUnmatched' => false,
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap.php', false);
$this->assertNoErrors($result);
}
public function testIgnoreErrorExplicitReportUnmatchedDisableMulti(): void
{
$ignoreErrors = [
[
'message' => ['#Fail#'],
'reportUnmatched' => false,
],
];
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/bootstrap.php', false);
$this->assertNoErrors($result);
}
public function testIgnoreErrorExplicitReportUnmatchedEnable(): void
{
$ignoreErrors = [
[
'message' => '#Fail#',
'reportUnmatched' => true,
],
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error pattern #Fail# was not matched in reported errors.', $result[0]);
}
public function testIgnoreErrorExplicitReportUnmatchedEnableMulti(): void
{
$ignoreErrors = [
[
'messages' => ['#Fail#'],
'reportUnmatched' => true,
],
];
$result = $this->runAnalyser($ignoreErrors, false, __DIR__ . '/data/bootstrap.php', false);
$this->assertCount(1, $result);
$this->assertSame('Ignored error pattern #Fail# was not matched in reported errors.', $result[0]);
}
/**
* @param mixed[] $ignoreErrors
* @param string|string[] $filePaths
* @return string[]|Error[]
*/
private function runAnalyser(
array $ignoreErrors,
bool $reportUnmatchedIgnoredErrors,
$filePaths,
bool $onlyFiles,
bool $enableIgnoreErrorsWithinPhpDocs = true,
): array
{
$analyser = $this->createAnalyser($enableIgnoreErrorsWithinPhpDocs);
if (is_string($filePaths)) {
$filePaths = [$filePaths];
}
$ignoredErrorHelper = new IgnoredErrorHelper(
$this->getFileHelper(),
$ignoreErrors,
$reportUnmatchedIgnoredErrors,
);
$ignoredErrorHelperResult = $ignoredErrorHelper->initialize();
if (count($ignoredErrorHelperResult->getErrors()) > 0) {
return $ignoredErrorHelperResult->getErrors();
}
$normalizedFilePaths = array_map(fn (string $path): string => $this->getFileHelper()->normalizePath($path), $filePaths);
$analyserResult = $analyser->analyse($normalizedFilePaths);
$finalizer = new AnalyserResultFinalizer(
new DirectRuleRegistry([]),
new RuleErrorTransformer(),
$this->createScopeFactory(
$this->createReflectionProvider(),
self::getContainer()->getService('typeSpecifier'),
),
new LocalIgnoresProcessor(),
$reportUnmatchedIgnoredErrors,
);
$analyserResult = $finalizer->finalize($analyserResult, $onlyFiles, false)->getAnalyserResult();
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $normalizedFilePaths, $analyserResult->hasReachedInternalErrorsCountLimit());
$errors = $ignoredErrorHelperProcessedResult->getNotIgnoredErrors();
$errors = array_merge($errors, $ignoredErrorHelperProcessedResult->getOtherIgnoreMessages());
if ($analyserResult->hasReachedInternalErrorsCountLimit()) {
$errors[] = sprintf('Reached internal errors count limit of %d, exiting...', 50);
}
return array_merge(
$errors,
array_map(static fn (InternalError $internalError) => $internalError->getMessage(), $analyserResult->getInternalErrors()),
);
}
private function createAnalyser(bool $enableIgnoreErrorsWithinPhpDocs): Analyser
{
$ruleRegistry = new DirectRuleRegistry([
new AlwaysFailRule(),
]);
$collectorRegistry = new CollectorRegistry([]);
$reflectionProvider = $this->createReflectionProvider();
$fileHelper = $this->getFileHelper();
$typeSpecifier = self::getContainer()->getService('typeSpecifier');
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
$phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper, self::getContainer()->getByType(StubPhpDocProvider::class));
$nodeScopeResolver = new NodeScopeResolver(
$reflectionProvider,
self::getContainer()->getByType(InitializerExprTypeResolver::class),
self::getReflector(),
self::getClassReflectionExtensionRegistryProvider(),
self::getContainer()->getByType(ParameterOutTypeExtensionProvider::class),
$this->getParser(),
$fileTypeMapper,
self::getContainer()->getByType(StubPhpDocProvider::class),
self::getContainer()->getByType(PhpVersion::class),
self::getContainer()->getByType(SignatureMapProvider::class),
$phpDocInheritanceResolver,
$fileHelper,
$typeSpecifier,
self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class),
self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class),
self::getContainer()->getByType(ParameterClosureTypeHelper::class),
self::createScopeFactory($reflectionProvider, $typeSpecifier),
false,
true,
[],
[],
[stdClass::class],
true,
$this->shouldTreatPhpDocTypesAsCertain(),
self::getContainer()->getParameter('featureToggles')['detectDeadTypeInMultiCatch'],
self::getContainer()->getParameter('featureToggles')['paramOutType'],
self::getContainer()->getParameter('featureToggles')['preciseMissingReturn'],
self::getContainer()->getParameter('featureToggles')['explicitThrow'],
);
$lexer = new Lexer(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos']]);
$fileAnalyser = new FileAnalyser(
$this->createScopeFactory($reflectionProvider, $typeSpecifier),
$nodeScopeResolver,
new RichParser(
new Php7($lexer),
$lexer,
new NameResolver(),
self::getContainer(),
new IgnoreLexer(),
$enableIgnoreErrorsWithinPhpDocs,
),
new DependencyResolver($fileHelper, $reflectionProvider, new ExportedNodeResolver($fileTypeMapper, new ExprPrinter(new Printer())), $fileTypeMapper),
new RuleErrorTransformer(),
new LocalIgnoresProcessor(),
);
return new Analyser(
$fileAnalyser,
$ruleRegistry,
$collectorRegistry,
$nodeScopeResolver,
50,
);
}
}