Error Identifier: property.readOnlyNoNativeType
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 Config
{
/** @var string */
public readonly $name;
}
Why is it reported? #
A readonly property must have a native type declaration. PHP requires that all readonly properties have an explicit native type (such as string, int, mixed, etc.). A PHPDoc type alone is not sufficient. This is a language-level requirement enforced by the PHP engine.
How to fix it #
Add a native type declaration to the property:
<?php declare(strict_types = 1);
class Config
{
- /** @var string */
- public readonly $name;
+ public readonly string $name;
}
If the property can hold multiple types, use a union type or mixed:
<?php declare(strict_types = 1);
class Config
{
- /** @var string|int */
- public readonly $name;
+ public readonly string|int $name;
}
How to ignore this error #
You can use the identifier property.readOnlyNoNativeType to ignore this error using a comment:
// @phpstan-ignore property.readOnlyNoNativeType
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.readOnlyNoNativeType
Rules that report this error #
- PHPStan\Rules\Properties\ReadOnlyPropertyRule [1]