ramsey/uuid · error · ValueError

%s(): Argument #1 ($data) is invalid

Error message

%s(): Argument #1 ($data) is invalid

What it means

When PHP unserializes a Ramsey\Uuid\Type\Time object via the modern __unserialize(array $data) path, the array must contain both 'seconds' and 'microseconds' keys. A missing key throws a native ValueError whose message mirrors PHP's internal argument errors. The payload did not come from this class's __serialize() output, or it was corrupted.

Source

Thrown at src/Type/Time.php:123

    {
        /** @var array{seconds?: float | int | string, microseconds?: float | int | string} $time */
        $time = json_decode($data, true);

        if (!isset($time['seconds']) || !isset($time['microseconds'])) {
            throw new UnsupportedOperationException('Attempted to unserialize an invalid value');
        }

        $this->__construct($time['seconds'], $time['microseconds']);
    }

    /**
     * @param array{seconds?: string, microseconds?: string} $data
     */
    public function __unserialize(array $data): void
    {
        // @codeCoverageIgnoreStart
        if (!isset($data['seconds']) || !isset($data['microseconds'])) {
            throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));
        }
        // @codeCoverageIgnoreEnd

        $this->__construct($data['seconds'], $data['microseconds']);
    }
}

View on GitHub (pinned to da5b521600)

Solutions

  1. Rebuild the object: new Time($seconds, $microseconds)
  2. Invalidate the malformed cache/queue entries so they are rewritten
  3. Pin compatible library versions on all sides that share the store
  4. Serialize primitives (two ints/strings) rather than the object itself

Example fix

// before
$time = unserialize($cachedValue); // array body missing 'seconds'/'microseconds'
// ValueError: Ramsey\Uuid\Type\Time::__unserialize(): Argument #1 ($data) is invalid

// after: persist parts and reconstruct
$time = new Time($seconds, $microseconds);
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate shape before trusting a stored payload
$decoded = unserialize($cached, ['allowed_classes' => false]);
if (!(is_array($decoded) && isset($decoded['seconds'], $decoded['microseconds']))) {
    // treat as cache miss; rebuild
    $time = new Ramsey\Uuid\Type\Time($seconds, $microseconds);
}

Try / catch

use Ramsey\Uuid\Type\Time;

try {
    $time = unserialize($cached);
} catch (\ValueError $e) {
    $time = new Time($seconds, $microseconds); // rebuild path
}

Prevention

When it happens

Trigger: unserialize() of a Time payload whose array body lacks 'seconds' or 'microseconds' — hand-edited serialized strings, cache values from a differently-shaped producer, or payloads damaged in storage.

Common situations: Serialized blobs in Redis/sessions shared across application versions; cross-service payload exchange with mismatched expectations; corrupted data after partial writes or crashes.

Related errors


AI-assisted analysis of ramsey/uuid@da5b521600 (2026-08-21). Data as JSON: /api/errors/c52788628877ae64. Report an issue: GitHub.