{"record":{"id":"b70db5946ed795a1","repo":"ramsey/uuid","slug":"length-must-be-a-positive-integer-greater-than-or","errorCode":null,"errorMessage":"Length must be a positive integer greater than or equal to 6","messagePattern":"Length must be a positive integer greater than or equal to 6","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Generator/CombGenerator.php","lineNumber":77,"sourceCode":"class CombGenerator implements RandomGeneratorInterface\n{\n    public const TIMESTAMP_BYTES = 6;\n\n    public function __construct(\n        private RandomGeneratorInterface $generator,\n        private NumberConverterInterface $numberConverter\n    ) {\n    }\n\n    /**\n     * @throws InvalidArgumentException if $length is not a positive integer greater than or equal to CombGenerator::TIMESTAMP_BYTES\n     *\n     * @inheritDoc\n     */\n    public function generate(int $length): string\n    {\n        if ($length < self::TIMESTAMP_BYTES) {\n            throw new InvalidArgumentException(\n                'Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES\n            );\n        }\n\n        if ($length % 2 !== 0) {\n            throw new InvalidArgumentException('Length must be an even number');\n        }\n\n        $hash = '';\n\n        /** @phpstan-ignore greater.alwaysTrue (TIMESTAMP_BYTES constant could change in child classes) */\n        if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {\n            $hash = $this->generator->generate($length - self::TIMESTAMP_BYTES);\n        }\n\n        $lsbTime = str_pad(\n            $this->numberConverter->toHex($this->timestamp()),\n            self::TIMESTAMP_BYTES * 2,","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Generator/CombGenerator.php#L59-L95","documentation":"CombGenerator (deprecated in favor of UUIDv7) builds COMB identifiers by placing a 48-bit timestamp into a byte string of a requested length; generate() rejects lengths below TIMESTAMP_BYTES (6) with InvalidArgumentException because there would be no room for the timestamp.","triggerScenarios":"$combGenerator->generate(4) (or any length under 6); adapting CombGenerator as a general-purpose random generator and requesting small sizes.","commonSituations":"Custom token/ID code reusing the CombGenerator example with arbitrary lengths; assuming it behaves like RandomBytesGenerator for any size; legacy COMB code touched during a UUIDv7 migration.","solutions":["Request a length of at least 6 bytes (and even).","Use RandomBytesGenerator when you need arbitrary-length random bytes without a timestamp.","Migrate time-ordered identifiers to Uuid::uuid7() - CombGenerator is deprecated."],"exampleFix":"// before\n$bytes = $combGenerator->generate(4); // InvalidArgumentException\n\n// after\n$bytes = $combGenerator->generate(16); // >= 6 and even\n// or migrate: $uuid = Uuid::uuid7();","handlingStrategy":"validation","validationCode":"function assertCombLength(int $length): void\n{\n    if ($length < 6) {\n        throw new InvalidArgumentException('COMB length must be >= 6 bytes');\n    }\n    if ($length % 2 !== 0) {\n        throw new InvalidArgumentException('COMB length must be even');\n    }\n}\n\nassertCombLength($length);\n$bytes = $combGenerator->generate($length);","typeGuard":"function isValidCombLength(int $length): bool\n{\n    return $length >= 6 && $length % 2 === 0;\n}","tryCatchPattern":null,"preventionTips":["Validate configurable lengths once at boot (>= 6, even).","Default to 16 bytes - the UUID width COMBs are designed for.","Plan the migration to Uuid::uuid7(); CombGenerator is deprecated.","Unit-test your length configuration so invalid sizes fail loudly in CI, not production."],"tags":["php","ramsey-uuid","comb-generator","generator-length","deprecated"],"backgroundTag":"generator-invalid-length","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}