Skip to content

Commit eec492e

Browse files
committed
Report "Ignored error pattern is expected to occur N times but occured only M times" as file-specific
1 parent a170ff3 commit eec492e

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/Analyser/IgnoredErrorHelperResult.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ public function process(array $errors, bool $onlyFiles, bool $reachedInternalErr
9797
$realCount++;
9898
$unmatchedIgnoredErrors[$i]['realCount'] = $realCount;
9999

100+
if (!isset($unmatchedIgnoredErrors[$i]['file'])) {
101+
$unmatchedIgnoredErrors[$i]['file'] = $error->getFile();
102+
$unmatchedIgnoredErrors[$i]['line'] = $error->getLine();
103+
}
104+
100105
if ($realCount > $ignore['count']) {
101106
$shouldBeIgnored = false;
102-
if (!isset($unmatchedIgnoredErrors[$i]['file'])) {
103-
$unmatchedIgnoredErrors[$i]['file'] = $error->getFile();
104-
$unmatchedIgnoredErrors[$i]['line'] = $error->getLine();
105-
}
106107
}
107108
} else {
108109
unset($unmatchedIgnoredErrors[$i]);
@@ -213,14 +214,14 @@ public function process(array $errors, bool $onlyFiles, bool $reachedInternalErr
213214
&& (isset($unmatchedIgnoredError['realPath']) || !$onlyFiles)
214215
) {
215216
if ($unmatchedIgnoredError['realCount'] < $unmatchedIgnoredError['count']) {
216-
$errors[] = sprintf(
217+
$errors[] = new Error(sprintf(
217218
'Ignored error pattern %s is expected to occur %d %s, but occured only %d %s.',
218219
IgnoredError::stringifyPattern($unmatchedIgnoredError),
219220
$unmatchedIgnoredError['count'],
220221
$unmatchedIgnoredError['count'] === 1 ? 'time' : 'times',
221222
$unmatchedIgnoredError['realCount'],
222223
$unmatchedIgnoredError['realCount'] === 1 ? 'time' : 'times'
223-
);
224+
), $unmatchedIgnoredError['file'], $unmatchedIgnoredError['line'], false);
224225
}
225226
} elseif (!$onlyFiles) {
226227
$errors[] = sprintf(

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testIgnoreErrorByPathAndCountMoreThanExpected(bool $onlyFiles):
123123
$this->assertInstanceOf(Error::class, $result[2]);
124124
$this->assertStringContainsString('Ignored error pattern #Fail\.#', $result[2]->getMessage());
125125
$this->assertStringContainsString('is expected to occur 1 time, but occured 3 times.', $result[2]->getMessage());
126-
$this->assertSame(6, $result[2]->getLine());
126+
$this->assertSame(5, $result[2]->getLine());
127127
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[2]->getFile());
128128
}
129129

@@ -142,9 +142,11 @@ public function testIgnoreErrorByPathAndCountLessThanExpected(bool $onlyFiles):
142142
];
143143
$result = $this->runAnalyser($ignoreErrors, true, __DIR__ . '/data/two-fails.php', $onlyFiles);
144144
$this->assertCount(1, $result);
145-
$this->assertIsString($result[0]);
146-
$this->assertStringContainsString('Ignored error pattern #Fail\.#', $result[0]);
147-
$this->assertStringContainsString('is expected to occur 4 times, but occured only 3 times.', $result[0]);
145+
$this->assertInstanceOf(Error::class, $result[0]);
146+
$this->assertStringContainsString('Ignored error pattern #Fail\.#', $result[0]->getMessage());
147+
$this->assertStringContainsString('is expected to occur 4 times, but occured only 3 times.', $result[0]->getMessage());
148+
$this->assertSamePaths(__DIR__ . '/data/two-fails.php', $result[0]->getFile());
149+
$this->assertSame(5, $result[0]->getLine());
148150
}
149151

150152
public function testIgnoreErrorByPathAndCountMissing(): void

0 commit comments

Comments
 (0)