Error Identifier: property.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);
class Foo
{
public int $count = 'hello';
}
Why is it reported? #
The default value assigned to a property does not match the property’s declared native type. In the example above, the property $count is declared as int, but its default value is the string 'hello'. This would cause a TypeError at runtime.
PHPStan checks that the default value is compatible with the property’s type declaration so that the class can be instantiated without errors.
How to fix it #
Change the default value to match the property’s type:
<?php declare(strict_types = 1);
class Foo
{
- public int $count = 'hello';
+ public int $count = 0;
}
Or change the property type to match the default value:
<?php declare(strict_types = 1);
class Foo
{
- public int $count = 'hello';
+ public string $count = 'hello';
}
How to ignore this error #
You can use the identifier property.defaultValue to ignore this error using a comment:
// @phpstan-ignore property.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: property.defaultValue
Rules that report this error #
- PHPStan\Rules\Properties\DefaultValueTypesAssignedToPropertiesRule [1]