Skip to content

Fix phpstan/phpstan#14332: Conflicting methods/properties across multiple traits#5255

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-jojsy9t
Open

Fix phpstan/phpstan#14332: Conflicting methods/properties across multiple traits#5255
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-jojsy9t

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When a class uses multiple traits that define the same method, PHP throws a fatal error unless the conflict is resolved via insteadof or the class defines the method itself. PHPStan did not detect this, silently passing code that would crash at runtime.

Changes

  • Added src/Rules/Classes/ConflictingTraitMethodsRule.php — new rule that detects trait method collisions in class compositions
  • Added tests/PHPStan/Rules/Classes/ConflictingTraitMethodsRuleTest.php — test case
  • Added tests/PHPStan/Rules/Classes/data/bug-14332.php — test data with conflicting traits, insteadof resolutions, class-defined overrides, and partial resolutions
  • Added tests/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 DuplicateDeclarationRule and DuplicateTraitDeclarationRule only detect duplicates within a single class-like node, not across traits.

Test

The regression test covers:

  • Simple two-trait conflict (error)
  • Conflict resolved with insteadof (no error)
  • Conflict resolved by class defining its own method (no error)
  • No overlapping methods between traits (no error)
  • Multiple conflicts from three traits (errors)
  • Partial resolution leaving one conflict (error)
  • Full resolution of all conflicts (no error)
  • Abstract + concrete method across traits (no error, PHP allows this)

Fixes phpstan/phpstan#14332

- 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
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.

1 participant