Skip to content

Commit 785b663

Browse files
committed
Fixed local ignoring using annotations in traits
1 parent 8633000 commit 785b663

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

src/Analyser/FileAnalyser.php

+23-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Roave\BetterReflection\Reflection\Exception\NotAClassReflection;
1919
use Roave\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
2020
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
21-
use function array_fill_keys;
2221
use function array_key_exists;
2322
use function array_unique;
2423

@@ -165,7 +164,7 @@ public function analyseFile(
165164
}
166165

167166
foreach ($this->getLinesToIgnore($node) as $lineToIgnore) {
168-
$linesToIgnore[] = $lineToIgnore;
167+
$linesToIgnore[$scope->getFileDescription()][$lineToIgnore] = true;
169168
}
170169

171170
try {
@@ -192,43 +191,42 @@ public function analyseFile(
192191
$scope,
193192
$nodeCallback
194193
);
195-
$linesToIgnoreKeys = array_fill_keys($linesToIgnore, true);
196-
$unmatchedLineIgnores = $linesToIgnoreKeys;
194+
$unmatchedLineIgnores = $linesToIgnore;
197195
foreach ($temporaryFileErrors as $tmpFileError) {
198196
$line = $tmpFileError->getLine();
199197
if (
200198
$line !== null
201199
&& $tmpFileError->canBeIgnored()
202-
&& array_key_exists($line, $linesToIgnoreKeys)
200+
&& array_key_exists($tmpFileError->getFile(), $linesToIgnore)
201+
&& array_key_exists($line, $linesToIgnore[$tmpFileError->getFile()])
203202
) {
204-
unset($unmatchedLineIgnores[$line]);
203+
unset($unmatchedLineIgnores[$tmpFileError->getFile()][$line]);
205204
continue;
206205
}
207206

208207
$fileErrors[] = $tmpFileError;
209208
}
210209

211210
if ($this->reportUnmatchedIgnoredErrors) {
212-
foreach (array_keys($unmatchedLineIgnores) as $line) {
213-
$traitFilePath = null;
214-
if ($scope->isInTrait()) {
215-
$traitReflection = $scope->getTraitReflection();
216-
if ($traitReflection->getFileName() !== false) {
217-
$traitFilePath = $traitReflection->getFileName();
218-
}
211+
foreach ($unmatchedLineIgnores as $ignoredFile => $lines) {
212+
if ($ignoredFile !== $file) {
213+
continue;
214+
}
215+
216+
foreach (array_keys($lines) as $line) {
217+
$fileErrors[] = new Error(
218+
sprintf('No error to ignore is reported on line %d.', $line),
219+
$scope->getFileDescription(),
220+
$line,
221+
false,
222+
$scope->getFile(),
223+
null,
224+
null,
225+
null,
226+
null,
227+
'ignoredError.unmatchedOnLine'
228+
);
219229
}
220-
$fileErrors[] = new Error(
221-
sprintf('No error to ignore is reported on line %d.', $line),
222-
$scope->getFileDescription(),
223-
$line,
224-
false,
225-
$scope->getFile(),
226-
$traitFilePath,
227-
null,
228-
null,
229-
null,
230-
'ignoredError.unmatchedOnLine'
231-
);
232230
}
233231
}
234232
} catch (\PhpParser\Error $e) {

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ public function testBug4300(): void
299299
$this->assertSame(13, $errors[0]->getLine());
300300
}
301301

302+
public function testBug4513(): void
303+
{
304+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4513.php');
305+
$this->assertCount(0, $errors);
306+
}
307+
302308
/**
303309
* @param string $file
304310
* @return \PHPStan\Analyser\Error[]
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bug4513;
4+
5+
class Foo
6+
{
7+
8+
use BarTrait;
9+
10+
}
11+
12+
trait BarTrait
13+
{
14+
15+
public function doFoo(): void
16+
{
17+
// @phpstan-ignore-next-line
18+
echo 'foo';
19+
}
20+
21+
public function doBar(): void
22+
{
23+
// @phpstan-ignore-next-line
24+
echo [];
25+
}
26+
27+
}

0 commit comments

Comments
 (0)