twigphp/Twig · error · RuntimeError

The "has every" test expects a sequence or a mapping, got

Error message

The "has every" test expects a sequence or a mapping, got "%s".

What it means

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.

Solutions

  1. Default to an empty array first: `{% if (items ?? []) has every(...) %}`.
  2. Combine with an iterable check: `{% if items is iterable and items has every(...) %}`.
  3. Initialize collections upstream (constructor defaults, `?? []` in the controller) so the field is always an array.
  4. Catch Twig\Error\RuntimeError around render and use the template/line info in the message to locate the expression.

Example fix

// before (Twig)
{% if entries has every(e => e.published) %}...{% endif %}

// after
{% if (entries ?? []) has every(e => e.published) %}...{% endif %}
Defensive patterns

Strategy: validation

Validate before calling

// Twig: guard before the test
{% if items is iterable and items has every(i => i.valid) %}
{% 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 every" test expects a sequence or a mapping')) {
        // treat as failed-every path
    } else { throw $e; }
}

Prevention

When it happens

Trigger: `{% 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.

Common situations: 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).

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


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/ce631906c6b5d4f4. Report an issue: GitHub.

Appendix: source

Thrown at src/Extension/CoreExtension.php:2165

        foreach ($array as $k => $v) {
            if ($arrow($v, $k)) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param \Closure $arrow
     *
     * @internal
     */
    public static function arrayEvery(Environment $env, $array, $arrow, bool $isSandboxed = false)
    {
        if (!is_iterable($array)) {
            throw new RuntimeError(\sprintf('The "has every" test expects a sequence or a mapping, got "%s".', get_debug_type($array)));
        }

        self::checkArrow($isSandboxed, $arrow, 'has every', 'operator');

        foreach ($array as $k => $v) {
            if (!$arrow($v, $k)) {
                return false;
            }
        }

        return true;
    }

    /**
     * @internal
     */
    public static function checkArrow(bool $isSandboxed, $arrow, $thing, $type): void
    {

View on GitHub (pinned to a414c3a491)