Error Identifier: method.childParameterType
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class ParentClass
{
public function process(string|int $value): void
{
}
}
class ChildClass extends ParentClass
{
public function process(string $value): void
{
}
}
Why is it reported? #
A child class overrides a method from its parent, but the parameter type in the child is narrower than in the parent. This violates the Liskov Substitution Principle: code that works with the parent type should also work with any child type. In the example above, ParentClass::process() accepts string|int, but ChildClass::process() only accepts string, meaning code passing int would break.
How to fix it #
Keep the parameter type at least as wide as in the parent method:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
- public function process(string $value): void
+ public function process(string|int $value): void
{
}
}
How to ignore this error #
You can use the identifier method.childParameterType to ignore this error using a comment:
// @phpstan-ignore method.childParameterType
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: method.childParameterType