{"record":{"id":"0432a8b4d8334222","repo":"ramsey/uuid","slug":"attempted-to-unserialize-an-invalid-value","errorCode":null,"errorMessage":"Attempted to unserialize an invalid value","messagePattern":"Attempted to unserialize an invalid value","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/Type/Time.php","lineNumber":110,"sourceCode":"    {\n        return [\n            'seconds' => $this->getSeconds()->toString(),\n            'microseconds' => $this->getMicroseconds()->toString(),\n        ];\n    }\n\n    /**\n     * Constructs the object from a serialized string representation\n     *\n     * @param string $data The serialized string representation of the object\n     */\n    public function unserialize(string $data): void\n    {\n        /** @var array{seconds?: float | int | string, microseconds?: float | int | string} $time */\n        $time = json_decode($data, true);\n\n        if (!isset($time['seconds']) || !isset($time['microseconds'])) {\n            throw new UnsupportedOperationException('Attempted to unserialize an invalid value');\n        }\n\n        $this->__construct($time['seconds'], $time['microseconds']);\n    }\n\n    /**\n     * @param array{seconds?: string, microseconds?: string} $data\n     */\n    public function __unserialize(array $data): void\n    {\n        // @codeCoverageIgnoreStart\n        if (!isset($data['seconds']) || !isset($data['microseconds'])) {\n            throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));\n        }\n        // @codeCoverageIgnoreEnd\n\n        $this->__construct($data['seconds'], $data['microseconds']);\n    }","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Type/Time.php#L92-L128","documentation":"Ramsey\\Uuid\\Type\\Time is the value object for timestamp parts (seconds plus microseconds). Its legacy unserialize(string $data) entry point json_decode()s the stored string and requires both 'seconds' and 'microseconds' keys; if either is missing it throws Ramsey\\Uuid\\Exception\\UnsupportedOperationException. This fires when the serialized representation being restored is not the JSON pair this class writes — corrupted, foreign, or hand-built payloads.","triggerScenarios":"Calling unserialize() on a serialized Time object whose stored body is not JSON containing both keys — e.g. a cache entry from an incompatible format, a truncated payload, or calling $time->unserialize('garbage') directly in legacy Serializable-style code.","commonSituations":"Caches or queues carrying serialized Time objects written by older/other code; storage truncation of serialized blobs; partial payloads after failed writes; migrations between serialization libraries.","solutions":["Construct the value directly instead of restoring it: new Time($seconds, $microseconds)","Store the components (or a single integer timestamp) and rebuild Time on read","Catch UnsupportedOperationException at the cache layer and treat the entry as a miss","Regenerate/flush stale serialized entries after any serialization format change"],"exampleFix":"// before\n$time = unserialize($cachedPayload);\n// UnsupportedOperationException: Attempted to unserialize an invalid value\n\n// after: store components and rebuild\n$time = new Time($cachedSeconds, $cachedMicroseconds);","handlingStrategy":"try-catch","validationCode":"// Validate the JSON shape before calling unserialize() manually\n$decoded = json_decode($data, true);\nif (!is_array($decoded) || !isset($decoded['seconds'], $decoded['microseconds'])) {\n    throw new \\InvalidArgumentException('Time payload must contain seconds and microseconds');\n}\n$time->unserialize($data);","typeGuard":null,"tryCatchPattern":"use Ramsey\\Uuid\\Exception\\UnsupportedOperationException;\n\ntry {\n    $time = unserialize($cachedPayload);\n} catch (UnsupportedOperationException $e) {\n    // malformed stored Time: rebuild from source timestamps\n    $time = new Ramsey\\Uuid\\Type\\Time($seconds, $microseconds);\n}","preventionTips":["Store seconds/microseconds as plain values and construct Time on read","Never hand-edit serialized Time payloads","Catch UnsupportedOperationException at the cache layer and treat entries as misses","Regenerate serialized entries after changing storage formats"],"tags":["php","ramsey-uuid","time","unserialize","json","cache-corruption"],"backgroundTag":"unserialize-payload-invalid","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}