[DeadCode] Keep private method called via self::class static call#8098
Open
TomasVotruba wants to merge 1 commit into
Open
[DeadCode] Keep private method called via self::class static call#8098TomasVotruba wants to merge 1 commit into
TomasVotruba wants to merge 1 commit into
Conversation
A private method invoked through a class-string static call, e.g. self::class::sampleClass(), is not detected by the usage analyzer and would be wrongly removed. Keep such methods.
0c30408 to
429255c
Compare
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.
Why
RemoveUnusedPrivateMethodRectorremoves private methods that look unused to static analysis. But a private method invoked through a class-string static call is not seen by the usage analyzer, so it gets wrongly removed.This pattern was found by scanning
withSkip()across popular Rector configs - real projects disable the rule on files where private methods are called this way.Code sample
Before this PR,
sampleClass()is removed (theself::class::sampleClass()call is invisible to the analyzer), breakingrun(). After, it is kept.Fix
resolveClassStringStaticCallNames()collects method names invoked via a::classstatic call (self::class::x(),static::class::x(),Foo::class::x()) and skips removing them.Tests
New keep-fixture
keep_self_class_static_call.php.inc. ECS + PHPStan clean, all rule tests green.