{"record":{"id":"ce631906c6b5d4f4","repo":"twigphp/Twig","slug":"the-has-every-test-expects-a-sequence-or-a-mapping-got-s","errorCode":null,"errorMessage":"The \"has every\" test expects a sequence or a mapping, got \"%s\".","messagePattern":"The \"has every\" test expects a sequence or a mapping, got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":2165,"sourceCode":"\n        foreach ($array as $k => $v) {\n            if ($arrow($v, $k)) {\n                return true;\n            }\n        }\n\n        return false;\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function arrayEvery(Environment $env, $array, $arrow, bool $isSandboxed = false)\n    {\n        if (!is_iterable($array)) {\n            throw new RuntimeError(\\sprintf('The \"has every\" test expects a sequence or a mapping, got \"%s\".', get_debug_type($array)));\n        }\n\n        self::checkArrow($isSandboxed, $arrow, 'has every', 'operator');\n\n        foreach ($array as $k => $v) {\n            if (!$arrow($v, $k)) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    /**\n     * @internal\n     */\n    public static function checkArrow(bool $isSandboxed, $arrow, $thing, $type): void\n    {","sourceCodeStart":2147,"sourceCodeEnd":2183,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L2147-L2183","documentation":"The Twig `has every` test (CoreExtension::arrayEvery, exposed via twig_array_every) checks whether all elements of an iterable satisfy an arrow function. Twig throws this RuntimeError when the left-hand operand is not a sequence or mapping. The `is_iterable()` check at CoreExtension.php:2165 runs at template execution time.","triggerScenarios":"`{% if items has every(i => i.valid) %}` where items is null, a string, or a scalar. Same shape problem as `has some`, but with the every-quantifier test.","commonSituations":"Optional collection fields that are null when not set, scalar config values mistakenly piped into quantifier tests, variables whose type changed after a model refactor (single object instead of list).","solutions":["Default to an empty array first: `{% if (items ?? []) has every(...) %}`.","Combine with an iterable check: `{% if items is iterable and items has every(...) %}`.","Initialize collections upstream (constructor defaults, `?? []` in the controller) so the field is always an array.","Catch Twig\\Error\\RuntimeError around render and use the template/line info in the message to locate the expression."],"exampleFix":"// before (Twig)\n{% if entries has every(e => e.published) %}...{% endif %}\n\n// after\n{% if (entries ?? []) has every(e => e.published) %}...{% endif %}","handlingStrategy":"validation","validationCode":"// Twig: guard before the test\n{% if items is iterable and items has every(i => i.valid) %}\n{% else %}\n{% endif %}","typeGuard":"function isIterable($v): bool { return is_array($v) || $v instanceof \\Traversable; }","tryCatchPattern":"try {\n    $html = $twig->render('tpl.html.twig', $context);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), '\"has every\" test expects a sequence or a mapping')) {\n        // treat as failed-every path\n    } else { throw $e; }\n}","preventionTips":["Default the operand: `(items ?? []) has every(...)`","Ensure collection-typed fields are never null (DB defaults, constructor init)","Keep model refactors type-safe: a field that was a list should stay a list","Cover both null and empty cases in template tests"],"tags":["twig","test","type-mismatch","runtime-error"],"backgroundTag":"type-mismatch","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}