Skip to content

Commit b366dd9

Browse files
authored
Replace magic numbers with constants (#50)
1 parent 3ace7fc commit b366dd9

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

src/UUID.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ class UUID
5959
public const TIME_OFFSET_INT = 0x01b21dd213814000;
6060

6161
/** @internal */
62-
private const INT_1E7 = 10_000_000;
62+
private const SUBSEC_RANGE = 10_000_000;
6363

6464
/** @internal */
65-
private const SUBSEC_BITS = 14;
65+
private const V7_SUBSEC_RANGE = 10_000;
66+
67+
/** @internal */
68+
private const V7_SUBSEC_BITS = 14;
6669

6770
/** @internal */
6871
private const UUID_REGEX = '/^(?:urn:)?(?:uuid:)?(\{)?([0-9a-f]{8})\-?([0-9a-f]{4})'
@@ -83,7 +86,7 @@ private static function getUnixTime(): array
8386
if (self::$unixts > $unixts || self::$unixts === $unixts && self::$subsec >= $subsec) {
8487
$unixts = self::$unixts;
8588
$subsec = self::$subsec;
86-
if ($subsec >= 9999999) {
89+
if ($subsec >= self::SUBSEC_RANGE - 1) {
8790
$subsec = 0;
8891
$unixts++;
8992
} else {
@@ -135,13 +138,13 @@ private static function uuidFromHex(string $uhex, int $version): string
135138
/** @internal */
136139
private static function encodeSubsec(int $value): int
137140
{
138-
return intdiv($value << self::SUBSEC_BITS, 10000);
141+
return intdiv($value << self::V7_SUBSEC_BITS, self::V7_SUBSEC_RANGE);
139142
}
140143

141144
/** @internal */
142145
private static function decodeSubsec(int $value): int
143146
{
144-
return -(-$value * 10000 >> self::SUBSEC_BITS);
147+
return -(-$value * self::V7_SUBSEC_RANGE >> self::V7_SUBSEC_BITS);
145148
}
146149

147150
/**
@@ -195,7 +198,7 @@ public static function uuid5(string $namespace, string $name): string
195198
public static function uuid6(): string
196199
{
197200
[$unixts, $subsec] = self::getUnixTime();
198-
$timestamp = $unixts * self::INT_1E7 + $subsec;
201+
$timestamp = $unixts * self::SUBSEC_RANGE + $subsec;
199202
$timehex = str_pad(dechex($timestamp + self::TIME_OFFSET_INT), 15, '0', \STR_PAD_LEFT);
200203
$uhex = substr_replace(substr($timehex, -15), '6', -3, 0);
201204
$uhex .= bin2hex(random_bytes(8));
@@ -211,8 +214,8 @@ public static function uuid6(): string
211214
public static function uuid7(): string
212215
{
213216
[$unixts, $subsec] = self::getUnixTime();
214-
$unixtsms = $unixts * 1000 + intdiv($subsec, 10000);
215-
$subsec = self::encodeSubsec($subsec % 10000);
217+
$unixtsms = $unixts * 1000 + intdiv($subsec, self::V7_SUBSEC_RANGE);
218+
$subsec = self::encodeSubsec($subsec % self::V7_SUBSEC_RANGE);
216219
$subsecA = $subsec >> 2;
217220
$subsecB = $subsec & 0x03;
218221
$randB = random_bytes(8);
@@ -269,7 +272,7 @@ public static function getTime(string $uuid): ?string
269272
} elseif ($version === 7) {
270273
$unixts = hexdec(substr($timehex, 0, 13));
271274
$subsec = self::decodeSubsec(hexdec(substr($timehex, 13)) + (hexdec(substr($uuid, 16, 1)) >> 4 & 0x03));
272-
$retval = strval($unixts * 10000 + $subsec);
275+
$retval = strval($unixts * self::V7_SUBSEC_RANGE + $subsec);
273276
$retval = substr_replace(str_pad($retval, 8, '0', \STR_PAD_LEFT), '.', -7, 0);
274277
}
275278
return $retval;

0 commit comments

Comments
 (0)