Skip to content

Commit 63cc502

Browse files
committed
Error in trait can be ignored by both the class context file path and the trait file path
1 parent ca0218c commit 63cc502

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

src/Analyser/Analyser.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ public function analyse(
168168
$line = $node->getLine();
169169
$fileName = $scope->getFileDescription();
170170
$filePath = $scope->getFile();
171+
$traitFilePath = null;
172+
if ($scope->isInTrait()) {
173+
$traitReflection = $scope->getTraitReflection();
174+
if ($traitReflection->getFileName() !== false) {
175+
$traitFilePath = $traitReflection->getFileName();
176+
}
177+
}
171178
if (is_string($ruleError)) {
172179
$message = $ruleError;
173180
} else {
@@ -184,14 +191,16 @@ public function analyse(
184191
) {
185192
$fileName = $ruleError->getFile();
186193
$filePath = $ruleError->getFile();
194+
$traitFilePath = null;
187195
}
188196
}
189197
$fileErrors[] = new Error(
190198
$message,
191199
$fileName,
192200
$line,
193201
true,
194-
$filePath
202+
$filePath,
203+
$traitFilePath
195204
);
196205
}
197206
}

src/Analyser/Error.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ class Error
2020
/** @var string|null */
2121
private $filePath;
2222

23+
/** @var string|null */
24+
private $traitFilePath;
25+
2326
public function __construct(
2427
string $message,
2528
string $file,
2629
?int $line = null,
2730
bool $canBeIgnored = true,
28-
?string $filePath = null
31+
?string $filePath = null,
32+
?string $traitFilePath = null
2933
)
3034
{
3135
$this->message = $message;
3236
$this->file = $file;
3337
$this->line = $line;
3438
$this->canBeIgnored = $canBeIgnored;
3539
$this->filePath = $filePath;
40+
$this->traitFilePath = $traitFilePath;
3641
}
3742

3843
public function getMessage(): string
@@ -54,6 +59,11 @@ public function getFilePath(): string
5459
return $this->filePath;
5560
}
5661

62+
public function getTraitFilePath(): ?string
63+
{
64+
return $this->traitFilePath;
65+
}
66+
5767
public function getLine(): ?int
5868
{
5969
return $this->line;

src/Analyser/IgnoredError.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ public static function shouldIgnore(
5151
if ($path !== null) {
5252
$fileExcluder = new FileExcluder($fileHelper, [$path]);
5353

54-
return \Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) !== null
55-
&& $fileExcluder->isExcludedFromAnalysing($error->getFilePath());
54+
if (\Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) === null) {
55+
return false;
56+
}
57+
58+
$isExcluded = $fileExcluder->isExcludedFromAnalysing($error->getFilePath());
59+
if (!$isExcluded && $error->getTraitFilePath() !== null) {
60+
return $fileExcluder->isExcludedFromAnalysing($error->getTraitFilePath());
61+
}
62+
63+
return $isExcluded;
5664
}
5765

5866
return \Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) !== null;

tests/PHPStan/Analyser/AnalyserTest.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,37 @@ public function testIgnoreErrorNotFoundInPath(): void
213213
$this->assertSame('Ignored error pattern #Fail\.# in path ' . __DIR__ . '/data/not-existent-path.php was not matched in reported errors.', $result[0]);
214214
}
215215

216+
public function dataIgnoreErrorInTraitUsingClassFilePath(): array
217+
{
218+
return [
219+
[
220+
__DIR__ . '/data/traits-ignore/Foo.php',
221+
],
222+
[
223+
__DIR__ . '/data/traits-ignore/FooTrait.php',
224+
],
225+
];
226+
}
227+
228+
/**
229+
* @dataProvider dataIgnoreErrorInTraitUsingClassFilePath
230+
* @param string $pathToIgnore
231+
*/
232+
public function testIgnoreErrorInTraitUsingClassFilePath(string $pathToIgnore): void
233+
{
234+
$ignoreErrors = [
235+
[
236+
'message' => '#Fail\.#',
237+
'path' => $pathToIgnore,
238+
],
239+
];
240+
$result = $this->runAnalyser($ignoreErrors, true, [
241+
__DIR__ . '/data/traits-ignore/Foo.php',
242+
__DIR__ . '/data/traits-ignore/FooTrait.php',
243+
], true);
244+
$this->assertCount(0, $result);
245+
}
246+
216247
public function testIgnoredErrorMissingMessage(): void
217248
{
218249
$ignoreErrors = [
@@ -276,22 +307,29 @@ public function testReportMultipleParserErrorsAtOnce(): void
276307
/**
277308
* @param mixed[] $ignoreErrors
278309
* @param bool $reportUnmatchedIgnoredErrors
279-
* @param string $filePath
310+
* @param string|string[] $filePaths
280311
* @param bool $onlyFiles
281312
* @return string[]|\PHPStan\Analyser\Error[]
282313
*/
283314
private function runAnalyser(
284315
array $ignoreErrors,
285316
bool $reportUnmatchedIgnoredErrors,
286-
string $filePath,
317+
$filePaths,
287318
bool $onlyFiles
288319
): array
289320
{
290321
$analyser = $this->createAnalyser(
291322
$ignoreErrors,
292323
$reportUnmatchedIgnoredErrors
293324
);
294-
return $analyser->analyse([$this->getFileHelper()->normalizePath($filePath)], $onlyFiles);
325+
326+
if (is_string($filePaths)) {
327+
$filePaths = [$filePaths];
328+
}
329+
330+
return $analyser->analyse(array_map(function (string $path): string {
331+
return $this->getFileHelper()->normalizePath($path);
332+
}, $filePaths), $onlyFiles);
295333
}
296334

297335
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TraitsIgnore;
4+
5+
class Foo
6+
{
7+
8+
use FooTrait;
9+
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TraitsIgnore;
4+
5+
trait FooTrait
6+
{
7+
8+
public function doFoo(): void
9+
{
10+
doFoo();
11+
}
12+
13+
}

0 commit comments

Comments
 (0)