Skip to content

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
phpstan-bot:create-pull-request/patch-hix4qy4
Open

Fix phpstan/phpstan#12062: False positive "has no effect" on call to late static binding method of abstract class#5345
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-hix4qy4

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

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

  • Modified src/Rules/DeadCode/PossiblyPureStaticCallCollector.php to skip collecting static:: call sites when:
    • The calling class is not final
    • The method is not final
    • The method is not private
  • Updated expected errors in tests/PHPStan/Rules/DeadCode/CallToStaticMethodStatementWithoutImpurePointsRuleTest.php for the existing static::myFunc() test case (line 48)
  • Added regression test tests/PHPStan/Rules/DeadCode/data/bug-12062.php

Root cause

The PossiblyPureStaticCallCollector resolved static::validateParam() to the base class method and collected it as a possibly-pure call. The MethodWithoutImpurePointsCollector then confirmed the base class method body had no impure points. The cross-reference in CallToStaticMethodStatementWithoutImpurePointsRule matched them, producing the false positive. The fix skips collecting static:: calls when the method can be overridden, since a subclass might add side effects.

Test

Added tests/PHPStan/Rules/DeadCode/data/bug-12062.php with an abstract base class that has a pure validateParam() method called via static::, and a subclass that overrides it to throw an exception. The test expects no errors.

Fixes phpstan/phpstan#12062

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