Skip to content
Merged
Prev Previous commit
Next Next commit
Report errors in traits with the same message just once
  • Loading branch information
ondrejmirtes committed Mar 27, 2026
commit 686ab8ab6bebcf4c1ad403488b8264bcecc46e27
6 changes: 6 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ jobs:
cd e2e/bug-11857
composer install
../../bin/phpstan
- script: |
cd e2e/in-trait
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan --error-format=raw")
../bashunit -a contains 'FooTrait.php:10:Strict comparison using === between int<0, max> and false will always evaluate to false.' "$OUTPUT"
../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"
../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"
- script: |
cd e2e/result-cache-meta-extension
composer install
Expand Down
4 changes: 4 additions & 0 deletions e2e/in-trait/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 8
paths:
- src
20 changes: 20 additions & 0 deletions e2e/in-trait/src/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace E2EInTrait;

class Bar
{

use FooTrait;

public function getSth(): ?self
{
return rand(0, 1) ? $this : null;
}

public function getSth2(): self
{
return $this;
}

}
20 changes: 20 additions & 0 deletions e2e/in-trait/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace E2EInTrait;

class Foo
{

use FooTrait;

public function getSth(): self
{
return $this;
}

public function getSth2(): self
{
return $this;
}

}
23 changes: 23 additions & 0 deletions e2e/in-trait/src/FooTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace E2EInTrait;

trait FooTrait
{

public function doFoo(int $i): void
{
if (abs($i) === false) {

}

if ($this->getSth() === null) {

}

if ($this->getSth2() === null) {

}
}

}
22 changes: 22 additions & 0 deletions src/Analyser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ public function changeTraitFilePath(string $newFilePath): self
);
}

public function removeTraitContext(): self
{
if ($this->traitFilePath === null) {
throw new ShouldNotHappenException();
}

return new self(
$this->message,
$this->traitFilePath,
$this->line,
$this->canBeIgnored,
$this->filePath,
null,
$this->tip,
$this->nodeLine,
$this->nodeType,
$this->identifier,
$this->metadata,
$this->fixedErrorDiff,
);
}

public function getTraitFilePath(): ?string
{
return $this->traitFilePath;
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/ConstantConditionInTraitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array

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

Expand Down