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
Closed
Conversation
…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
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
The playground "never" rule (
phpstanPlayground.never) reported false positives suggesting that a method's return type should be changed toneverwhen 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 toneverwould break the child class.Changes
src/Rules/Playground/MethodNeverRule.phpto skip reporting for non-private methods in non-final classes, since these methods can be overriddentests/PHPStan/Rules/Playground/data/bug-10820.phpcovering abstract parent with override, non-final class with protected method, private method, and final classtests/PHPStan/Rules/Playground/data/method-never.phpto usefinal classto preserve existing test coverageRoot cause
The
MethodNeverRuledid not consider whether a method could be overridden by a subclass. It reported any method that always throws or terminates as needing aneverreturn 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.phpverifies:Value::assertString()(public method in abstract non-final class, overridden by child) - no errorNonFinalBase::doSomething()(protected method in non-final class) - no errorNonFinalBase::doPrivate()(private method) - error reported (correct)FinalClass::doSomething()(public method in final class) - error reported (correct)Fixes phpstan/phpstan#10820