{"record":{"id":"c9a367193984ef37","repo":"symfony/http-foundation","slug":"expected-a-scalar-value-as-a-2nd-argument-to-s-s-given","errorCode":null,"errorMessage":"Expected a scalar value as a 2nd argument to \"%s()\", \"%s\" given.","messagePattern":"Expected a scalar value as a 2nd argument to \"(.+?)\\(\\)\", \"(.+?)\" given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"InputBag.php","lineNumber":40,"sourceCode":" * @author Saif Eddin Gmati <azjezz@protonmail.com>\n */\nfinal class InputBag extends ParameterBag\n{\n    /**\n     * Returns a scalar input value by name.\n     *\n     * @template TDefault of string|int|float|bool|null\n     *\n     * @param TDefault $default The default value if the input key does not exist\n     *\n     * @return TDefault|TInput\n     *\n     * @throws BadRequestException if the input contains a non-scalar value\n     */\n    public function get(string $key, mixed $default = null): string|int|float|bool|null\n    {\n        if (null !== $default && !\\is_scalar($default) && !$default instanceof \\Stringable) {\n            throw new \\InvalidArgumentException(\\sprintf('Expected a scalar value as a 2nd argument to \"%s()\", \"%s\" given.', __METHOD__, get_debug_type($default)));\n        }\n\n        $value = parent::get($key, $this);\n\n        if (null !== $value && $this !== $value && !\\is_scalar($value) && !$value instanceof \\Stringable) {\n            throw new BadRequestException(\\sprintf('Input value \"%s\" contains a non-scalar value.', $key));\n        }\n\n        return $this === $value ? $default : $value;\n    }\n\n    /**\n     * Replaces the current input values by a new set.\n     */\n    public function replace(array $inputs = []): void\n    {\n        $this->parameters = [];\n        $this->add($inputs);","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/InputBag.php#L22-L58","documentation":"InputBag::get() validates that the $default (2nd argument) is a scalar or Stringable, throwing InvalidArgumentException otherwise. Because get() is typed to return string|int|float|bool|null, a non-scalar default (array, object, null resource) would violate the return contract, so Symfony rejects it up front with a message naming the method and the offending type via get_debug_type().","triggerScenarios":"Calling $request->request->get('key', ['a','b']) or ->get('key', new \\DateTime()) — i.e. any array/object default passed as the 2nd argument to InputBag::get(). Note server->get()/query->get() on ParameterBag don't have this restriction; only InputBag (request, json input) does.","commonSituations":"Migrating code from ParameterBag to InputBag after Symfony 5.1+ where defaults that were arrays/objects previously worked; using a config array as a default for a request input value; copy-pasted get() calls with object defaults.","solutions":["Use a scalar or null default, e.g. ->get('key', 'default')","If you need a complex default, fetch then check: $v = $bag->get('key'); $v ??= $complexDefault;","For array input use $request->request->all('key') or InputBag::all() instead of get() with an array default","If you need Stringable support, pass an object implementing Stringable (allowed) rather than a raw object"],"exampleFix":"// before\n$page = $request->request->get('filters', ['status' => 'active']); // throws\n// after\n$filters = $request->request->all('filters') ?: ['status' => 'active'];","handlingStrategy":"type-guard","validationCode":"$default = $default ?? null;\nif (null !== $default && !is_scalar($default) && !$default instanceof Stringable) {\n    $default = null; // drop non-scalar default before calling get()\n}","typeGuard":"function isScalarOrDefault(mixed $d): bool {\n    return $d === null || is_scalar($d) || $d instanceof \\Stringable;\n}","tryCatchPattern":"try {\n    $value = $request->request->get($key, $default);\n} catch (\\InvalidArgumentException $e) {\n    $value = $request->request->get($key);\n    $value = $value ?? $defaultFallback;\n}","preventionTips":["Only pass scalar (string|int|float|bool) or null defaults to InputBag::get()","Use ->all($key) or ->getBoolean()/->getInt() helpers for structured/typed input","Remember InputBag differs from ParameterBag: complex defaults that worked before Symfony 5.1 now throw","Check get_debug_type() of your default when the error names an unexpected type"],"tags":["php","symfony","http-foundation","type-mismatch","input"],"backgroundTag":"type-mismatch","analyzedSha":"5aea19cd678fa4140f6108406f1096de5e9ed6e4","analyzedAt":"2026-09-13T01:52:22.855Z","contentChangedAt":"2026-09-13T01:52:22.855Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}