{"record":{"id":"df1391245f5dccba","repo":"twigphp/Twig","slug":"the-has-some-test-expects-a-sequence-or-a-mapping-got-s","errorCode":null,"errorMessage":"The \"has some\" test expects a sequence or a mapping, got \"%s\".","messagePattern":"The \"has some\" test expects a sequence or a mapping, got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":2143,"sourceCode":"        self::checkArrow($isSandboxed, $arrow, 'reduce', 'filter');\n\n        $accumulator = $initial;\n        foreach ($array as $key => $value) {\n            $accumulator = $arrow($accumulator, $value, $key);\n        }\n\n        return $accumulator;\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function arraySome(Environment $env, $array, $arrow, bool $isSandboxed = false)\n    {\n        if (!is_iterable($array)) {\n            throw new RuntimeError(\\sprintf('The \"has some\" test expects a sequence or a mapping, got \"%s\".', get_debug_type($array)));\n        }\n\n        self::checkArrow($isSandboxed, $arrow, 'has some', 'operator');\n\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     */","sourceCodeStart":2125,"sourceCodeEnd":2161,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L2125-L2161","documentation":"The Twig `has some` test (CoreExtension::arraySome, exposed via twig_array_some) checks whether at least one element of an iterable satisfies an arrow function. Twig throws this RuntimeError when the left-hand operand is not iterable. Like the array filters, the check runs at template execution time.","triggerScenarios":"`{% if tags has some(t => t.active) %}` where tags is null, a string, or another scalar. The iterable guard is at CoreExtension.php:2143, and the operand comes from the left side of the `has some` expression.","commonSituations":"Testing membership on a field that is null when unset (optional tags/roles), variables that are scalar in some entity states, typos where a single value is passed instead of the collection containing it.","solutions":["Ensure the left operand is an array: wrap single values `value ? [value] : []` or default `{{ (tags ?? []) has some(...) }}`.","Use `is iterable` before the test: `{% if tags is iterable and tags has some(...) %}`.","Fix the data source so the collection is always initialized (e.g. Doctrine `ArrayCollection` in the constructor).","Catch Twig\\Error\\RuntimeError in the renderer and inspect the template trace for the failing expression."],"exampleFix":"// before (Twig)\n{% if roles has some(r => r == 'ADMIN') %}...{% endif %}\n\n// after\n{% if roles is iterable and roles has some(r => r == 'ADMIN') %}...{% endif %}","handlingStrategy":"validation","validationCode":"// Twig: guard before the test\n{% if tags is iterable and tags has some(t => t.active) %}\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 some\" test expects a sequence or a mapping')) {\n        // treat as \"no match\" path\n    } else { throw $e; }\n}","preventionTips":["Wrap single values in an array before quantifier tests: `value ? [value] : []`","Initialize Doctrine collections to ArrayCollection in entity constructors","Prefer `(x ?? [])` on the left side of `has some` when null is possible","Add tests covering entity states where the collection is null"],"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"}