ramsey/uuid · error · InvalidArgumentException

Fields used to create a UuidV6 must represent a version 6 (r

Error message

Fields used to create a UuidV6 must represent a version 6 (reordered time) UUID

What it means

Ramsey\Uuid\Nonstandard\UuidV6 is the deprecated pre-RFC home of version 6 UUIDs (deprecated in favor of Ramsey\Uuid\Rfc4122\UuidV6). Its constructor verifies the fields actually report version 6 (UUID_TYPE_REORDERED_TIME); constructing it with fields of any other version throws InvalidArgumentException to prevent a mislabeled object.

Source

Thrown at src/Nonstandard/UuidV6.php:60

    use TimeTrait;

    /**
     * Creates a version 6 (reordered Gregorian time) UUID
     *
     * @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
     * @param NumberConverterInterface $numberConverter The number converter to use for converting hex values to/from integers
     * @param CodecInterface $codec The codec to use when encoding or decoding UUID strings
     * @param TimeConverterInterface $timeConverter The time converter to use for converting timestamps extracted from a
     *     UUID to unix timestamps
     */
    public function __construct(
        Rfc4122FieldsInterface $fields,
        NumberConverterInterface $numberConverter,
        CodecInterface $codec,
        TimeConverterInterface $timeConverter,
    ) {
        if ($fields->getVersion() !== BaseUuid::UUID_TYPE_REORDERED_TIME) {
            throw new InvalidArgumentException(
                'Fields used to create a UuidV6 must represent a version 6 (reordered time) UUID',
            );
        }

        parent::__construct($fields, $numberConverter, $codec, $timeConverter);
    }

    /**
     * Converts this UUID into an instance of a version 1 UUID
     */
    public function toUuidV1(): UuidV1
    {
        $hex = $this->getHex()->toString();
        $hex = substr($hex, 7, 5)
            . substr($hex, 13, 3)
            . substr($hex, 3, 4)
            . '1' . substr($hex, 0, 3)
            . substr($hex, 16);

View on GitHub (pinned to da5b521600)

Solutions

  1. Replace Nonstandard\UuidV6 with Rfc4122\UuidV6 everywhere (the class is deprecated)
  2. If you must construct it, feed it fields with a version-6 nibble (e.g. from Uuid::uuid6()->getFields())
  3. Prefer factory APIs (Uuid::uuid6(), Uuid::fromString()) over manual construction

Example fix

// before
use Ramsey\Uuid\Nonstandard\UuidV6;
$v6 = new UuidV6($v4fields, $nc, $codec, $tc); // version 4 fields

// after
use Ramsey\Uuid\Rfc4122\UuidV6;
$v6 = \Ramsey\Uuid\Uuid::fromString('1ef0f0c8-0f4b-6f00-9f3f-0242ac110002'); // verbatim v6
// or: $v6 = \Ramsey\Uuid\Uuid::v6();
Defensive patterns

Strategy: validation

Validate before calling

use Ramsey\Uuid\Uuid;
if ($fields->getVersion() !== Uuid::UUID_TYPE_REORDERED_TIME) {
    throw new InvalidArgumentException('Nonstandard\UuidV6 requires version 6 fields');
}
$v6 = new \Ramsey\Uuid\Nonstandard\UuidV6($fields, $nc, $codec, $tc);

Type guard

function isVersion6Fields(\Ramsey\Uuid\Rfc4122\FieldsInterface $fields): bool
{
    return $fields->getVersion() === \Ramsey\Uuid\Uuid::UUID_TYPE_REORDERED_TIME;
}

Try / catch

try {
    $v6 = new \Ramsey\Uuid\Nonstandard\UuidV6($fields, $nc, $codec, $tc);
} catch (\Ramsey\Uuid\Exception\InvalidArgumentException $e) {
    // fields are not v6; fall back to factory-generated v6
    $v6 = \Ramsey\Uuid\Uuid::uuid6();
}

Prevention

When it happens

Trigger: Directly calling new Nonstandard\UuidV6($fields, ...) with Rfc4122FieldsInterface fields whose version nibble is not 6 — typical in code upgraded from ramsey/uuid 4.x that built v6 objects manually, custom factories, or DI containers that autowire the constructor with mismatched fields.

Common situations: Upgrading ramsey/uuid from 4.x to 5.x where Nonstandard\UuidV6 still exists but builders now produce Rfc4122\UuidV6; copy-pasted custom builder code; unit tests constructing version objects by hand.

Related errors


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