Error Identifier: parameter.defaultValue
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);
function greet(string $name = null): string
{
return 'Hello, ' . $name;
}
Why is it reported? #
The default value of a parameter is incompatible with the parameter’s declared type. PHP would accept this at the function declaration, but it indicates a type mismatch that can lead to unexpected behavior.
In the example above, the parameter $name is declared as string, but its default value is null, which is not a string.
How to fix it #
Make the parameter type nullable to match the default value:
-function greet(string $name = null): string
+function greet(?string $name = null): string
{
return 'Hello, ' . ($name ?? 'World');
}
Or change the default value to match the declared type:
-function greet(string $name = null): string
+function greet(string $name = 'World'): string
{
return 'Hello, ' . $name;
}
How to ignore this error #
You can use the identifier parameter.defaultValue to ignore this error using a comment:
// @phpstan-ignore parameter.defaultValue
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.defaultValue