{"record":{"id":"2d44f9e94d3a36be","repo":"ramsey/uuid","slug":"s-argument-1-data-is-invalid","errorCode":null,"errorMessage":"%s(): Argument #1 ($data) is invalid","messagePattern":"(.+?)\\(\\): Argument #1 \\(\\$data\\) is invalid","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/Fields/SerializableFieldsTrait.php","lineNumber":77,"sourceCode":"     * @param string $data The serialized string representation of the object\n     */\n    public function unserialize(string $data): void\n    {\n        if (strlen($data) === 16) {\n            $this->__construct($data);\n        } else {\n            $this->__construct(base64_decode($data));\n        }\n    }\n\n    /**\n     * @param array{bytes?: string} $data\n     */\n    public function __unserialize(array $data): void\n    {\n        // @codeCoverageIgnoreStart\n        if (!isset($data['bytes'])) {\n            throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));\n        }\n        // @codeCoverageIgnoreEnd\n\n        $this->unserialize($data['bytes']);\n    }\n}\n","sourceCodeStart":59,"sourceCodeEnd":84,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Fields/SerializableFieldsTrait.php#L59-L84","documentation":"UUID fields classes use SerializableFieldsTrait; __unserialize() expects exactly the array layout __serialize() emits - a 'bytes' key holding the 16-byte string. A ValueError ('SerializableFieldsTrait...::__unserialize(): Argument #1 ($data) is invalid') means the payload array lacks that key, i.e. the serialized representation is corrupt or hand-built.","triggerScenarios":"unserialize() over a truncated or rewritten serialization of a Fields object (or a Uuid that embeds fields) whose array no longer contains 'bytes'; manually constructing the payload array without the 'bytes' key.","commonSituations":"Redis/Memcached/file-cache entries corrupted by partial writes, eviction bugs, or version skew across ramsey/uuid upgrades; session data truncated by storage limits; custom serializers or object mappers mangling the internal array.","solutions":["Treat the entry as poison: delete/regenerate the cached serialized UUIDs instead of repairing payloads.","Wrap unserialize() in try/catch (ValueError / Throwable) and fall back to a cache miss + regenerate.","Store UUIDs in caches as canonical strings and re-parse with Uuid::fromString(), not as serialized objects."],"exampleFix":"// before\n$fields = unserialize($cachedSerializedFields); // ValueError if payload lost 'bytes'\n\n// after\ntry {\n    $fields = unserialize($cachedSerializedFields);\n} catch (\\Throwable $e) {\n    $cache->delete($key);\n    $fields = null; // regenerate from the UUID string\n}","handlingStrategy":"try-catch","validationCode":"$payload = @unserialize($cached, ['allowed_classes' => true]);\nif (!is_array($payload) || !isset($payload['bytes'])) {\n    // missing 'bytes' key: treat as corrupt, regenerate\n    $payload = null;\n}","typeGuard":"function isSerializableFieldsPayload(mixed $data): bool\n{\n    return is_array($data) && isset($data['bytes']) && is_string($data['bytes']);\n}","tryCatchPattern":"try {\n    $object = unserialize($cached);\n} catch (\\ValueError $e) {\n    $cache->delete($key); // poison entry\n    $object = null;       // regenerate from source of truth\n}","preventionTips":["Cache UUIDs as canonical strings, not serialized objects.","Version cache keys with the library major version to avoid unserializing cross-version payloads.","Wrap unserialize() in try/catch and treat failures as cache misses.","Use stampede-safe cache invalidation so partial writes do not persist."],"tags":["php","ramsey-uuid","serialization","unserialize","cache-corruption"],"backgroundTag":"php-unserialize-corruption","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}