{"record":{"id":"3a18138f2a5c3797","repo":"symfony/http-foundation","slug":"parameter-value-s-cannot-be-filtered","errorCode":null,"errorMessage":"Parameter value \"%s\" cannot be filtered.","messagePattern":"Parameter value \"(.+?)\" cannot be filtered\\.","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"ParameterBag.php","lineNumber":232,"sourceCode":"     * @throws UnexpectedValueException if the parameter value is a non-stringable object\n     * @throws UnexpectedValueException if the parameter value is invalid and \\FILTER_NULL_ON_FAILURE is not set\n     */\n    public function filter(string $key, mixed $default = null, int $filter = \\FILTER_DEFAULT, mixed $options = []): mixed\n    {\n        $value = $this->get($key, $default);\n\n        // Always turn $options into an array - this allows filter_var option shortcuts.\n        if (!\\is_array($options) && $options) {\n            $options = ['flags' => $options];\n        }\n\n        // Add a convenience check for arrays.\n        if (\\is_array($value) && !isset($options['flags'])) {\n            $options['flags'] = \\FILTER_REQUIRE_ARRAY;\n        }\n\n        if (\\is_object($value) && !$value instanceof \\Stringable) {\n            throw new UnexpectedValueException(\\sprintf('Parameter value \"%s\" cannot be filtered.', $key));\n        }\n\n        if ((\\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \\Closure)) {\n            throw new \\InvalidArgumentException(\\sprintf('A Closure must be passed to \"%s()\" when FILTER_CALLBACK is used, \"%s\" given.', __METHOD__, get_debug_type($options['options'] ?? null)));\n        }\n\n        $options['flags'] ??= 0;\n        $nullOnFailure = $options['flags'] & \\FILTER_NULL_ON_FAILURE;\n        $options['flags'] |= \\FILTER_NULL_ON_FAILURE;\n\n        $value = filter_var($value, $filter, $options);\n\n        if (null !== $value || $nullOnFailure) {\n            return $value;\n        }\n\n        throw new \\UnexpectedValueException(\\sprintf('Parameter value \"%s\" is invalid and flag \"FILTER_NULL_ON_FAILURE\" was not set.', $key));\n    }","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/ParameterBag.php#L214-L250","documentation":"ParameterBag::filter() applies a PHP filter (filter_var) to a stored parameter. It throws UnexpectedValueException when the stored value is an object that does not implement Stringable, because filter_var cannot meaningfully filter such values. Arrays are auto-handled by adding FILTER_REQUIRE_ARRAY, but plain objects are rejected.","triggerScenarios":"$bag->filter('key', default, FILTER_VALIDATE_INT) (or any filter) where the parameter holds a non-Stringable object such as a DateTime, stdClass, or a service instance. Also indirectly via getInt/getBoolean/filterCallback when the stored value is such an object.","commonSituations":"Someone put an object into the bag (e.g. $bag->set('limit', new \\DateTime())) while other code assumes scalars; dependency injection misconfiguration; deserialization produced objects instead of scalars.","solutions":["Store scalars (string/int/bool) or Stringable objects in the bag instead of raw objects","Cast or extract a scalar from the object before filtering (e.g. ->getTimestamp(), (string) $obj with __toString)","Retrieve with get() and filter manually after converting to a scalar","Fix the code path that sets the object into the parameter bag"],"exampleFix":"// before\n$ts = $bag->getInt('created_at'); // 'created_at' holds a DateTime -> throws\n// after\n$ts = $bag->get('created_at') instanceof \\DateTime ? $bag->get('created_at')->getTimestamp() : $bag->getInt('created_at');","handlingStrategy":"type-guard","validationCode":"if (isset($value) && is_object($value) && !$value instanceof \\Stringable) {\n    throw new \\InvalidArgumentException('Parameter must be scalar or Stringable before filtering');\n}","typeGuard":"function isFilterable(mixed $v): bool {\n    return null === $v || is_scalar($v) || $v instanceof \\Stringable || is_array($v);\n}","tryCatchPattern":"try {\n    $n = $bag->filter('key', 0, \\FILTER_VALIDATE_INT);\n} catch (\\UnexpectedValueException $e) {\n    $n = 0;\n}","preventionTips":["Never put raw objects into a ParameterBag; store scalars or Stringable wrappers","Add a __toString to value objects you must store","Convert objects to scalars at set() time, not read() time","Audit $bag->set() call sites for object arguments"],"tags":["parameters","filter","type-mismatch","validation"],"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"}