{"record":{"id":"22405c5091f5b3c9","repo":"moodle/moodle","slug":"s-expects-expected-arguments-given-were-provided","errorCode":null,"errorMessage":"%s() expects {$expected} arguments, {$given} were provided","messagePattern":"(.+?)\\(\\) expects (.+?) arguments, (.+?) were provided","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"public/lib/jmespath/src/FnDispatcher.php","lineNumber":301,"sourceCode":"    private function typeError($from, $msg)\n    {\n        if (mb_strpos($from, ':', 0, 'UTF-8')) {\n            list($fn, $pos) = explode(':', $from);\n            throw new \\RuntimeException(\n                sprintf('Argument %d of %s %s', $pos, $fn, $msg)\n            );\n        } else {\n            throw new \\RuntimeException(\n                sprintf('Type error: %s %s', $from, $msg)\n            );\n        }\n    }\n\n    private function validateArity($from, $given, $expected)\n    {\n        if ($given != $expected) {\n            $err = \"%s() expects {$expected} arguments, {$given} were provided\";\n            throw new \\RuntimeException(sprintf($err, $from));\n        }\n    }\n\n    private function validate($from, $args, $types = [])\n    {\n        $this->validateArity($from, count($args), count($types));\n        foreach ($args as $index => $value) {\n            if (!isset($types[$index]) || !$types[$index]) {\n                continue;\n            }\n            $this->validateType(\"{$from}:{$index}\", $value, $types[$index]);\n        }\n    }\n\n    private function validateType($from, $value, array $types)\n    {\n        if ($types[0] == 'any'\n            || in_array(Utils::type($value), $types)","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/jmespath/src/FnDispatcher.php#L283-L319","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Count the arguments in the expression and match the built-in's documented arity","Check the JMESPath spec version supported by your mtdowling/jmespath.php release for signature changes","Escape or reject user-supplied expressions; validate them in a try/catch at ingest time","Use the JmesPath\\Env or compiler to lint expressions in tests"],"exampleFix":"// before\nsearch('join(\", \", a, b)', $data) // join takes exactly 2 args but no more variants\n// after\nsearch('join(`\", \"`, a)', $data) // correct arity: separator + array","handlingStrategy":"validation","validationCode":"// Lint arity before running user expressions\nforeach (['join' => 2, 'to_string' => 1] as $fn => $arity) {\n    if (preg_match('/\\b' . $fn . '\\s*\\(([^()]*)\\)/', $expr, $m) && count(array_filter(array_map('trim', explode(',', $m[1])), 'strlen')) !== $arity) {\n        throw new \\InvalidArgumentException(\"$fn expects $arity arguments\");\n    }\n}","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Check each built-in's documented signature when writing expressions","Lint user-supplied expressions at ingest time with a try/search in a sandbox","Watch for spec-version differences when porting expressions between tools (aws cli, python jmepsath, php)","Keep expression strings in constants/tests rather than inline string building"],"tags":["php","jmespath","arity","argument-count"],"backgroundTag":"missing-required-argument","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}