Error Identifier: staticMethod.protected
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
{
protected static function baz(): void
{
}
}
class Unrelated
{
public function doFoo(): void
{
Foo::baz();
}
}
Why is it reported? #
The code calls a protected static method from a class that is not part of the declaring class hierarchy. Protected methods can only be called from within the declaring class or its descendants. Calling a protected method from an unrelated class results in a fatal error at runtime.
How to fix it #
If the method needs to be called from outside the class hierarchy, change its visibility to public:
<?php declare(strict_types = 1);
class Foo
{
- protected static function baz(): void
+ public static function baz(): void
{
}
}
Alternatively, call it from within the class hierarchy by extending the declaring class:
<?php declare(strict_types = 1);
-class Unrelated
+class Unrelated extends Foo
{
public function doFoo(): void
{
- Foo::baz();
+ static::baz();
}
}
How to ignore this error #
You can use the identifier staticMethod.protected to ignore this error using a comment:
// @phpstan-ignore staticMethod.protected
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.protected