Skip to content

Commit 686ab8a

Browse files
committed
Report errors in traits with the same message just once
1 parent 8175b6e commit 686ab8a

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

.github/workflows/e2e-tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ jobs:
267267
cd e2e/bug-11857
268268
composer install
269269
../../bin/phpstan
270+
- script: |
271+
cd e2e/in-trait
272+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan --error-format=raw")
273+
../bashunit -a contains 'FooTrait.php:10:Strict comparison using === between int<0, max> and false will always evaluate to false.' "$OUTPUT"
274+
../bashunit -a contains 'FooTrait.php (in context of class E2EInTrait\Bar):18:Strict comparison using === between E2EInTrait\Bar and null will always evaluate to false.' "$OUTPUT"
275+
../bashunit -a contains 'FooTrait.php (in context of class E2EInTrait\Foo):18:Strict comparison using === between E2EInTrait\Foo and null will always evaluate to false.' "$OUTPUT"
270276
- script: |
271277
cd e2e/result-cache-meta-extension
272278
composer install

e2e/in-trait/phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- src

e2e/in-trait/src/Bar.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace E2EInTrait;
4+
5+
class Bar
6+
{
7+
8+
use FooTrait;
9+
10+
public function getSth(): ?self
11+
{
12+
return rand(0, 1) ? $this : null;
13+
}
14+
15+
public function getSth2(): self
16+
{
17+
return $this;
18+
}
19+
20+
}

e2e/in-trait/src/Foo.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace E2EInTrait;
4+
5+
class Foo
6+
{
7+
8+
use FooTrait;
9+
10+
public function getSth(): self
11+
{
12+
return $this;
13+
}
14+
15+
public function getSth2(): self
16+
{
17+
return $this;
18+
}
19+
20+
}

e2e/in-trait/src/FooTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace E2EInTrait;
4+
5+
trait FooTrait
6+
{
7+
8+
public function doFoo(int $i): void
9+
{
10+
if (abs($i) === false) {
11+
12+
}
13+
14+
if ($this->getSth() === null) {
15+
16+
}
17+
18+
if ($this->getSth2() === null) {
19+
20+
}
21+
}
22+
23+
}

src/Analyser/Error.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ public function changeTraitFilePath(string $newFilePath): self
106106
);
107107
}
108108

109+
public function removeTraitContext(): self
110+
{
111+
if ($this->traitFilePath === null) {
112+
throw new ShouldNotHappenException();
113+
}
114+
115+
return new self(
116+
$this->message,
117+
$this->traitFilePath,
118+
$this->line,
119+
$this->canBeIgnored,
120+
$this->filePath,
121+
null,
122+
$this->tip,
123+
$this->nodeLine,
124+
$this->nodeType,
125+
$this->identifier,
126+
$this->metadata,
127+
$this->fixedErrorDiff,
128+
);
129+
}
130+
109131
public function getTraitFilePath(): ?string
110132
{
111133
return $this->traitFilePath;

src/Rules/Comparison/ConstantConditionInTraitRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array
7777

7878
if (count($uniquedErrors) === 1) {
7979
// report directly in trait, no "in context of"
80-
$transformedErrors[] = new TransformedRuleError($uniquedErrors[0]);
80+
$transformedErrors[] = new TransformedRuleError($uniquedErrors[0]->removeTraitContext());
8181
continue;
8282
}
8383

0 commit comments

Comments
 (0)