Error Identifier: parameter.notOptional
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 Foo
{
public function doFoo(int $i, int $j = 0): void
{
}
}
class Bar extends Foo
{
public function doFoo(int $i, int $j): void
{
}
}
Why is it reported? #
The overriding method makes a parameter required that is optional in the parent method. In the example above, parameter $j has a default value in Foo::doFoo() (making it optional), but the overriding method Bar::doFoo() declares $j without a default value (making it required).
This violates the Liskov Substitution Principle. Code that calls $foo->doFoo(1) with only one argument would break when $foo is an instance of Bar, because Bar::doFoo() requires two arguments.
How to fix it #
Make the parameter optional in the child class to match the parent:
class Bar extends Foo
{
- public function doFoo(int $i, int $j): void
+ public function doFoo(int $i, int $j = 0): void
{
}
}
How to ignore this error #
You can use the identifier parameter.notOptional to ignore this error using a comment:
// @phpstan-ignore parameter.notOptional
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: parameter.notOptional