{"record":{"id":"671a672678b3068e","repo":"twigphp/Twig","slug":"the-map-filter-expects-a-sequence-or-a-mapping-got-s","errorCode":null,"errorMessage":"The \"map\" filter expects a sequence or a mapping, got \"%s\".","messagePattern":"The \"map\" filter expects a sequence or a mapping, got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":2101,"sourceCode":"\n        foreach ($array as $k => $v) {\n            if ($arrow($v, $k)) {\n                return $v;\n            }\n        }\n\n        return null;\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function map(Environment $env, bool $isSandboxed, $array, $arrow)\n    {\n        if (!is_iterable($array)) {\n            throw new RuntimeError(\\sprintf('The \"map\" filter expects a sequence or a mapping, got \"%s\".', get_debug_type($array)));\n        }\n\n        self::checkArrow($isSandboxed, $arrow, 'map', 'filter');\n\n        $r = [];\n        foreach ($array as $k => $v) {\n            $r[$k] = $arrow($v, $k);\n        }\n\n        return $r;\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function reduce(Environment $env, bool $isSandboxed, $array, $arrow, $initial = null)","sourceCodeStart":2083,"sourceCodeEnd":2119,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L2083-L2119","documentation":"The Twig `map` filter (CoreExtension::map, invoked via the twig_array_map helper) requires an iterable argument to transform element-by-element. Twig throws this RuntimeError when the piped value is not an array or Traversable. Because Twig filters are applied at runtime, non-iterable input is only detected when the template executes.","triggerScenarios":"`{{ value|map(v => v * 2) }}` where value is null, a string, an int, or a bool. Also happens when a helper function returns false on error and its result is piped straight into `map`. Guard at CoreExtension.php:2101.","commonSituations":"Null context variables (missing optional data), API/config values that are strings rather than arrays, a function returning `false` instead of `[]` on empty results, refactors where a variable changed from collection to single entity.","solutions":["Coerce to array first: `{{ (value ?? [])|map(...) }}` or wrap with `|default([])`.","Verify what the producing expression returns and fix it at the source (controller, repository, service).","Use `{% if value is iterable %}` to branch when the type legitimately varies.","Catch Twig\\Error\\RuntimeError in the rendering code and log the template line to identify the variable."],"exampleFix":"// before (Twig)\n{{ prices|map(p => p * 1.2) }}\n\n// after\n{{ (prices ?? [])|map(p => p * 1.2) }}","handlingStrategy":"validation","validationCode":"// PHP, before rendering\ndef ensure_iterable_for_map($value): void {\n    if (!is_iterable($value)) {\n        throw new InvalidArgumentException('map() input must be iterable, got ' . get_debug_type($value));\n    }\n}","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(), '\"map\" filter expects a sequence or a mapping')) {\n        // log variable name, fall back to empty rendering\n    } else { throw $e; }\n}","preventionTips":["Pipe `(value ?? [])` into `map` whenever the value can be null","Type-hint context builders so collection variables cannot silently become scalars","Add template smoke tests for pages whose data may be empty","Avoid PHP functions in context that return false on failure; wrap with `?: []`"],"tags":["twig","filter","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"}