Fix phpstan/phpstan#14250: trait.duplicateMethod is not reported#5181
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14250: trait.duplicateMethod is not reported#5181phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
… methods in traits - Created DuplicateTraitDeclarationRule that listens for InTraitNode to detect duplicate methods, properties, and constants within trait declarations - The existing DuplicateDeclarationRule only handles InClassNode, which is never emitted for traits (NodeScopeResolver skips trait declarations at line 967-968) - InTraitNode is emitted when a trait is used by a class, allowing the new rule to check trait bodies for duplicates - Added regression test in tests/PHPStan/Rules/Classes/data/bug-14250.php
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.
Summary
Duplicate method declarations within traits were not being reported with the
trait.duplicateMethodidentifier. The existingDuplicateDeclarationRulelistens forInClassNode, butInClassNodeis never emitted for trait declarations —NodeScopeResolverskips trait nodes entirely at declaration time (they are only processed whenused by a class).Changes
src/Rules/Classes/DuplicateTraitDeclarationRule.php— a new rule that implementsRule<InTraitNode>and checks for duplicate methods, properties, and constants within trait declarationsDuplicateDeclarationRulebut operates onInTraitNode(emitted when a trait is used by a class)#[RegisteredRule(level: 0)], same as the original ruletests/PHPStan/Rules/Classes/DuplicateTraitDeclarationRuleTest.phptests/PHPStan/Rules/Classes/data/bug-14250.phpRoot cause
NodeScopeResolver::processStmtNodes()short-circuits trait declarations (line 967-968), returning immediately without emittingInClassNode. This is by design — traits are processed in the context of their using class viaprocessTraitUse(). However,InTraitNodeIS emitted during trait use processing, so a rule listening forInTraitNodecan detect duplicate declarations within traits. No such rule existed before this fix.Test
The regression test in
bug-14250.phpdefines a trait with two methods nameddoSomething()and a class that uses the trait. The test expects one error: "Cannot redeclare method Bug14250\MyTrait::doSomething()." at line 11.Fixes phpstan/phpstan#14250