Zum Inhalt springen

Unguarded first element access (ARCH7078)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key php.performance.first-or-default-null · Severity warning · Category performance

Accessing [0] on a filtered collection raises an undefined-index warning or returns null when the result is empty. Guard for emptiness before access.

<?php
$matches = array_values(array_filter($users, fn ($u) => $u->active));
$first = $matches[0];
echo $first->name;
<?php
$matches = array_values(array_filter($users, fn ($u) => $u->active));
$first = $matches[0] ?? null;
if ($first !== null) {
echo $first->name;
}

Guard the first-element access with ?? null and handle the null case explicitly.