twigphp/Twig · error · RuntimeError
The "has some" test expects a sequence or a mapping, got
Error message
The "has some" test expects a sequence or a mapping, got "%s".
What it means
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.
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.
Example fix
// before (Twig)
{% if roles has some(r => r == 'ADMIN') %}...{% endif %}
// after
{% if roles is iterable and roles has some(r => r == 'ADMIN') %}...{% endif %} Defensive patterns
Strategy: validation
Validate before calling
// Twig: guard before the test
{% if tags is iterable and tags has some(t => t.active) %}
{% else %}
{% endif %} Type guard
function isIterable($v): bool { return is_array($v) || $v instanceof \Traversable; } Try / catch
try {
$html = $twig->render('tpl.html.twig', $context);
} catch (\Twig\Error\RuntimeError $e) {
if (str_contains($e->getMessage(), '"has some" test expects a sequence or a mapping')) {
// treat as "no match" path
} else { throw $e; }
} Prevention
- 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
When it happens
Trigger: `{% 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- The "has every" test expects a sequence or a mapping, got
- The "find" filter expects a sequence or a mapping, got
- The "map" filter expects a sequence or a mapping, got
- The "reduce" filter expects a sequence or a mapping, got
- Failed to load Twig template
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/df1391245f5dccba.
Report an issue: GitHub.
Appendix: source
Thrown at src/Extension/CoreExtension.php:2143
self::checkArrow($isSandboxed, $arrow, 'reduce', 'filter');
$accumulator = $initial;
foreach ($array as $key => $value) {
$accumulator = $arrow($accumulator, $value, $key);
}
return $accumulator;
}
/**
* @param \Closure $arrow
*
* @internal
*/
public static function arraySome(Environment $env, $array, $arrow, bool $isSandboxed = false)
{
if (!is_iterable($array)) {
throw new RuntimeError(\sprintf('The "has some" test expects a sequence or a mapping, got "%s".', get_debug_type($array)));
}
self::checkArrow($isSandboxed, $arrow, 'has some', 'operator');
foreach ($array as $k => $v) {
if ($arrow($v, $k)) {
return true;
}
}
return false;
}
/**
* @param \Closure $arrow
*
* @internal
*/View on GitHub (pinned to a414c3a491)