Fix #7388: Error when parent class and interface contradicts each other#5065
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix #7388: Error when parent class and interface contradicts each other#5065phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added InheritedMethodCompatibilityRule to check that methods inherited from parent classes are compatible with interface requirements - The existing OverridingMethodRule only fires when a method is declared in the class itself; this new rule catches the case where a method is inherited but doesn't satisfy an implemented interface's signature - Checks parameter count, parameter types, return types, and pass-by-reference compatibility - New regression test in tests/PHPStan/Rules/Methods/data/bug-7388.php Closes phpstan/phpstan#7388
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
PHPStan failed to report an error when a class extends a parent class and implements an interface, but the parent class's method signature is incompatible with the interface's requirements. For example:
PHP itself produces a fatal error for this case, but PHPStan silently accepted it.
Changes
InheritedMethodCompatibilityRuleinsrc/Rules/Methods/InheritedMethodCompatibilityRule.phpInClassNode(class-level) at rule level 0tests/PHPStan/Rules/Methods/InheritedMethodCompatibilityRuleTest.phptests/PHPStan/Rules/Methods/data/bug-7388.phpRoot cause
The existing
OverridingMethodRuleandMethodSignatureRuleboth processInClassMethodNode, which only fires when a method is explicitly declared in the class. When a class inherits a method from a parent without overriding it, noInClassMethodNodeis generated for that method in the child class context. This meant that inherited methods were never validated against interface requirements.The fix adds a new class-level rule that iterates over interface methods and checks if inherited implementations are compatible.
Test
The regression test (
bug-7388.php) covers:Fixes phpstan/phpstan#7388