Error Identifier: property.deprecated
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 User
{
/**
* @deprecated Use getName() instead
*/
public string $name;
public function getName(): string
{
return $this->name;
}
}
function greet(User $user): string
{
return 'Hello, ' . $user->name; // ERROR: Access to deprecated property $name of class User: Use getName() instead
}
Why is it reported? #
The code accesses a property that has been marked as @deprecated in its PHPDoc. Deprecated properties are scheduled for removal in a future version and should no longer be used. The deprecation notice typically suggests an alternative approach.
This rule is provided by the phpstan/phpstan-deprecation-rules package.
How to fix it #
Replace the deprecated property access with the suggested alternative:
<?php declare(strict_types = 1);
function greet(User $user): string
{
- return 'Hello, ' . $user->name;
+ return 'Hello, ' . $user->getName();
}
How to ignore this error #
You can use the identifier property.deprecated to ignore this error using a comment:
// @phpstan-ignore property.deprecated
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.deprecated