Do not report a private method overriding a private trait method as unused - #6191
Open
Jean-Beru wants to merge 1 commit into
Open
Do not report a private method overriding a private trait method as unused#6191Jean-Beru wants to merge 1 commit into
Jean-Beru wants to merge 1 commit into
Conversation
…nused A class member takes precedence over the member of the same name coming from a used trait, so a private method redeclared in the class is what the trait's own methods call. The rule sees those call sites only when the trait is part of the analysed files: analysing a project whose paths do not include its dependencies leaves ClassMethodsNode::getMethodCalls() with no call at all, and the class method is reported as unused. The canonical case is a Symfony application, where the framework recipe generates a Kernel redeclaring KernelTrait::getAllowedEnvs() while the trait lives in vendor/, outside of the analysed paths. Skip those methods. Nothing is left to distinguish an override that the trait calls from one it does not, so a redeclared private method the trait never calls is no longer reported either. Fixes phpstan/phpstan#12201 Assisted-by: Claude Code:claude-opus-5
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.
Fixes phpstan/phpstan#12201
Why
A class member takes precedence over the member of the same name coming from a used trait. So a private method redeclared in the class is the one the trait's own methods call.
UnusedPrivateMethodRulesees those call sites only when the trait is part of the analysed files. In a project whosepathsexclude its dependencies,ClassMethodsNode::getMethodCalls()collects no call at all. The class method is then reported as unused.The canonical case is a Symfony application. The recipe generates a
Kernelthat redeclaresKernelTrait::getAllowedEnvs(), and the trait lives invendor/.Every new Symfony 8.1 project hits this from level 4 up.
ignoreErrorsis the only way out.What
A private method is skipped when a used trait declares a private method of the same name.
getTraits()is enough without recursion. PHP flattens trait composition.getAllowedEnvs()comes from the nestedKernelTrait, and the reflection ofMicroKernelTraitalready reports it.