$questionId) { $key = $answerKey[$index] ?? null; $selected = $answers[$index]; if (!is_array($key) || !is_int($selected) || $selected <= 0 || (int)($key['question_id'] ?? 0) !== (int)$questionId || !is_array($key['option_ids'] ?? null) || !in_array($selected, $key['option_ids'], true) || !in_array((int)($key['correct_option_id'] ?? 0), $key['option_ids'], true)) { throw new \InvalidArgumentException('Answer does not belong to the attempt'); } if ($selected === (int)$key['correct_option_id']) $correct++; } return $correct; } }