Skip to content

Commit d7a7419

Browse files
committed
Remove generate methods and GenerateInterface with related tests and usages
1 parent 4d9280a commit d7a7419

File tree

6 files changed

+2
-146
lines changed

6 files changed

+2
-146
lines changed

src/Code/Contract/GenerateInterface.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/String/StringUuidV4.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
namespace PhpTypedValues\String;
66

7-
use PhpTypedValues\Code\Contract\GenerateInterface;
87
use PhpTypedValues\Code\Exception\StringTypeException;
98
use PhpTypedValues\Code\String\StrType;
10-
use Random\RandomException;
119

12-
use function chr;
13-
use function ord;
1410
use function preg_match;
1511
use function sprintf;
1612
use function strtolower;
@@ -20,7 +16,7 @@
2016
*
2117
* @psalm-immutable
2218
*/
23-
readonly class StringUuidV4 extends StrType implements GenerateInterface
19+
readonly class StringUuidV4 extends StrType
2420
{
2521
/** @var non-empty-string */
2622
protected string $value;
@@ -60,33 +56,4 @@ public function value(): string
6056
{
6157
return $this->value;
6258
}
63-
64-
/**
65-
* @throws RandomException
66-
* @throws StringTypeException
67-
*/
68-
public static function generate(): static
69-
{
70-
// UUID v4: 16 random bytes, with version and variant bits set as per RFC 4122
71-
$bytes = random_bytes(16);
72-
73-
// Set version to 4 (0100xxxx)
74-
$bytes[6] = chr((ord($bytes[6]) & 0x0F) | 0x40);
75-
76-
// Set variant to RFC 4122 (10xxxxxx)
77-
$bytes[8] = chr((ord($bytes[8]) & 0x3F) | 0x80);
78-
79-
$hex = bin2hex($bytes);
80-
81-
$uuid = sprintf(
82-
'%s-%s-%s-%s-%s',
83-
substr($hex, 0, 8),
84-
substr($hex, 8, 4),
85-
substr($hex, 12, 4),
86-
substr($hex, 16, 4),
87-
substr($hex, 20, 12)
88-
);
89-
90-
return new static($uuid);
91-
}
9259
}

src/String/StringUuidV7.php

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44

55
namespace PhpTypedValues\String;
66

7-
use const STR_PAD_LEFT;
8-
9-
use PhpTypedValues\Code\Contract\GenerateInterface;
107
use PhpTypedValues\Code\Exception\StringTypeException;
118
use PhpTypedValues\Code\String\StrType;
12-
use Random\RandomException;
139

14-
use function ord;
1510
use function preg_match;
1611
use function sprintf;
1712
use function strtolower;
@@ -21,7 +16,7 @@
2116
*
2217
* @psalm-immutable
2318
*/
24-
readonly class StringUuidV7 extends StrType implements GenerateInterface
19+
readonly class StringUuidV7 extends StrType
2520
{
2621
/** @var non-empty-string */
2722
protected string $value;
@@ -61,53 +56,4 @@ public function value(): string
6156
{
6257
return $this->value;
6358
}
64-
65-
/**
66-
* @throws RandomException
67-
* @throws StringTypeException
68-
*/
69-
public static function generate(): static
70-
{
71-
// Get current Unix time in milliseconds (48 bits)
72-
// Use a float literal for the multiplier to satisfy Psalm strict operands (float*float)
73-
$timeMs = (int) floor(microtime(true) * 1000.0);
74-
75-
// Split timestamp into 48 bits: high 32 bits and low 16 bits
76-
$timeHigh = ($timeMs & 0xFFFFFFFF0000) >> 16; // upper 32 bits of 48-bit timestamp
77-
$timeLow = $timeMs & 0xFFFF; // lower 16 bits
78-
79-
// Generate 10 random bytes for the remaining fields
80-
$random = random_bytes(10);
81-
82-
// Build the UUID fields (all integers) according to UUID v7 layout
83-
// time_high (32 bits)
84-
$timeHighHex = str_pad(dechex($timeHigh), 8, '0', STR_PAD_LEFT);
85-
86-
// time_mid (16 bits)
87-
$timeMidHex = str_pad(dechex($timeLow), 4, '0', STR_PAD_LEFT);
88-
89-
// time_hi_and_version (16 bits): high 4 bits = version 7 (0b0111)
90-
$timeHiAndVersion = (ord($random[0]) & 0x0F) | 0x70; // clear high 4 bits, set to 0111
91-
$timeHiAndVersionHex = sprintf('%02x%02x', $timeHiAndVersion, ord($random[1]));
92-
93-
// clock_seq_hi_and_reserved (8 bits): set variant to 0b10xx
94-
$clockSeqHi = (ord($random[2]) & 0x3F) | 0x80; // clear top 2 bits, set to 10
95-
$clockSeqLow = ord($random[3]);
96-
$clockSeqHex = sprintf('%02x%02x', $clockSeqHi, $clockSeqLow);
97-
98-
// node (48 bits) = remaining 6 random bytes
99-
$nodeHex = bin2hex(substr($random, 4, 6));
100-
101-
// Assemble canonical UUID string: 8-4-4-4-12
102-
$uuid = sprintf(
103-
'%s-%s-%s-%s-%s',
104-
$timeHighHex,
105-
$timeMidHex,
106-
$timeHiAndVersionHex,
107-
$clockSeqHex,
108-
$nodeHex
109-
);
110-
111-
return new static($uuid);
112-
}
11359
}

src/psalmTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
use PhpTypedValues\String\Alias\NonEmptyStr;
2626
use PhpTypedValues\String\StringNonEmpty;
2727
use PhpTypedValues\String\StringStandard;
28-
use PhpTypedValues\String\StringUuidV4;
29-
use PhpTypedValues\String\StringUuidV7;
3028

3129
/**
3230
* Integer.
@@ -49,8 +47,6 @@
4947

5048
echo StringStandard::fromString('hi')->toString() . \PHP_EOL;
5149
echo NonEmptyStr::fromString('hi')->toString() . \PHP_EOL;
52-
echo StringUuidV7::generate()->toString() . \PHP_EOL;
53-
echo StringUuidV4::generate()->toString() . \PHP_EOL;
5450

5551
/**
5652
* Float.

tests/Unit/String/StringUuidV4Test.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,3 @@
4343
expect(fn() => StringUuidV4::fromString($badChar))
4444
->toThrow(StringTypeException::class, 'Expected UUID v4 (xxxxxxxx-xxxx-4xxx-[89ab]xxx-xxxxxxxxxxxx), got "' . $badChar . '"');
4545
});
46-
47-
it('generate produces a valid lowercase UUID v4 and different values across calls', function (): void {
48-
$a = StringUuidV4::generate();
49-
$b = StringUuidV4::generate();
50-
51-
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
52-
53-
expect($a->toString())->toMatch($regex)
54-
->and($a->value())->toBe($a->toString())
55-
->and($a->toString())->toBe(strtolower($a->toString()))
56-
// Two subsequently generated UUIDs should not be equal with overwhelmingly high probability
57-
->and($a->toString())->not->toBe($b->toString())
58-
->and($b->toString())->toMatch($regex)
59-
->and($b->toString())->toBe(strtolower($b->toString()));
60-
});

tests/Unit/String/StringUuidV7Test.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,3 @@
4545
expect(fn() => StringUuidV7::fromString($badChar))
4646
->toThrow(StringTypeException::class, 'Expected UUID v7 (xxxxxxxx-xxxx-7xxx-[89ab]xxx-xxxxxxxxxxxx), got "' . $badChar . '"');
4747
});
48-
49-
it('generate produces a valid lowercase UUID v7 and different values across calls', function (): void {
50-
$a = StringUuidV7::generate();
51-
$b = StringUuidV7::generate();
52-
53-
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
54-
55-
expect($a->toString())->toMatch($regex)
56-
->and($a->value())->toBe($a->toString())
57-
->and($a->toString())->toBe(strtolower($a->toString()))
58-
->and($b->toString())->toMatch($regex)
59-
->and($b->toString())->toBe(strtolower($b->toString()))
60-
// Two generated UUIDs should almost certainly differ
61-
->and($a->toString())->not->toBe($b->toString());
62-
});
63-
64-
it('generate produces time-ordered UUID v7 (lexicographic order increases over time)', function (): void {
65-
$first = StringUuidV7::generate();
66-
// Sleep a little to ensure the next millisecond is different on most systems
67-
usleep(2000); // 2 ms
68-
$second = StringUuidV7::generate();
69-
70-
// For UUID v7, the canonical string representation is time-ordered; later values should compare greater
71-
expect(strcmp($first->toString(), $second->toString()))->toBeLessThan(0);
72-
});

0 commit comments

Comments
 (0)