Method Signature Mismatch
ID |
php.method_signature_mismatch |
Severity |
high |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Reliability |
Language |
Php |
Tags |
inheritance, reliability |
Description
Reports a method whose parameter list is incompatible with the method it overrides in a parent class declared in the same file. The override is flagged when it requires more parameters than the parent accepts, or when it accepts fewer parameters than the parent requires. Constructors are exempt, and a variadic parameter on either side suppresses the check. Resolution is limited to a parent class visible in the same file.
Rationale
A subclass must honour the contract of the type it extends: code written against the parent may call the method with the parent’s parameters, and that call must keep working on every subclass. When an override demands extra required parameters, a parent-style call leaves them unset; when it accepts fewer parameters, a parent-style call overflows it. PHP reports such incompatible overrides as a fatal error or a warning, so the mismatch breaks substitutability and surfaces at runtime instead of where the contract was written.
<?php
class Base
{
public function handle(int $a, int $b): void {}
}
class Bad extends Base
{
public function handle(int $a, int $b, int $c): void {} // FLAW — requires 3, parent accepts 2
}
class Good extends Base
{
public function handle(int $a, int $b, int $c = 0): void {} // OK — extra parameter is optional
}
Remediation
Keep the override compatible with the parent: give any extra parameter a default value so the parameter count the parent exposes still works, or align the parameter list with the parent’s. If the contract genuinely needs to change, change it on the parent so every subclass agrees.