Error Identifier: staticMethod.nonObject
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);
/**
* @param int|string $value
*/
function doFoo($value): void
{
$value::foo();
}
Why is it reported? #
A static method is being called on a value that is not an object or class name. Static methods can only be called on class names (as strings or class references) or on objects. Calling a static method on a type like int, float, bool, or a union that does not guarantee an object type will result in a runtime error.
How to fix it #
Ensure the variable holds a valid class name or object before calling a static method on it:
<?php declare(strict_types = 1);
-/**
- * @param int|string $value
- */
-function doFoo($value): void
+function doFoo(string $className): void
{
- $value::foo();
+ if (is_a($className, MyClass::class, true)) {
+ $className::foo();
+ }
}
Or call the static method directly on the class:
<?php declare(strict_types = 1);
-/**
- * @param int|string $value
- */
-function doFoo($value): void
+function doFoo(): void
{
- $value::foo();
+ MyClass::foo();
}
How to ignore this error #
You can use the identifier staticMethod.nonObject to ignore this error using a comment:
// @phpstan-ignore staticMethod.nonObject
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.nonObject