website/errors/empty.offset.md
<?php declare(strict_types = 1);
/** @param array{name: string} $data */
function check(array $data): void
{
if (empty($data['age'])) {
echo 'No age';
}
}
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.
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';
}
}