{"record":{"id":"04f7e7cf9093ae8f","repo":"symfony/symfony","slug":"cache-key-length-must-be-greater-than-zero","errorCode":null,"errorMessage":"Cache key length must be greater than zero.","messagePattern":"Cache key length must be greater than zero\\.","errorType":"exception","errorClass":"Symfony\\Component\\Cache\\Exception\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Symfony/Component/Cache/CacheItem.php","lineNumber":137,"sourceCode":"    public function getMetadata(): array\n    {\n        return $this->metadata;\n    }\n\n    /**\n     * Validates a cache key according to PSR-6.\n     *\n     * @param mixed $key The key to validate\n     *\n     * @throws InvalidArgumentException When $key is not valid\n     */\n    public static function validateKey($key): string\n    {\n        if (!\\is_string($key)) {\n            throw new InvalidArgumentException(\\sprintf('Cache key must be string, \"%s\" given.', get_debug_type($key)));\n        }\n        if ('' === $key) {\n            throw new InvalidArgumentException('Cache key length must be greater than zero.');\n        }\n        if (false !== strpbrk($key, self::RESERVED_CHARACTERS)) {\n            throw new InvalidArgumentException(\\sprintf('Cache key \"%s\" contains reserved characters \"%s\".', $key, self::RESERVED_CHARACTERS));\n        }\n\n        return $key;\n    }\n\n    /**\n     * Internal logging helper.\n     *\n     * @internal\n     */\n    public static function log(?LoggerInterface $logger, string $message, array $context = []): void\n    {\n        if ($logger) {\n            $logger->warning($message, $context);\n        } else {","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/symfony/symfony/blob/698e28026c22cf35d032cdb6e800db48b1535790/src/Symfony/Component/Cache/CacheItem.php#L119-L155","documentation":"CacheItem::validateKey() rejects empty-string keys. PSR-6 mandates that keys must be non-empty because an empty key is ambiguous and many storage backends cannot distinguish between 'no key' and 'empty key'. This InvalidArgumentException is thrown when the key is a string but equals ''.","triggerScenarios":"Calling $pool->getItem('') with an explicitly empty string, or passing a variable that resolves to an empty string (e.g. a trimmed blank input, or an empty environment variable used as a cache key).","commonSituations":"Dynamic key construction where a component is missing: $pool->getItem($prefix . '_' . $suffix) when both are empty. Or fetching a cache key from configuration that hasn't been set.","solutions":["Guard against empty keys: if ('' !== $key) { $pool->getItem($key); }.","Provide a fallback default key or skip caching when the key is empty.","Validate key inputs at the entry point of your caching layer."],"exampleFix":"// before\n$key = trim($input) ?? '';\n$item = $pool->getItem($key); // throws if empty\n\n// after\n$key = trim($input);\nif ($key === '') {\n    return $default;\n}\n$item = $pool->getItem($key);","handlingStrategy":"validation","validationCode":"if ('' === $key) {\n    return $default; // or throw your own exception\n}\n$item = $pool->getItem($key);","typeGuard":"function isNonEmptyKey(string $key): bool {\n    return '' !== $key;\n}","tryCatchPattern":"try {\n    $item = $pool->getItem($key);\n} catch (\\Symfony\\Component\\Cache\\Exception\\InvalidArgumentException $e) {\n    // skip cache for empty keys\n    return $default;\n}","preventionTips":["Check for empty strings before calling getItem().","Return early or use defaults when keys are empty.","Validate cache keys at your service boundary."],"tags":["cache","psr-6","validation","runtime"],"analyzedSha":"698e28026c22cf35d032cdb6e800db48b1535790","analyzedAt":"2026-08-06T23:40:49.025Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}