Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,31 @@ jobs:
../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/bug-14718
# https://github.com/phpstan/phpstan/issues/14718
# An inline @phpstan-ignore on an error deduplicated directly into the trait must
# suppress it, consistently with both an empty and a primed result cache.
../../bin/phpstan clear-result-cache
../bashunit -a exit_code "0" "../../bin/phpstan --error-format=raw"
../bashunit -a exit_code "0" "../../bin/phpstan --error-format=raw"
# With the ignore removed, the error is reported directly in the trait file.
sed -i 's# // @phpstan-ignore identical.alwaysFalse##' src/FooTrait.php
../../bin/phpstan clear-result-cache
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"
# An ignoreErrors path may target the error by the trait file or by the using-class
# file - both keep working (no BC break).
../../bin/phpstan clear-result-cache
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-trait-path.neon"
../../bin/phpstan clear-result-cache
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c ignore-class-path.neon"
# A generated baseline suppresses the error on re-run, with both an empty and a
# primed result cache (the issue reported baseline generation as impossible).
../../bin/phpstan --generate-baseline=baseline.neon
../../bin/phpstan clear-result-cache
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon"
../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon"
- script: |
cd e2e/result-cache-meta-extension
composer install
Expand Down
1 change: 1 addition & 0 deletions e2e/bug-14718/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/baseline.neon
7 changes: 7 additions & 0 deletions e2e/bug-14718/ignore-class-path.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan.neon
parameters:
ignoreErrors:
-
identifier: identical.alwaysFalse
path: src/Foo.php
7 changes: 7 additions & 0 deletions e2e/bug-14718/ignore-trait-path.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan.neon
parameters:
ignoreErrors:
-
identifier: identical.alwaysFalse
path: src/FooTrait.php
4 changes: 4 additions & 0 deletions e2e/bug-14718/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 8
paths:
- src
10 changes: 10 additions & 0 deletions e2e/bug-14718/src/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Bug14718;

class Bar
{

use FooTrait;

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

namespace Bug14718;

class Foo
{

use FooTrait;

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

namespace Bug14718;

trait FooTrait
{

public function doFoo(int $i): void
{
if (abs($i) === false) { // @phpstan-ignore identical.alwaysFalse

}
}

}
3 changes: 3 additions & 0 deletions e2e/bug-14718/with-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
includes:
- phpstan.neon
- baseline.neon
2 changes: 1 addition & 1 deletion src/Analyser/AnalyserResultFinalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles, bool $
$collectorErrors = [];
$locallyIgnoredCollectorErrors = [];
foreach ($tempCollectorErrors as $tempCollectorError) {
$file = $tempCollectorError->getFilePath();
$file = $tempCollectorError->getTraitFilePath() ?? $tempCollectorError->getFilePath();
$linesToIgnore = $allLinesToIgnore[$file] ?? [];
$unmatchedLineIgnores = $allUnmatchedLineIgnores[$file] ?? [];
$localIgnoresProcessorResult = $this->localIgnoresProcessor->process(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function removeTraitContext(): self
$this->line,
$this->canBeIgnored,
$this->filePath,
null,
$this->traitFilePath,
$this->tip,
$this->nodeLine,
$this->nodeType,
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ public function testError(): void
$this->assertSame(10, $error->getLine());
}

public function testRemoveTraitContextKeepsTraitFilePath(): void
{
$error = new Error('Message', 'trait.php (in context of class C)', 11, true, 'user.php', 'trait.php');
$this->assertSame('user.php', $error->getFilePath());
$this->assertSame('trait.php', $error->getTraitFilePath());

$withoutTraitContext = $error->removeTraitContext();
// The error is now reported directly in the trait: the displayed file is
// the trait, and traitFilePath is kept so the editor URL and the
// trait-file ignore lookups resolve to the trait (#14718). filePath stays
// the using-class file, so an ignoreErrors path keyed on either the trait
// or the using-class file keeps matching (no BC break).
$this->assertSame('trait.php', $withoutTraitContext->getFile());
$this->assertSame('user.php', $withoutTraitContext->getFilePath());
$this->assertSame('trait.php', $withoutTraitContext->getTraitFilePath());
}

public static function dataValidIdentifier(): iterable
{
yield ['a'];
Expand Down
Loading