Inefficient membership test (ARCH7075)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Key php.performance.inefficient-membership-test · Severity warning · Category performance
Checking membership with in_array() over a list, especially inside a loop, scans O(n) each time. Use a keyed map and isset() for an O(1) lookup.
Incorrect
Section titled “Incorrect”<?php$allowed = ['admin', 'editor', 'viewer'];foreach ($users as $user) { if (in_array($user->role, $allowed, true)) { $granted[] = $user; }}Correct
Section titled “Correct”<?php$allowed = ['admin' => true, 'editor' => true, 'viewer' => true];foreach ($users as $user) { if (isset($allowed[$user->role])) { $granted[] = $user; }}For repeated membership checks, convert the list to a keyed map and query it with isset().