Error Identifier: staticProperty.dynamicName
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.
This error is reported by phpstan/phpstan-strict-rules.
Code example #
<?php declare(strict_types = 1);
class Registry
{
public static string $name = 'default';
public static function get(string $property): string
{
return Registry::$$property;
}
}
Why is it reported? #
Accessing a static property with a dynamic (variable) name makes the code harder to follow and analyse statically. PHPStan cannot verify that the variable holds a valid property name, which may lead to runtime errors that go undetected during analysis.
How to fix it #
Replace the variable static property access with explicit property references.
<?php declare(strict_types = 1);
class Registry
{
public static string $name = 'default';
- public static function get(string $property): string
+ public static function get(string $property): ?string
{
- return Registry::$$property;
+ return match ($property) {
+ 'name' => Registry::$name,
+ default => null,
+ };
}
}
How to ignore this error #
You can use the identifier staticProperty.dynamicName to ignore this error using a comment:
// @phpstan-ignore staticProperty.dynamicName
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: staticProperty.dynamicName
Rules that report this error #
- PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule [1] phpstan/phpstan-strict-rules