Error Identifier: phpstanPlayground.staticWithoutType
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);
function counter(): int
{
static $count;
$count++;
return $count;
}
Why is it reported? #
A static variable is declared without a @var PHPDoc type annotation. Static variables persist their value across function calls, and without a type annotation PHPStan cannot reliably infer their type throughout the analysis.
This rule is specific to the PHPStan playground and cannot be ignored.
How to fix it #
Add a @var PHPDoc tag above the static variable declaration:
<?php declare(strict_types = 1);
function counter(): int
{
+ /** @var int $count */
static $count;
$count++;
return $count;
}
When declaring multiple static variables in a single statement, specify the type for each variable by name:
<?php declare(strict_types = 1);
function example(): void
{
+ /** @var int $a */
+ /** @var string $b */
static $a, $b;
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Playground\StaticVarWithoutTypeRule [1]