{"record":{"id":"1a32431e9ab193ba","repo":"symfony/http-foundation","slug":"expected-a-scalar-or-an-array-as-a-2nd-argument-to-s-s-given","errorCode":null,"errorMessage":"Expected a scalar, or an array as a 2nd argument to \"%s()\", \"%s\" given.","messagePattern":"Expected a scalar, or an array as a 2nd argument to \"(.+?)\\(\\)\", \"(.+?)\" given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"InputBag.php","lineNumber":79,"sourceCode":"    /**\n     * Adds input values.\n     */\n    public function add(array $inputs = []): void\n    {\n        foreach ($inputs as $input => $value) {\n            $this->set($input, $value);\n        }\n    }\n\n    /**\n     * Sets an input by name.\n     *\n     * @param string|int|float|bool|array|null $value\n     */\n    public function set(string $key, mixed $value): void\n    {\n        if (null !== $value && !\\is_scalar($value) && !\\is_array($value) && !$value instanceof \\Stringable) {\n            throw new \\InvalidArgumentException(\\sprintf('Expected a scalar, or an array as a 2nd argument to \"%s()\", \"%s\" given.', __METHOD__, get_debug_type($value)));\n        }\n\n        $this->parameters[$key] = $value;\n    }\n\n    /**\n     * Returns the input value converted to integer.\n     *\n     * @throws BadRequestException if the value cannot be converted to integer\n     */\n    public function getInt(string $key, int $default = 0): int\n    {\n        return $this->filter($key, $default, \\FILTER_VALIDATE_INT, ['flags' => \\FILTER_REQUIRE_SCALAR | \\FILTER_NULL_ON_FAILURE]) ?? throw new BadRequestException(\\sprintf('Input value \"%s\" cannot be converted to \"int\".', $key));\n    }\n\n    /**\n     * Returns the input value converted to boolean.\n     *","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/InputBag.php#L61-L97","documentation":"InputBag::set() only accepts scalar, array, null, or Stringable values. When a value of any other type (e.g. an object, resource, or closure) is passed as the second argument, the method throws this InvalidArgumentException instead of silently storing a value that would later fail string coercion. This guards the bag's contract that parameters can be safely consumed as request input.","triggerScenarios":"Calling $inputBag->set('key', $someObject) where $someObject is not scalar, not an array, not null, and does not implement Stringable; e.g. passing a DateTime, stdClass, or a typed value object without __toString().","commonSituations":"Developers copying values from a ParameterBag or a service container into an InputBag; passing a request-mapped DTO object instead of its scalar representation; refactoring code that previously used ParameterBag::set() (which accepts anything) onto InputBag, which is stricter.","solutions":["Cast the value to a scalar before setting it, e.g. (string) $value or $value->format(...) for dates.","If the object is string-convertible, make it implement Stringable (add a __toString() method).","Wrap the object's relevant data in an array if the consumer expects structure.","Use ParameterBag instead of InputBag if you genuinely need to store arbitrary types.","Add an is_scalar()/instanceof Stringable check at the call site to fail early with a clear message."],"exampleFix":"// before\n$inputBag->set('createdAt', $createdAt); // DateTimeImmutable: throws\n\n// after\n$inputBag->set('createdAt', $createdAt->format(DATE_ATOM));","handlingStrategy":"type-guard","validationCode":"// before $bag->set($key, $value)\nif (null !== $value && !is_scalar($value) && !is_array($value) && !$value instanceof \\Stringable) {\n    throw new \\UnexpectedValueException(sprintf('Key \"%s\" must be scalar/array/Stringable, %s given.', $key, get_debug_type($value)));\n}","typeGuard":"function isInputBagValue(mixed $v): bool {\n    return $v === null || is_scalar($v) || is_array($v) || $v instanceof \\Stringable;\n}","tryCatchPattern":"try {\n    $bag->set($key, $value);\n} catch (\\InvalidArgumentException $e) {\n    $logger->warning('Invalid InputBag value', ['key' => $key, 'type' => get_debug_type($value)]);\n    $bag->set($key, (string) $value); // or skip\n}","preventionTips":["Only pass scalars, arrays, nulls, or Stringable objects into InputBag::set().","Cast DateTime/value objects to strings explicitly before storing.","Prefer ParameterBag when arbitrary types are needed.","Enable PHPStan/Psalm strict types to catch object arguments at analysis time."],"tags":["php","symfony","input-validation","type-error"],"backgroundTag":"invalid-argument-value","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"}