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 static function doFoo(): void
{
}
public static function test(int $name): void
{
self::$name();
}
}
Why is it reported? #
When calling a static method dynamically with the Foo::{$name}() or self::$name() syntax, the $name expression must be a string. Static method names are not cast to string at runtime, so even an object implementing Stringable is not accepted — only an actual string works. In the example above, $name is an int, which cannot be used as a method name and results in an error.
This check is currently part of Bleeding Edge.
How to fix it #
Ensure the dynamic method name expression is a string:
- public static function test(int $name): void
+ public static function test(string $name): void
{
self::$name();
}
If the value comes from a Stringable object, cast it to string first:
public static function test(\Stringable $name): void
{
- self::$name();
+ self::{(string) $name}();
}
How to ignore this error #
You can use the identifier staticMethod.nameNotString to ignore this error using a comment:
// @phpstan-ignore staticMethod.nameNotString
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: staticMethod.nameNotString
Rules that report this error #
- PHPStan\Rules\Classes\ClassConstantRule [1]
- PHPStan\Rules\Methods\CallMethodsRule [1]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1]
- PHPStan\Rules\Properties\AccessPropertiesInAssignRule [1]
- PHPStan\Rules\Properties\AccessPropertiesRule [1]
- PHPStan\Rules\Properties\AccessStaticPropertiesInAssignRule [1]
- PHPStan\Rules\Properties\AccessStaticPropertiesRule [1]
- PHPStan\Rules\Variables\DefinedVariableRule [1]