CoversClass does not transitively target traits used by enumerations#1147
Merged
sebastianbergmann merged 1 commit intosebastianbergmann:12.5from Apr 4, 2026
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 12.5 #1147 +/- ##
=========================================
Coverage 88.21% 88.21%
- Complexity 1398 1400 +2
=========================================
Files 105 105
Lines 4718 4718
=========================================
Hits 4162 4162
Misses 556 556 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a class uses a trait,
#[CoversClass(TheClass::class)]transitively whitelists the trait's lines for coverage. When an enum uses the same trait, the trait's lines are not whitelisted — PHPUnit flags the test as risky:This forces authors to add
#[UsesTrait]or#[CoversTrait]to enum tests, which is inconsistent with the class behavior and creates friction with static analysis tools that don't accept those attributes.A minimal reproduction is available at https://github.com/no-simpler/phpunit-psalm-trit-catch22 (rows 1 vs 4 in its test matrix).
Root cause
CodeUnitFindingVisitor::leaveNode()resolves trait usages only forClass_andTrait_nodes.Enum_extendsClassLike(notClass_), so it is excluded by the early-return gate. Enums are correctly discovered inenterNode()viaprocessClass(), but their trait lists are never populated.Changes
Three one-line fixes in
CodeUnitFindingVisitor:leaveNode()— includeEnum_in the gate so enums proceed to trait resolutionpostProcessClassOrTrait()signature — widen the type union to acceptEnum_postProcessClassOrTrait()body — routeEnum_into the class branch (enums are stored in$this->classes)Plus a regression test (
testHandlesEnumUsingTrait) and fixture file mirroring the existingtestHandlesTraitUsingTraitpattern.