From bc29cd4b32219e3795a205f7570b9b2af3fc5935 Mon Sep 17 00:00:00 2001 From: Marcel Hernandez Date: Sun, 24 Sep 2023 20:01:13 +0200 Subject: [PATCH] private class constants readonly classes and properties typed properties --- src/CombGenerator.php | 59 ++++++++++++++----------------- src/SequentialGenerator.php | 13 ++----- src/Uuid.php | 10 +++--- src/Version1Generator.php | 15 ++++---- src/Version4Generator.php | 2 +- src/Version5Generator.php | 15 ++++---- tests/CombGeneratorTest.php | 2 +- tests/SequentialGeneratorTest.php | 13 ++----- tests/UuidGeneratorTest.php | 2 +- tests/UuidTest.php | 4 +-- tests/Version1GeneratorTest.php | 2 +- tests/Version5GeneratorTest.php | 2 +- 12 files changed, 54 insertions(+), 85 deletions(-) diff --git a/src/CombGenerator.php b/src/CombGenerator.php index bc59693..66845ed 100644 --- a/src/CombGenerator.php +++ b/src/CombGenerator.php @@ -7,17 +7,10 @@ /** * @see http://www.informit.com/articles/article.aspx?p=25862 */ -class CombGenerator implements UuidGenerator +final readonly class CombGenerator implements UuidGenerator { - /** - * @var int - */ - private $exponent; - - /** - * @var Version4Generator - */ - private $v4; + private int $exponent; + private Version4Generator $v4; /** * @param int $granularity Precision of the timestamps, ranging from second up to microsecond. @@ -39,7 +32,7 @@ public function __construct(int $granularity = 6) */ public function generate(string $name = null): Uuid { - $head = $this->procrust($this->timestamp()); + $head = self::procrust($this->timestamp()); $tail = \substr($this->v4->generate()->asBytes(), -10); return Uuid::fromBytes($head . $tail); @@ -61,28 +54,6 @@ public function getOverflowDate(): \DateTimeImmutable return new \DateTimeImmutable("@$maxTimestamp UTC"); } - /** - * Returns $timestamp "procrusted" to 6 bytes. - * - * If the timestamp is smaller than 6 bytes, leading 0 bits are appended. - * If the timestamp is larger than 6 bytes, its least significant bits are chopped off. - * - * The returned string is raw binary (each character encodes 8 bits) - * and has always the same size -- 6 bytes. - * - * @example '59b7d71f' => 0x000059b7d71f - * @example '3812e6738' => 0x0003812e6738 - * @example '230bd00838' => 0x00230bd00838 - * @example '15e76205236' => 0x015e76205236 - * @example 'db09d433621' => 0x0db09d433621 - * @example '88e624a01d4c' => 0x88e624a01d4c - * @example '558fd6e4124fb' => 0x558fd6e4124f - */ - private function procrust(string $timestamp): string - { - return \pack('H12', \str_pad(\substr($timestamp, 0, 12), 12, '0', STR_PAD_LEFT)); - } - /** * Returns the current unix timestamp as a hex-encoded string (that is, each character * encodes 4 bits) with variable precision, ranging from second to microsecond. @@ -103,4 +74,26 @@ private function timestamp(): string { return \dechex((int)(\microtime(true) * $this->exponent)); } + + /** + * Returns $timestamp "procrusted" to 6 bytes. + * + * If the timestamp is smaller than 6 bytes, leading 0 bits are appended. + * If the timestamp is larger than 6 bytes, its least significant bits are chopped off. + * + * The returned string is raw binary (each character encodes 8 bits) + * and has always the same size -- 6 bytes. + * + * @example '59b7d71f' => 0x000059b7d71f + * @example '3812e6738' => 0x0003812e6738 + * @example '230bd00838' => 0x00230bd00838 + * @example '15e76205236' => 0x015e76205236 + * @example 'db09d433621' => 0x0db09d433621 + * @example '88e624a01d4c' => 0x88e624a01d4c + * @example '558fd6e4124fb' => 0x558fd6e4124f + */ + private static function procrust(string $timestamp): string + { + return \pack('H12', \str_pad(\substr($timestamp, 0, 12), 12, '0', STR_PAD_LEFT)); + } } diff --git a/src/SequentialGenerator.php b/src/SequentialGenerator.php index 551d597..b7d65b2 100644 --- a/src/SequentialGenerator.php +++ b/src/SequentialGenerator.php @@ -14,17 +14,10 @@ * * Don't use this one in production. */ -class SequentialGenerator implements UuidGenerator +final class SequentialGenerator implements UuidGenerator { - /** - * @var int - */ - private $counter; - - /** - * @var string - */ - private $head; + private int $counter; + private readonly string $head; /** * @example $mark = 15 and $start = 10 will generate: diff --git a/src/Uuid.php b/src/Uuid.php index e965e39..063c256 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -7,28 +7,26 @@ /** * Value object that encapsulates the 128 bits of an UUID. */ -class Uuid +final readonly class Uuid { /** * The 'Nil' UUID described in section 4.1.7 of RFC 4122. */ - const NIL = '00000000-0000-0000-0000-000000000000'; + private const NIL = '00000000-0000-0000-0000-000000000000'; /** * The regular expression of what the value object considers to be a valid UUID in textual form. * * It does not try to enforce any particular version. */ - const TEXTUAL_FORMAT = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i'; + private const TEXTUAL_FORMAT = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i'; /** * Textual representation of the UUID byte sequence. * * @example '96aaab69-7b76-4461-b008-cbb9cfcb6fdf' - * - * @var string */ - private $uuid; + private string $uuid; /** * @throws \InvalidArgumentException If $text is not a valid Uuid in string format. diff --git a/src/Version1Generator.php b/src/Version1Generator.php index 36cd7e1..38760cc 100644 --- a/src/Version1Generator.php +++ b/src/Version1Generator.php @@ -10,7 +10,7 @@ * * @see https://tools.ietf.org/html/rfc4122#section-4.2 */ -class Version1Generator implements UuidGenerator +final readonly class Version1Generator implements UuidGenerator { /** * This is the number of 100-nanosecond intervals elapsed from @@ -23,19 +23,16 @@ class Version1Generator implements UuidGenerator * var_dump($g->getTimestamp()); * var_dump($g->getTimestamp() * -10000000); */ - const GREGORIAN_OFFSET = 122192928000000000; + private const GREGORIAN_OFFSET = 122192928000000000; /** * Regular expression for matching MAC addresses. * * @example 01:23:45:67:89:ab */ - const MAC_ADDR_FORMAT = '/^[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}$/i'; + private const MAC_ADDR_FORMAT = '/^[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}$/i'; - /** - * @var string - */ - private $nodeID; + private string $nodeID; /** * @param string $nodeID A valid MAC address. @@ -55,7 +52,7 @@ public function __construct(string $nodeID) public function generate(string $name = null): Uuid { - $t = $this->timestamp(); + $t = self::timestamp(); $bytes = \pack( 'NnnnH12', @@ -81,7 +78,7 @@ public function generate(string $name = null): Uuid * $maxT = (int)((PHP_INT_MAX - self::GREGORIAN_OFFSET)/10000000); * var_dump(new \DateTimeImmutable("@$maxT UTC")); */ - private function timestamp(): int + private static function timestamp(): int { return self::GREGORIAN_OFFSET + (int)(10000000 * \microtime(true)); } diff --git a/src/Version4Generator.php b/src/Version4Generator.php index dd67bea..42ab6b9 100644 --- a/src/Version4Generator.php +++ b/src/Version4Generator.php @@ -10,7 +10,7 @@ * * @see https://tools.ietf.org/html/rfc4122#section-4.4 */ -class Version4Generator implements UuidGenerator +final readonly class Version4Generator implements UuidGenerator { /** * @throws \Exception When PHP cannot gather enough entropy to diff --git a/src/Version5Generator.php b/src/Version5Generator.php index d8a83ff..d85e7bf 100644 --- a/src/Version5Generator.php +++ b/src/Version5Generator.php @@ -10,21 +10,18 @@ * * @see https://tools.ietf.org/html/rfc4122#section-4.3 */ -class Version5Generator implements UuidGenerator +final readonly class Version5Generator implements UuidGenerator { /** * These are a few well known Uuids listed in Appendix C * of RFC 4122 to be used as namespace identifiers. */ - const NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; - const NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; - const NS_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; - const NS_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; + public const NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; + public const NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; + public const NS_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; + public const NS_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; - /** - * @var string - */ - private $nsBytes; + private string $nsBytes; public function __construct(Uuid $namespace) { diff --git a/tests/CombGeneratorTest.php b/tests/CombGeneratorTest.php index 245fee7..f62c025 100644 --- a/tests/CombGeneratorTest.php +++ b/tests/CombGeneratorTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase; use UMA\Uuid\CombGenerator; -class CombGeneratorTest extends TestCase +final class CombGeneratorTest extends TestCase { public function testInvalidGranularity(): void { diff --git a/tests/SequentialGeneratorTest.php b/tests/SequentialGeneratorTest.php index 576a440..e3bd847 100644 --- a/tests/SequentialGeneratorTest.php +++ b/tests/SequentialGeneratorTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase; use UMA\Uuid\SequentialGenerator; -class SequentialGeneratorTest extends TestCase +final class SequentialGeneratorTest extends TestCase { public function testVanillaUsage(): void { @@ -43,17 +43,8 @@ public function testCustomMarksAndStarts(): void self::assertSame('00000000-0000-000a-0000-00000000000d', (string) $sut->generate()); } - public function testOverflows(): void + public function testOverflow(): void { - $sut = new SequentialGenerator(0, PHP_INT_MAX); - - // After reaching PHP_INT_MAX the internal counter does not overflow. - // Instead, it converts to a floating point number and the ticking stops. - self::assertSame('00000000-0000-0000-7fff-ffffffffffff', (string) $sut->generate()); - self::assertSame('00000000-0000-0000-8000-000000000000', (string) $sut->generate()); - self::assertSame('00000000-0000-0000-8000-000000000000', (string) $sut->generate()); - self::assertSame('00000000-0000-0000-8000-000000000000', (string) $sut->generate()); - $sut = new SequentialGenerator(0, -2); self::assertSame('00000000-0000-0000-ffff-fffffffffffe', (string) $sut->generate()); diff --git a/tests/UuidGeneratorTest.php b/tests/UuidGeneratorTest.php index a0e7484..e8cd545 100644 --- a/tests/UuidGeneratorTest.php +++ b/tests/UuidGeneratorTest.php @@ -9,7 +9,7 @@ use UMA\Uuid\Version1Generator; use UMA\Uuid\Version4Generator; -class UuidGeneratorTest extends TestCase +final class UuidGeneratorTest extends TestCase { const ITERATIONS = 10000; diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 1ddf616..eadf44e 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -8,11 +8,11 @@ use UMA\Uuid\Uuid; use UMA\Uuid\Version4Generator; -class UuidTest extends TestCase +final class UuidTest extends TestCase { public function testNilFactory(): void { - self::assertSame(Uuid::NIL, Uuid::nil()->asString()); + self::assertSame('00000000-0000-0000-0000-000000000000', Uuid::nil()->asString()); } public function testAlias(): void diff --git a/tests/Version1GeneratorTest.php b/tests/Version1GeneratorTest.php index 879b6ed..6faeb77 100644 --- a/tests/Version1GeneratorTest.php +++ b/tests/Version1GeneratorTest.php @@ -8,7 +8,7 @@ use UMA\Uuid\Uuid; use UMA\Uuid\Version1Generator; -class Version1GeneratorTest extends TestCase +final class Version1GeneratorTest extends TestCase { public function testValidMacAddress(): void { diff --git a/tests/Version5GeneratorTest.php b/tests/Version5GeneratorTest.php index 29663d9..b39911f 100644 --- a/tests/Version5GeneratorTest.php +++ b/tests/Version5GeneratorTest.php @@ -8,7 +8,7 @@ use UMA\Uuid\Uuid; use UMA\Uuid\Version5Generator; -class Version5GeneratorTest extends TestCase +final class Version5GeneratorTest extends TestCase { public function testIt(): void {