Error Identifier: staticMethod.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.
Code example #
<?php declare(strict_types = 1);
class Foo
{
public static function doBar(): void
{
}
}
$method = 'doBar';
Foo::$method();
Why is it reported? #
This error is reported by phpstan-strict-rules.
A static method is called using a variable method name (Foo::$method()). Variable static method calls make the code harder to analyze and reason about because the actual method being called is only known at runtime. This prevents static analysis from verifying that the method exists and that the correct arguments are passed.
How to fix it #
Call the method directly by its name:
-$method = 'doBar';
-Foo::$method();
+Foo::doBar();
If dynamic dispatch is needed, consider using a match expression or a strategy pattern:
-$method = 'doBar';
-Foo::$method();
+match ($action) {
+ 'bar' => Foo::doBar(),
+ 'baz' => Foo::doBaz(),
+};
How to ignore this error #
You can use the identifier staticMethod.dynamicName to ignore this error using a comment:
// @phpstan-ignore staticMethod.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: staticMethod.dynamicName