Fix phpstan/phpstan#14332: Conflicting methods/properties across multiple traits#5255
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14332: Conflicting methods/properties across multiple traits#5255phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- New ConflictingTraitMethodsRule reports when a class uses multiple traits that define the same method without resolving the conflict via insteadof - Handles abstract trait methods correctly (abstract + concrete is allowed) - Conflicts are also resolved if the class defines the method itself - Registered at level 0 as this is a fatal error in PHP - New regression tests in tests/PHPStan/Rules/Classes/data/bug-14332.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
When a class uses multiple traits that define the same method, PHP throws a fatal error unless the conflict is resolved via
insteadofor the class defines the method itself. PHPStan did not detect this, silently passing code that would crash at runtime.Changes
src/Rules/Classes/ConflictingTraitMethodsRule.php— new rule that detects trait method collisions in class compositionstests/PHPStan/Rules/Classes/ConflictingTraitMethodsRuleTest.php— test casetests/PHPStan/Rules/Classes/data/bug-14332.php— test data with conflicting traits, insteadof resolutions, class-defined overrides, and partial resolutionstests/PHPStan/Rules/Classes/data/bug-14332-abstract.php— test data for abstract + concrete trait method (no conflict)Root cause
No rule existed to check for method name collisions across multiple traits used in the same class. The existing
DuplicateDeclarationRuleandDuplicateTraitDeclarationRuleonly detect duplicates within a single class-like node, not across traits.Test
The regression test covers:
insteadof(no error)Fixes phpstan/phpstan#14332