Error Identifier: staticMethod.dynamicCall
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 bar(): void
{
}
}
function doFoo(Foo $foo): void
{
$foo->bar();
}
Why is it reported? #
This error is reported by phpstan/phpstan-strict-rules.
A static method is being called dynamically using $instance->method() syntax instead of the proper ClassName::method() static call syntax. While PHP allows this, it is misleading because it suggests the method operates on the instance when it actually does not. Static methods do not have access to $this and calling them dynamically obscures the code’s intent.
How to fix it #
Call the method using the static call syntax:
<?php declare(strict_types = 1);
function doFoo(Foo $foo): void
{
- $foo->bar();
+ Foo::bar();
}
How to ignore this error #
You can use the identifier staticMethod.dynamicCall to ignore this error using a comment:
// @phpstan-ignore staticMethod.dynamicCall
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.dynamicCall