moodle/moodle · error · RuntimeException

() expects arguments, were provided

Error message

%s() expects {$expected} arguments, {$given} were provided

What it means

Thrown by FnDispatcher::validateArity() when a JMESPath built-in function is called with a different number of arguments than it requires. The message names the function and shows expected vs provided counts.

Solutions

  1. Count the arguments in the expression and match the built-in's documented arity
  2. Check the JMESPath spec version supported by your mtdowling/jmespath.php release for signature changes
  3. Escape or reject user-supplied expressions; validate them in a try/catch at ingest time
  4. Use the JmesPath\Env or compiler to lint expressions in tests

Example fix

// before
search('join(", ", a, b)', $data) // join takes exactly 2 args but no more variants
// after
search('join(`", "`, a)', $data) // correct arity: separator + array
Defensive patterns

Strategy: validation

Validate before calling

// Lint arity before running user expressions
foreach (['join' => 2, 'to_string' => 1] as $fn => $arity) {
    if (preg_match('/\b' . $fn . '\s*\(([^()]*)\)/', $expr, $m) && count(array_filter(array_map('trim', explode(',', $m[1])), 'strlen')) !== $arity) {
        throw new \InvalidArgumentException("$fn expects $arity arguments");
    }
}

Try / catch

try { $result = JmesPath\search($expr, $data); } catch (\RuntimeException $e) { if (strpos($e->getMessage(), 'expects') !== false && strpos($e->getMessage(), 'arguments') !== false) { throw new \InvalidArgumentException('Invalid expression: ' . $e->getMessage(), 0, $e); } throw $e; }

Prevention

When it happens

Trigger: Expression calls e.g. join() with 1 argument instead of 2, or to_string() with 2 arguments; validate() passes count($args) vs count($types) to validateArity for every dispatched built-in.

Common situations: Hand-written JMESPath expressions with wrong arity, dynamically built expressions from user input, or copying expression syntax between JMESPath implementations/spec versions with differing built-in signatures.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/22405c5091f5b3c9. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/jmespath/src/FnDispatcher.php:301

    private function typeError($from, $msg)
    {
        if (mb_strpos($from, ':', 0, 'UTF-8')) {
            list($fn, $pos) = explode(':', $from);
            throw new \RuntimeException(
                sprintf('Argument %d of %s %s', $pos, $fn, $msg)
            );
        } else {
            throw new \RuntimeException(
                sprintf('Type error: %s %s', $from, $msg)
            );
        }
    }

    private function validateArity($from, $given, $expected)
    {
        if ($given != $expected) {
            $err = "%s() expects {$expected} arguments, {$given} were provided";
            throw new \RuntimeException(sprintf($err, $from));
        }
    }

    private function validate($from, $args, $types = [])
    {
        $this->validateArity($from, count($args), count($types));
        foreach ($args as $index => $value) {
            if (!isset($types[$index]) || !$types[$index]) {
                continue;
            }
            $this->validateType("{$from}:{$index}", $value, $types[$index]);
        }
    }

    private function validateType($from, $value, array $types)
    {
        if ($types[0] == 'any'
            || in_array(Utils::type($value), $types)

View on GitHub (pinned to 48ea8c3322)