Skip to content

Fix #7388: Error when parent class and interface contradicts each other#5065

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

Fix #7388: Error when parent class and interface contradicts each other#5065
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-if8ptix

Conversation

@phpstan-bot
Copy link
Collaborator

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:

interface FooInterface {
    public function bar(int $i): void;
}
class ParentFoo {
    public function bar(): void {}
}
class Foo extends ParentFoo implements FooInterface {} // Should error, didn't

PHP itself produces a fatal error for this case, but PHPStan silently accepted it.

Changes

  • Added new InheritedMethodCompatibilityRule in src/Rules/Methods/InheritedMethodCompatibilityRule.php
    • Processes InClassNode (class-level) at rule level 0
    • For each immediate interface method, checks if the inherited (not overridden) method from the parent class has a compatible signature
    • Checks parameter count, parameter type contravariance, return type covariance, and pass-by-reference compatibility
  • Added test class tests/PHPStan/Rules/Methods/InheritedMethodCompatibilityRuleTest.php
  • Added test data tests/PHPStan/Rules/Methods/data/bug-7388.php

Root cause

The existing OverridingMethodRule and MethodSignatureRule both process InClassMethodNode, which only fires when a method is explicitly declared in the class. When a class inherits a method from a parent without overriding it, no InClassMethodNode is 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:

  • Basic case: parent method missing a required parameter from the interface (reports error)
  • Multiple missing parameters case (reports error)
  • Parent method with extra optional params that satisfy the interface (no error)
  • Class that overrides the method correctly (no error, handled by existing rules)
  • Abstract class that defers implementation (no error)

Fixes phpstan/phpstan#7388

- 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
@staabm staabm closed this Feb 28, 2026
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