Error Identifier: empty.offset
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 array{name: string} $data */
function check(array $data): void
{
if (empty($data['age'])) {
echo 'No age';
}
}
Why is it reported? #
The offset used inside empty() does not exist on the given type. The empty() construct checks whether an array offset exists and has a non-empty value. When PHPStan can determine that the offset never exists on the array type, using empty() on it always evaluates to true, which indicates a logic error such as a typo in the key name or a wrong variable.
In the example above, the array shape array{name: string} does not have an 'age' key, so empty($data['age']) is always true.
How to fix it #
Use the correct offset name that exists in the array type:
<?php declare(strict_types = 1);
/** @param array{name: string} $data */
function check(array $data): void
{
- if (empty($data['age'])) {
- echo 'No age';
+ if ($data['name'] === '') {
+ echo 'No name';
}
}
Or update the type to include the expected offset:
<?php declare(strict_types = 1);
-/** @param array{name: string} $data */
+/** @param array{name: string, age?: int} $data */
function check(array $data): void
{
if (empty($data['age'])) {
echo 'No age';
}
}
How to ignore this error #
You can use the identifier empty.offset to ignore this error using a comment:
// @phpstan-ignore empty.offset
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: empty.offset