From 04c64d6f4570f488818bdf330c118408dc7d82da Mon Sep 17 00:00:00 2001 From: Thibaud Fabre Date: Sat, 8 Nov 2014 15:41:53 +0100 Subject: [PATCH] Refactor duplicate code --- src/UuidFactory.php | 74 +++++++++++++++++++++++++-------------------- tests/UuidTest.php | 22 ++++++++------ 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/UuidFactory.php b/src/UuidFactory.php index b1ab8b4..a66617f 100644 --- a/src/UuidFactory.php +++ b/src/UuidFactory.php @@ -159,20 +159,7 @@ class UuidFactory */ public function uuid1($node = null, $clockSeq = null) { - if ($node === null) { - $node = $this->nodeProvider->getNode(); - } - - // Convert the node to hex, if it is still an integer - if (is_int($node)) { - $node = sprintf('%012x', $node); - } - - if (! ctype_xdigit($node) || strlen($node) > 12) { - throw new \InvalidArgumentException('Invalid node value'); - } - - $node = strtolower(sprintf('%012s', $node)); + $node = $this->getValidNode($node); if ($clockSeq === null) { // Not using "stable storage"; see RFC 4122, Section 4.2.1.1 @@ -184,15 +171,8 @@ class UuidFactory $timeOfDay = $this->timeProvider->currentTime(); $uuidTime = $this->timeConverter->calculateTime($timeOfDay['sec'], $timeOfDay['usec']); - // Set the version number to 1 - $timeHi = hexdec($uuidTime['hi']) & 0x0fff; - $timeHi &= ~(0xf000); - $timeHi |= 1 << 12; - - // Set the variant to RFC 4122 - $clockSeqHi = ($clockSeq >> 8) & 0x3f; - $clockSeqHi &= ~(0xc0); - $clockSeqHi |= 0x80; + $timeHi = $this->applyVersion($uuidTime['hi'], 1); + $clockSeqHi = $this->applyVariant($clockSeq >> 8); $fields = array( 'time_low' => $uuidTime['low'], @@ -255,6 +235,25 @@ class UuidFactory return $this->uuidBuilder->build($this->codec, $fields); } + protected function applyVariant($clockSeqHi) + { + // Set the variant to RFC 4122 + $clockSeqHi = $clockSeqHi & 0x3f; + $clockSeqHi &= ~(0xc0); + $clockSeqHi |= 0x80; + + return $clockSeqHi; + } + + protected function applyVersion($timeHi, $version) + { + $timeHi = hexdec($timeHi) & 0x0fff; + $timeHi &= ~(0xf000); + $timeHi |= $version << 12; + + return $timeHi; + } + protected function uuidFromNsAndName($ns, $name, $version, $hashFunction) { if (!($ns instanceof Uuid)) { @@ -276,15 +275,8 @@ class UuidFactory */ protected function uuidFromHashedName($hash, $version) { - // Set the version number - $timeHi = hexdec(substr($hash, 12, 4)) & 0x0fff; - $timeHi &= ~(0xf000); - $timeHi |= $version << 12; - - // Set the variant to RFC 4122 - $clockSeqHi = hexdec(substr($hash, 16, 2)) & 0x3f; - $clockSeqHi &= ~(0xc0); - $clockSeqHi |= 0x80; + $timeHi = $this->applyVersion(substr($hash, 12, 4), $version); + $clockSeqHi = $this->applyVariant(hexdec(substr($hash, 16, 2))); $fields = array( 'time_low' => substr($hash, 0, 8), @@ -297,4 +289,22 @@ class UuidFactory return $this->uuid($fields); } + + protected function getValidNode($node) + { + if ($node === null) { + $node = $this->nodeProvider->getNode(); + } + + // Convert the node to hex, if it is still an integer + if (is_int($node)) { + $node = sprintf('%012x', $node); + } + + if (! ctype_xdigit($node) || strlen($node) > 12) { + throw new \InvalidArgumentException('Invalid node value'); + } + + return strtolower(sprintf('%012s', $node)); + } } diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 05f5744..2372b2c 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -1142,19 +1142,21 @@ class UuidTest extends TestCase $factory = new UuidFactory(); $smallIntFactory = new UuidFactory(new FeatureSet(false, true)); + $timeOfDay = new FixedTimeProvider(array( + 'sec' => $currentTime, + 'usec' => $usec, + 'minuteswest' => 0, + 'dsttime' => 0, + )); + + $factory->setTimeProvider($timeOfDay); + $smallIntFactory->setTimeProvider($timeOfDay); + while ($currentTime <= $endTime) { foreach (array(0, 50000, 250000, 500000, 750000, 999999) as $usec) { - - $timeOfDay = new FixedTimeProvider(array( - 'sec' => $currentTime, - 'usec' => $usec, - 'minuteswest' => 0, - 'dsttime' => 0, - )); - - $factory->setTimeProvider($timeOfDay); - $smallIntFactory->setTimeProvider($timeOfDay); + $timeOfDay->setSec($currentTime); + $timeOfDay->setUsec($usec); $uuid32 = $smallIntFactory->uuid1(0x00007ffffffe, 0x1669); $uuid64 = $factory->uuid1(0x00007ffffffe, 0x1669);