Skip to content

Fix phpstan/phpstan#10820: The playground reports false positives about changing the return type to never#5319

Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-45uay76
Closed

Fix phpstan/phpstan#10820: The playground reports false positives about changing the return type to never#5319
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-45uay76

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

The playground "never" rule (phpstanPlayground.never) reported false positives suggesting that a method's return type should be changed to never when the method always throws, even when the method is overridden in a child class where it doesn't throw. Changing the parent's return type to never would break the child class.

Changes

  • Modified src/Rules/Playground/MethodNeverRule.php to skip reporting for non-private methods in non-final classes, since these methods can be overridden
  • Added regression test in tests/PHPStan/Rules/Playground/data/bug-10820.php covering abstract parent with override, non-final class with protected method, private method, and final class
  • Updated existing test data in tests/PHPStan/Rules/Playground/data/method-never.php to use final class to preserve existing test coverage

Root cause

The MethodNeverRule did not consider whether a method could be overridden by a subclass. It reported any method that always throws or terminates as needing a never return type, regardless of whether subclasses might override the method with a non-throwing implementation. The fix checks if the method is private or the class is final before suggesting the return type change.

Test

The regression test in bug-10820.php verifies:

  • Value::assertString() (public method in abstract non-final class, overridden by child) - no error
  • NonFinalBase::doSomething() (protected method in non-final class) - no error
  • NonFinalBase::doPrivate() (private method) - error reported (correct)
  • FinalClass::doSomething() (public method in final class) - error reported (correct)

Fixes phpstan/phpstan#10820

…verridable methods

- MethodNeverRule now skips non-private methods in non-final classes
- Private methods and methods in final classes still get reported
- Added regression test in tests/PHPStan/Rules/Playground/data/bug-10820.php
- Updated existing test data to use final class to preserve coverage
@staabm staabm deleted the create-pull-request/patch-45uay76 branch March 29, 2026 06:33
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.

2 participants