Error Identifier: unset.variable
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 doFoo(): void
{
unset($undefined);
}
Why is it reported? #
The unset() call targets a variable that does not exist in the current scope. Unsetting an undefined variable is a no-op but usually indicates a bug – the variable name is likely misspelled, or the logic that was supposed to define the variable is missing or unreachable.
How to fix it #
Make sure the variable name is correct and the variable is defined before attempting to unset it:
<?php declare(strict_types = 1);
function doFoo(): void
{
- unset($undefined);
+ $data = getData();
+ // ... use $data ...
+ unset($data);
}
If the unset() call is no longer needed, remove it:
<?php declare(strict_types = 1);
function doFoo(): void
{
- unset($undefined);
}
How to ignore this error #
You can use the identifier unset.variable to ignore this error using a comment:
// @phpstan-ignore unset.variable
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: unset.variable
Rules that report this error #
- PHPStan\Rules\Variables\UnsetRule [1]