Skip to content

Preserve trait ignore-comment errors in the result cache - #6883

Open
alfredbez wants to merge 3 commits into
phpstan:2.3.xfrom
alfredbez:fix/15177-trait-ignore-cache
Open

alfredbez wants to merge 3 commits into
phpstan:2.3.xfrom
alfredbez:fix/15177-trait-ignore-cache

Conversation

@alfredbez

Copy link
Copy Markdown
Contributor

With reportIgnoresWithoutComments: true, cached analysis drops errors about missing ignore comments in traits used by classes. The error count changes between runs. This change keeps those errors in the cache and preserves their trait context in editor mode.

Fixes phpstan/phpstan#15177

Reproducer: https://phpstan.org/r/2451c832-f6b7-45ae-8bdd-6c5733ac01b9
The playground shows the source example. To reproduce the cache behaviour, run the local fixture with reportIgnoresWithoutComments enabled:

cd e2e/result-cache-trait-ignore
../../bin/phpstan analyse --no-progress --error-format raw -vvv
../../bin/phpstan analyse --no-progress --error-format raw -vvv

With PHP 8.3.36 and the PHPStan 2.3.x source, the fixture reports 10 errors on the first run and 6 on the second. With this change, both runs report 10 and their output is identical. The test also checks editor mode and removes the trait from one class to check that its errors disappear while the other class's errors remain.

Not covered: Reporting of ignore.unmatchedLine and ignore.unmatchedIdentifier is unchanged.

Comment thread .github/workflows/e2e-tests.yml Outdated
rm -rf tmp/cache tmp/resultCache.php
../../bin/phpstan analyse --no-progress --error-format raw -vvv > tmp/cold.txt 2> tmp/cold-log.txt || test "$?" -eq 1
../../bin/phpstan analyse --no-progress --error-format raw -vvv > tmp/warm.txt 2> tmp/warm-log.txt || test "$?" -eq 1
grep -F 'Result cache is saved.' tmp/cold-log.txt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know's grep -F. We use bashunit in these tests so please rewrite them, and make sure they still fail when src/ changes are reverted.

@alfredbez
alfredbez force-pushed the fix/15177-trait-ignore-cache branch from 790cca7 to bc9d890 Compare November 1, 2026 10:38
…runs

When reportIgnoresWithoutComments is enabled, ignore.noComment and
ignore.allLineErrors emitted for code inside traits were created with
filePath set to the formatted file description instead of the analysed
class file, and traitFilePath set to null.

Because the result cache indexes errors by Error::getFilePath() and
restores cached errors only for analysed files on warm runs, contextual
ignore errors in traits were lost when restoring from cache.

Pass the analysed file path and the trait file path to Error in
FileAnalyser.
@alfredbez
alfredbez force-pushed the fix/15177-trait-ignore-cache branch from bc9d890 to bf81146 Compare November 2, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reportIgnoresWithoutComments on Traits are skipped while cached

2 participants