From 785b66311ed7591710b189322c7d71fcfa1c3409 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 10 Feb 2021 17:17:31 +0100 Subject: [PATCH] Fixed local ignoring using annotations in traits --- src/Analyser/FileAnalyser.php | 48 +++++++++---------- .../Analyser/AnalyserIntegrationTest.php | 6 +++ tests/PHPStan/Analyser/data/bug-4513.php | 27 +++++++++++ 3 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 tests/PHPStan/Analyser/data/bug-4513.php diff --git a/src/Analyser/FileAnalyser.php b/src/Analyser/FileAnalyser.php index bbc10934ed..f5acdb13ff 100644 --- a/src/Analyser/FileAnalyser.php +++ b/src/Analyser/FileAnalyser.php @@ -18,7 +18,6 @@ use Roave\BetterReflection\Reflection\Exception\NotAClassReflection; use Roave\BetterReflection\Reflection\Exception\NotAnInterfaceReflection; use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound; -use function array_fill_keys; use function array_key_exists; use function array_unique; @@ -165,7 +164,7 @@ public function analyseFile( } foreach ($this->getLinesToIgnore($node) as $lineToIgnore) { - $linesToIgnore[] = $lineToIgnore; + $linesToIgnore[$scope->getFileDescription()][$lineToIgnore] = true; } try { @@ -192,16 +191,16 @@ public function analyseFile( $scope, $nodeCallback ); - $linesToIgnoreKeys = array_fill_keys($linesToIgnore, true); - $unmatchedLineIgnores = $linesToIgnoreKeys; + $unmatchedLineIgnores = $linesToIgnore; foreach ($temporaryFileErrors as $tmpFileError) { $line = $tmpFileError->getLine(); if ( $line !== null && $tmpFileError->canBeIgnored() - && array_key_exists($line, $linesToIgnoreKeys) + && array_key_exists($tmpFileError->getFile(), $linesToIgnore) + && array_key_exists($line, $linesToIgnore[$tmpFileError->getFile()]) ) { - unset($unmatchedLineIgnores[$line]); + unset($unmatchedLineIgnores[$tmpFileError->getFile()][$line]); continue; } @@ -209,26 +208,25 @@ public function analyseFile( } if ($this->reportUnmatchedIgnoredErrors) { - foreach (array_keys($unmatchedLineIgnores) as $line) { - $traitFilePath = null; - if ($scope->isInTrait()) { - $traitReflection = $scope->getTraitReflection(); - if ($traitReflection->getFileName() !== false) { - $traitFilePath = $traitReflection->getFileName(); - } + foreach ($unmatchedLineIgnores as $ignoredFile => $lines) { + if ($ignoredFile !== $file) { + continue; + } + + foreach (array_keys($lines) as $line) { + $fileErrors[] = new Error( + sprintf('No error to ignore is reported on line %d.', $line), + $scope->getFileDescription(), + $line, + false, + $scope->getFile(), + null, + null, + null, + null, + 'ignoredError.unmatchedOnLine' + ); } - $fileErrors[] = new Error( - sprintf('No error to ignore is reported on line %d.', $line), - $scope->getFileDescription(), - $line, - false, - $scope->getFile(), - $traitFilePath, - null, - null, - null, - 'ignoredError.unmatchedOnLine' - ); } } } catch (\PhpParser\Error $e) { diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index 74d540b006..d562c1ffb3 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -299,6 +299,12 @@ public function testBug4300(): void $this->assertSame(13, $errors[0]->getLine()); } + public function testBug4513(): void + { + $errors = $this->runAnalyse(__DIR__ . '/data/bug-4513.php'); + $this->assertCount(0, $errors); + } + /** * @param string $file * @return \PHPStan\Analyser\Error[] diff --git a/tests/PHPStan/Analyser/data/bug-4513.php b/tests/PHPStan/Analyser/data/bug-4513.php new file mode 100644 index 0000000000..23604269f4 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4513.php @@ -0,0 +1,27 @@ +