Skip to content

Commit

Permalink
Fixed local ignoring using annotations in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 10, 2021
1 parent 8633000 commit 785b663
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -165,7 +164,7 @@ public function analyseFile(
}

foreach ($this->getLinesToIgnore($node) as $lineToIgnore) {
$linesToIgnore[] = $lineToIgnore;
$linesToIgnore[$scope->getFileDescription()][$lineToIgnore] = true;
}

try {
Expand All @@ -192,43 +191,42 @@ 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;
}

$fileErrors[] = $tmpFileError;
}

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) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4513.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug4513;

class Foo
{

use BarTrait;

}

trait BarTrait
{

public function doFoo(): void
{
// @phpstan-ignore-next-line
echo 'foo';
}

public function doBar(): void
{
// @phpstan-ignore-next-line
echo [];
}

}

0 comments on commit 785b663

Please sign in to comment.