Fix phpstan/phpstan#12062: False positive "has no effect" on call to late static binding method of abstract class#5345
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
… call to overridable method - Skip collecting static:: calls in PossiblyPureStaticCallCollector when the method can be overridden - A subclass may override the method with side effects, so the call cannot be assumed pure - Updated existing test expectations for static:: calls in non-final classes - New regression test in tests/PHPStan/Rules/DeadCode/data/bug-12062.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 calling a method via
static::(late static binding) in a non-final class, PHPStan incorrectly reported "Call to ... on a separate line has no effect" even though a subclass could override the method with side effects (e.g., throwing an exception).Changes
src/Rules/DeadCode/PossiblyPureStaticCallCollector.phpto skip collectingstatic::call sites when:tests/PHPStan/Rules/DeadCode/CallToStaticMethodStatementWithoutImpurePointsRuleTest.phpfor the existingstatic::myFunc()test case (line 48)tests/PHPStan/Rules/DeadCode/data/bug-12062.phpRoot cause
The
PossiblyPureStaticCallCollectorresolvedstatic::validateParam()to the base class method and collected it as a possibly-pure call. TheMethodWithoutImpurePointsCollectorthen confirmed the base class method body had no impure points. The cross-reference inCallToStaticMethodStatementWithoutImpurePointsRulematched them, producing the false positive. The fix skips collectingstatic::calls when the method can be overridden, since a subclass might add side effects.Test
Added
tests/PHPStan/Rules/DeadCode/data/bug-12062.phpwith an abstract base class that has a purevalidateParam()method called viastatic::, and a subclass that overrides it to throw an exception. The test expects no errors.Fixes phpstan/phpstan#12062