website/errors/staticMethod.dynamicName.md
<?php declare(strict_types = 1);
class Foo
{
public static function doBar(): void
{
}
}
$method = 'doBar';
Foo::$method();
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.
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(),
+};