Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add rule to detect redundant trait usage #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

@AronNovak
Copy link

@AronNovak AronNovak commented Aug 27, 2025

This rule detects when a class uses both a trait A and trait B, where trait A already uses trait B (either directly or transitively).

Features

  • Detects direct redundancy (class uses A and B, A uses B)
  • Detects transitive redundancy (class uses A and C, A uses B, B uses C)
  • Configurable via strictRules.noRedundantTraitUse parameter

Example

trait BarTrait {}
trait FooTrait { use BarTrait; }
class MyClass {
 use FooTrait;
 use BarTrait; // Error: redundant, already included via FooTrait
}

@AronNovak AronNovak changed the title (削除) Add NoRedundantTraitUseRule to detect redundant trait usage (削除ここまで) (追記) Add rule to detect redundant trait usage (追記ここまで) Aug 28, 2025
Copy link
Member

Hi,
I don't think this is a very common and bug-inducing problem. Additionally, there might be valid reasons to do this if you account for trait adaptations (with insteadof and as keywords).

As usual, feel free to publish this as your own package, or try to pitch this idea to other maintainers of strict rules packages like https://github.com/shipmonk-rnd/phpstan-rules or https://github.com/ergebnis/phpstan-rules 😊

AronNovak reacted with thumbs up emoji

Copy link
Author

@ondrejmirtes It's problematic performance-wise. On a client project, phpstan needed 7 minutes to complete. After eliminating excessive redundant trait use, we were down to ~3 minutes. It can be a huge performance penalty for phpstan, and the reason is obvious, the trait is evaluated in every possible context where it appears. This redundant trait usage is problematic for this reason. Also it indicates bad design, but it's opinionated.

Copy link
Member

Please report this in phpstan/phpstan as a performance bug with a reproducer.

AronNovak reacted with thumbs up emoji

Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /