add typehints and return types

This commit is contained in:
Massimiliano Arione
2019-09-12 15:47:49 +02:00
committed by Ben Ramsey
parent 3a42c259a7
commit 5459113b81
34 changed files with 185 additions and 175 deletions
+3 -3
View File
@@ -14,7 +14,7 @@ class BinaryUtils
* @return int The high field of the clock sequence multiplexed with the variant
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
public static function applyVariant($clockSeqHi)
public static function applyVariant($clockSeqHi): int
{
// Set the variant to RFC 4122
$clockSeqHi = $clockSeqHi & 0x3f;
@@ -27,11 +27,11 @@ class BinaryUtils
* Applies the RFC 4122 version number to the `time_hi_and_version` field
*
* @param string $timeHi
* @param integer $version
* @param int $version
* @return int The high field of the timestamp multiplexed with the version number
* @link http://tools.ietf.org/html/rfc4122#section-4.1.3
*/
public static function applyVersion($timeHi, $version)
public static function applyVersion(string $timeHi, int $version): int
{
$timeHi = hexdec($timeHi) & 0x0fff;
$timeHi |= $version << 12;
+2 -1
View File
@@ -18,6 +18,7 @@ use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* DefaultUuidBuilder is the default UUID builder for ramsey/uuid; it builds
@@ -57,7 +58,7 @@ class DefaultUuidBuilder implements UuidBuilderInterface
* see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure.
* @return Uuid
*/
public function build(CodecInterface $codec, array $fields)
public function build(CodecInterface $codec, array $fields): UuidInterface
{
return new Uuid($fields, $this->numberConverter, $codec, $this->timeConverter);
}
+2 -1
View File
@@ -18,6 +18,7 @@ use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\DegradedUuid;
use Ramsey\Uuid\UuidInterface;
/**
* DegradedUuidBuilder builds instances of DegradedUuid
@@ -56,7 +57,7 @@ class DegradedUuidBuilder implements UuidBuilderInterface
* see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure.
* @return DegradedUuid
*/
public function build(CodecInterface $codec, array $fields)
public function build(CodecInterface $codec, array $fields): UuidInterface
{
return new DegradedUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
}
+1 -1
View File
@@ -30,5 +30,5 @@ interface UuidBuilderInterface
* see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure.
* @return UuidInterface
*/
public function build(CodecInterface $codec, array $fields);
public function build(CodecInterface $codec, array $fields): UuidInterface;
}
+4 -4
View File
@@ -29,7 +29,7 @@ interface CodecInterface
* @param UuidInterface $uuid
* @return string Hexadecimal string representation of a UUID
*/
public function encode(UuidInterface $uuid);
public function encode(UuidInterface $uuid): string;
/**
* Encodes a UuidInterface as a binary representation of a UUID
@@ -37,7 +37,7 @@ interface CodecInterface
* @param UuidInterface $uuid
* @return string Binary string representation of a UUID
*/
public function encodeBinary(UuidInterface $uuid);
public function encodeBinary(UuidInterface $uuid): string;
/**
* Decodes a string representation of a UUID into a UuidInterface object instance
@@ -46,7 +46,7 @@ interface CodecInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function decode($encodedUuid);
public function decode(string $encodedUuid): UuidInterface;
/**
* Decodes a binary representation of a UUID into a UuidInterface object instance
@@ -56,5 +56,5 @@ interface CodecInterface
* @throws InvalidUuidStringException
* @throws InvalidArgumentException if string has not 16 characters
*/
public function decodeBytes($bytes);
public function decodeBytes(string $bytes): UuidInterface;
}
+4 -4
View File
@@ -30,7 +30,7 @@ class GuidStringCodec extends StringCodec
* @param UuidInterface $uuid
* @return string Hexadecimal string representation of a GUID
*/
public function encode(UuidInterface $uuid)
public function encode(UuidInterface $uuid): string
{
$components = array_values($uuid->getFieldsHex());
@@ -49,7 +49,7 @@ class GuidStringCodec extends StringCodec
* @param UuidInterface $uuid
* @return string Binary string representation of a GUID
*/
public function encodeBinary(UuidInterface $uuid)
public function encodeBinary(UuidInterface $uuid): string
{
$components = array_values($uuid->getFieldsHex());
@@ -63,7 +63,7 @@ class GuidStringCodec extends StringCodec
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function decode($encodedUuid)
public function decode(string $encodedUuid): UuidInterface
{
$components = $this->extractComponents($encodedUuid);
@@ -79,7 +79,7 @@ class GuidStringCodec extends StringCodec
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function decodeBytes($bytes)
public function decodeBytes(string $bytes): UuidInterface
{
// Specifically call parent::decode to preserve correct byte order
return parent::decode(bin2hex($bytes));
+2 -2
View File
@@ -29,7 +29,7 @@ class OrderedTimeCodec extends StringCodec
* @param UuidInterface $uuid
* @return string Binary string representation of a UUID
*/
public function encodeBinary(UuidInterface $uuid)
public function encodeBinary(UuidInterface $uuid): string
{
$fields = $uuid->getFieldsHex();
@@ -52,7 +52,7 @@ class OrderedTimeCodec extends StringCodec
* @return UuidInterface
* @throws InvalidArgumentException if string has not 16 characters
*/
public function decodeBytes($bytes)
public function decodeBytes(string $bytes): UuidInterface
{
if (strlen($bytes) !== 16) {
throw new InvalidArgumentException('$bytes string should contain 16 characters.');
+4 -4
View File
@@ -48,7 +48,7 @@ class StringCodec implements CodecInterface
* @param UuidInterface $uuid
* @return string Hexadecimal string representation of a UUID
*/
public function encode(UuidInterface $uuid)
public function encode(UuidInterface $uuid): string
{
$fields = array_values($uuid->getFieldsHex());
@@ -64,7 +64,7 @@ class StringCodec implements CodecInterface
* @param UuidInterface $uuid
* @return string Binary string representation of a UUID
*/
public function encodeBinary(UuidInterface $uuid)
public function encodeBinary(UuidInterface $uuid): string
{
return (string) hex2bin($uuid->getHex());
}
@@ -76,7 +76,7 @@ class StringCodec implements CodecInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function decode($encodedUuid)
public function decode(string $encodedUuid): UuidInterface
{
$components = $this->extractComponents($encodedUuid);
$fields = $this->getFields($components);
@@ -91,7 +91,7 @@ class StringCodec implements CodecInterface
* @return UuidInterface
* @throws InvalidArgumentException if string has not 16 characters
*/
public function decodeBytes($bytes)
public function decodeBytes(string $bytes): UuidInterface
{
if (strlen($bytes) !== 16) {
throw new InvalidArgumentException('$bytes string should contain 16 characters.');
+4 -4
View File
@@ -29,7 +29,7 @@ class TimestampFirstCombCodec extends StringCodec
*
* @return string Hexadecimal string representation of a GUID
*/
public function encode(UuidInterface $uuid)
public function encode(UuidInterface $uuid): string
{
$sixPieceComponents = array_values($uuid->getFieldsHex());
@@ -48,7 +48,7 @@ class TimestampFirstCombCodec extends StringCodec
*
* @return string Binary string representation of timestamp first COMB UUID
*/
public function encodeBinary(UuidInterface $uuid)
public function encodeBinary(UuidInterface $uuid): string
{
$stringEncoding = $this->encode($uuid);
@@ -63,7 +63,7 @@ class TimestampFirstCombCodec extends StringCodec
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function decode($encodedUuid)
public function decode(string $encodedUuid): UuidInterface
{
$fivePieceComponents = $this->extractComponents($encodedUuid);
@@ -80,7 +80,7 @@ class TimestampFirstCombCodec extends StringCodec
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function decodeBytes($bytes)
public function decodeBytes(string $bytes): UuidInterface
{
return $this->decode(bin2hex($bytes));
}
+6 -5
View File
@@ -15,6 +15,7 @@
namespace Ramsey\Uuid;
use DateTimeImmutable;
use DateTimeInterface;
use Moontoast\Math\BigNumber;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
@@ -29,7 +30,7 @@ class DegradedUuid extends Uuid
/**
* @inheritdoc
*/
public function getDateTime()
public function getDateTime(): DateTimeInterface
{
if ($this->getVersion() != 1) {
throw new UnsupportedOperationException('Not a time-based UUID');
@@ -48,7 +49,7 @@ class DegradedUuid extends Uuid
* @return array
* @throws UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getFields()
public function getFields(): array
{
throw new UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since some '
@@ -64,7 +65,7 @@ class DegradedUuid extends Uuid
* @return int
* @throws UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getNode()
public function getNode(): int
{
throw new UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since node '
@@ -81,7 +82,7 @@ class DegradedUuid extends Uuid
* @return int
* @throws UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getTimeLow()
public function getTimeLow(): int
{
throw new UnsatisfiedDependencyException(
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since time_low '
@@ -99,7 +100,7 @@ class DegradedUuid extends Uuid
* @throws UnsatisfiedDependencyException if called on a 32-bit system
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
*/
public function getTimestamp()
public function getTimestamp(): int
{
if ($this->getVersion() != 1) {
throw new UnsupportedOperationException('Not a time-based UUID');
+18 -18
View File
@@ -160,7 +160,7 @@ class FeatureSet
*
* @return UuidBuilderInterface
*/
public function getBuilder()
public function getBuilder(): UuidBuilderInterface
{
return $this->builder;
}
@@ -170,7 +170,7 @@ class FeatureSet
*
* @return CodecInterface
*/
public function getCodec()
public function getCodec(): CodecInterface
{
return $this->codec;
}
@@ -180,7 +180,7 @@ class FeatureSet
*
* @return NodeProviderInterface
*/
public function getNodeProvider()
public function getNodeProvider(): NodeProviderInterface
{
return $this->nodeProvider;
}
@@ -190,7 +190,7 @@ class FeatureSet
*
* @return NumberConverterInterface
*/
public function getNumberConverter()
public function getNumberConverter(): NumberConverterInterface
{
return $this->numberConverter;
}
@@ -200,7 +200,7 @@ class FeatureSet
*
* @return RandomGeneratorInterface
*/
public function getRandomGenerator()
public function getRandomGenerator(): RandomGeneratorInterface
{
return $this->randomGenerator;
}
@@ -210,7 +210,7 @@ class FeatureSet
*
* @return TimeGeneratorInterface
*/
public function getTimeGenerator()
public function getTimeGenerator(): TimeGeneratorInterface
{
return $this->timeGenerator;
}
@@ -220,7 +220,7 @@ class FeatureSet
*
* @return ValidatorInterface
*/
public function getValidator()
public function getValidator(): ValidatorInterface
{
return $this->validator;
}
@@ -254,7 +254,7 @@ class FeatureSet
* @param bool $useGuids Whether to build UUIDs using the `GuidStringCodec`
* @return CodecInterface
*/
protected function buildCodec($useGuids = false)
protected function buildCodec(bool $useGuids = false): CodecInterface
{
if ($useGuids) {
return new GuidStringCodec($this->builder);
@@ -269,7 +269,7 @@ class FeatureSet
*
* @return NodeProviderInterface
*/
protected function buildNodeProvider()
protected function buildNodeProvider(): NodeProviderInterface
{
if ($this->ignoreSystemNode) {
return new RandomNodeProvider();
@@ -287,7 +287,7 @@ class FeatureSet
*
* @return NumberConverterInterface
*/
protected function buildNumberConverter()
protected function buildNumberConverter(): NumberConverterInterface
{
if ($this->hasGmp()) {
return new GmpConverter();
@@ -306,7 +306,7 @@ class FeatureSet
*
* @return RandomGeneratorInterface
*/
protected function buildRandomGenerator()
protected function buildRandomGenerator(): RandomGeneratorInterface
{
return (new RandomGeneratorFactory())->getGenerator();
}
@@ -318,7 +318,7 @@ class FeatureSet
* @param TimeProviderInterface $timeProvider
* @return TimeGeneratorInterface
*/
protected function buildTimeGenerator(TimeProviderInterface $timeProvider)
protected function buildTimeGenerator(TimeProviderInterface $timeProvider): TimeGeneratorInterface
{
if ($this->enablePecl) {
return new PeclUuidTimeGenerator();
@@ -335,9 +335,9 @@ class FeatureSet
* Determines which time converter to use and returns the configured
* time converter for this environment
*
* @return \Ramsey\Uuid\Converter\TimeConverterInterface
* @return TimeConverterInterface
*/
protected function buildTimeConverter()
protected function buildTimeConverter(): TimeConverterInterface
{
if ($this->is64BitSystem()) {
return new PhpTimeConverter();
@@ -360,7 +360,7 @@ class FeatureSet
*
* @return UuidBuilderInterface
*/
protected function buildUuidBuilder()
protected function buildUuidBuilder(): UuidBuilderInterface
{
if ($this->is64BitSystem()) {
return new DefaultUuidBuilder($this->numberConverter, $this->timeConverter);
@@ -374,7 +374,7 @@ class FeatureSet
*
* @return bool
*/
protected function hasBigNumber()
protected function hasBigNumber(): bool
{
return class_exists('Moontoast\Math\BigNumber') && !$this->disableBigNumber;
}
@@ -384,7 +384,7 @@ class FeatureSet
*
* @return bool
*/
protected function hasGmp()
protected function hasGmp(): bool
{
return extension_loaded('gmp') && !$this->disableGmp;
}
@@ -394,7 +394,7 @@ class FeatureSet
*
* @return bool
*/
protected function is64BitSystem()
protected function is64BitSystem(): bool
{
return PHP_INT_SIZE == 8 && !$this->disable64Bit;
}
+4 -4
View File
@@ -54,13 +54,13 @@ class CombGenerator implements RandomGeneratorInterface
/**
* Generates a string of binary data of the specified length
*
* @param integer $length The number of bytes of random binary data to generate
* @param int $length The number of bytes of random binary data to generate
* @return string A binary string
* @throws UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
* @throws InvalidArgumentException if length is not a positive integer
* @throws Exception
*/
public function generate($length)
public function generate(int $length): string
{
if ($length < self::TIMESTAMP_BYTES || $length < 0) {
throw new InvalidArgumentException('Length must be a positive integer.');
@@ -74,7 +74,7 @@ class CombGenerator implements RandomGeneratorInterface
$lsbTime = str_pad($this->converter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT);
return (string) hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime);
return (string) hex2bin(str_pad(bin2hex((string) $hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime);
}
/**
@@ -82,7 +82,7 @@ class CombGenerator implements RandomGeneratorInterface
*
* @return string
*/
private function timestamp()
private function timestamp(): string
{
$time = explode(' ', microtime(false));
+10 -7
View File
@@ -80,7 +80,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
* @throws InvalidArgumentException
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function generate($node = null, $clockSeq = null)
public function generate($node = null, int $clockSeq = null): string
{
$node = $this->getValidNode($node);
@@ -92,16 +92,19 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
// Create a 60-bit time value as a count of 100-nanosecond intervals
// since 00:00:00.00, 15 October 1582
$timeOfDay = $this->timeProvider->currentTime();
$uuidTime = $this->timeConverter->calculateTime((string) $timeOfDay['sec'], (string) $timeOfDay['usec']);
$uuidTime = $this->timeConverter->calculateTime(
(string) ($timeOfDay['sec'] ?? ''),
(string) ($timeOfDay['usec'] ?? '')
);
$timeHi = BinaryUtils::applyVersion($uuidTime['hi'], 1);
$timeHi = BinaryUtils::applyVersion((string) ($uuidTime['hi'] ?? 0), 1);
$clockSeqHi = BinaryUtils::applyVariant($clockSeq >> 8);
$hex = vsprintf(
'%08s%04s%04s%02s%02s%012s',
[
$uuidTime['low'],
$uuidTime['mid'],
$uuidTime['low'] ?? 0,
$uuidTime['mid'] ?? 0,
sprintf('%04x', $timeHi),
sprintf('%02x', $clockSeqHi),
sprintf('%02x', $clockSeq & 0xff),
@@ -121,7 +124,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
* @throws InvalidArgumentException
* @throws Exception
*/
protected function getValidNode($node)
protected function getValidNode($node): string
{
if ($node === null) {
$node = $this->nodeProvider->getNode();
@@ -132,7 +135,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
$node = sprintf('%012x', $node);
}
if (!ctype_xdigit($node) || strlen($node) > 12) {
if (!ctype_xdigit((string) $node) || strlen((string) $node) > 12) {
throw new InvalidArgumentException('Invalid node value');
}
+2 -2
View File
@@ -25,10 +25,10 @@ class PeclUuidRandomGenerator implements RandomGeneratorInterface
/**
* Generates a string of random binary data of the specified length
*
* @param integer $length The number of bytes of random binary data to generate
* @param int $length The number of bytes of random binary data to generate
* @return string A binary string
*/
public function generate($length)
public function generate(int $length): string
{
$uuid = uuid_create(UUID_TYPE_RANDOM);
+1 -1
View File
@@ -29,7 +29,7 @@ class PeclUuidTimeGenerator implements TimeGeneratorInterface
* @param int $clockSeq Not used in this context
* @return string A binary string
*/
public function generate($node = null, $clockSeq = null)
public function generate($node = null, int $clockSeq = null): string
{
$uuid = uuid_create(UUID_TYPE_TIME);
+2 -2
View File
@@ -28,11 +28,11 @@ class RandomBytesGenerator implements RandomGeneratorInterface
/**
* Generates a string of random binary data of the specified length
*
* @param integer $length The number of bytes of random binary data to generate
* @param int $length The number of bytes of random binary data to generate
* @return string A binary string
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function generate($length)
public function generate(int $length): string
{
return random_bytes($length);
}
+1 -1
View File
@@ -24,7 +24,7 @@ class RandomGeneratorFactory
*
* @return RandomGeneratorInterface
*/
public static function getGenerator()
public static function getGenerator(): RandomGeneratorInterface
{
return new RandomBytesGenerator();
}
+3 -3
View File
@@ -27,11 +27,11 @@ interface RandomGeneratorInterface
/**
* Generates a string of random binary data of the specified length
*
* @param integer $length The number of bytes of random binary data to generate
* @return string A binary string
* @param int $length The number of bytes of random binary data to generate
* @return string|null A binary string
* @throws UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
* @throws InvalidArgumentException
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function generate($length);
public function generate(int $length): ?string;
}
+3 -3
View File
@@ -52,10 +52,10 @@ class RandomLibAdapter implements RandomGeneratorInterface
/**
* Generates a string of random binary data of the specified length
*
* @param integer $length The number of bytes of random binary data to generate
* @return string A binary string
* @param int $length The number of bytes of random binary data to generate
* @return string|null A binary string
*/
public function generate($length)
public function generate(int $length): ?string
{
return $this->generator->generate($length);
}
+1 -1
View File
@@ -61,7 +61,7 @@ class TimeGeneratorFactory
*
* @return TimeGeneratorInterface
*/
public function getGenerator()
public function getGenerator(): TimeGeneratorInterface
{
return new DefaultTimeGenerator(
$this->nodeProvider,
+1 -1
View File
@@ -39,5 +39,5 @@ interface TimeGeneratorInterface
* @throws InvalidArgumentException
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function generate($node = null, $clockSeq = null);
public function generate($node = null, int $clockSeq = null): string;
}
+1 -1
View File
@@ -46,7 +46,7 @@ class FallbackNodeProvider implements NodeProviderInterface
* @return string|null System node ID as a hexadecimal string
* @throws Exception
*/
public function getNode()
public function getNode(): ?string
{
foreach ($this->nodeProviders as $provider) {
if ($node = $provider->getNode()) {
+1 -1
View File
@@ -31,7 +31,7 @@ class RandomNodeProvider implements NodeProviderInterface
* @return string System node ID as a hexadecimal string
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function getNode()
public function getNode(): string
{
$nodeBytes = random_bytes(6);
+2 -2
View File
@@ -25,7 +25,7 @@ class SystemNodeProvider implements NodeProviderInterface
/**
* Returns the system node ID
*
* @return string|false System node ID as a hexadecimal string, or false if it is not found
* @return string|null|false System node ID as a hexadecimal string, or false if it is not found
*/
public function getNode()
{
@@ -66,7 +66,7 @@ class SystemNodeProvider implements NodeProviderInterface
* @codeCoverageIgnore
* @return string
*/
protected function getIfconfig()
protected function getIfconfig(): string
{
if (strpos(strtolower((string) ini_get('disable_functions')), 'passthru') !== false) {
return '';
+1 -1
View File
@@ -25,7 +25,7 @@ interface NodeProviderInterface
/**
* Returns the system node ID
*
* @return string System node ID as a hexadecimal string
* @return string|null|false System node ID as a hexadecimal string
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function getNode();
+3 -3
View File
@@ -51,7 +51,7 @@ class FixedTimeProvider implements TimeProviderInterface
* @param int $value The `usec` value to set
* @return void
*/
public function setUsec($value)
public function setUsec(int $value)
{
$this->fixedTime['usec'] = $value;
}
@@ -62,7 +62,7 @@ class FixedTimeProvider implements TimeProviderInterface
* @param int $value The `sec` value to set
* @return void
*/
public function setSec($value)
public function setSec(int $value)
{
$this->fixedTime['sec'] = $value;
}
@@ -72,7 +72,7 @@ class FixedTimeProvider implements TimeProviderInterface
*
* @return int[] Array containing `sec` and `usec` components of a timestamp
*/
public function currentTime()
public function currentTime(): array
{
return $this->fixedTime;
}
+1 -1
View File
@@ -26,7 +26,7 @@ class SystemTimeProvider implements TimeProviderInterface
*
* @return int[] Array containing `sec` and `usec` components of a timestamp
*/
public function currentTime()
public function currentTime(): array
{
return gettimeofday();
}
+1 -1
View File
@@ -25,5 +25,5 @@ interface TimeProviderInterface
*
* @return int[] Array guaranteed to contain `sec` and `usec` components of a timestamp
*/
public function currentTime();
public function currentTime(): array;
}
+43 -42
View File
@@ -15,6 +15,7 @@
namespace Ramsey\Uuid;
use DateTimeImmutable;
use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Ramsey\Uuid\Codec\CodecInterface;
@@ -206,7 +207,7 @@ class Uuid implements UuidInterface
* @return string
* @link http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
*/
public function __toString()
public function __toString(): string
{
return $this->toString();
}
@@ -218,7 +219,7 @@ class Uuid implements UuidInterface
* @return string
* @link http://php.net/manual/en/class.jsonserializable.php
*/
public function jsonSerialize()
public function jsonSerialize(): string
{
return $this->toString();
}
@@ -230,7 +231,7 @@ class Uuid implements UuidInterface
* @return string
* @link http://php.net/manual/en/class.serializable.php
*/
public function serialize()
public function serialize(): string
{
return $this->toString();
}
@@ -252,7 +253,7 @@ class Uuid implements UuidInterface
$this->fields = $uuid->fields;
}
public function compareTo(UuidInterface $other)
public function compareTo(UuidInterface $other): int
{
if ($this->getMostSignificantBitsHex() < $other->getMostSignificantBitsHex()) {
return -1;
@@ -273,7 +274,7 @@ class Uuid implements UuidInterface
return 0;
}
public function equals($other)
public function equals(?object $other): bool
{
if (!$other instanceof UuidInterface) {
return false;
@@ -282,7 +283,7 @@ class Uuid implements UuidInterface
return $this->compareTo($other) == 0;
}
public function getBytes()
public function getBytes(): string
{
return $this->codec->encodeBinary($this);
}
@@ -293,12 +294,12 @@ class Uuid implements UuidInterface
*
* @return int Unsigned 8-bit integer value of clock_seq_hi_and_reserved
*/
public function getClockSeqHiAndReserved()
public function getClockSeqHiAndReserved(): int
{
return (int) hexdec($this->getClockSeqHiAndReservedHex());
}
public function getClockSeqHiAndReservedHex()
public function getClockSeqHiAndReservedHex(): string
{
return $this->fields['clock_seq_hi_and_reserved'];
}
@@ -308,12 +309,12 @@ class Uuid implements UuidInterface
*
* @return int Unsigned 8-bit integer value of clock_seq_low
*/
public function getClockSeqLow()
public function getClockSeqLow(): int
{
return (int) hexdec($this->getClockSeqLowHex());
}
public function getClockSeqLowHex()
public function getClockSeqLowHex(): string
{
return $this->fields['clock_seq_low'];
}
@@ -334,17 +335,17 @@ class Uuid implements UuidInterface
* @return int Unsigned 14-bit integer value of clock sequence
* @link http://tools.ietf.org/html/rfc4122#section-4.1.5
*/
public function getClockSequence()
public function getClockSequence(): int
{
return ($this->getClockSeqHiAndReserved() & 0x3f) << 8 | $this->getClockSeqLow();
}
public function getClockSequenceHex()
public function getClockSequenceHex(): string
{
return sprintf('%04x', $this->getClockSequence());
}
public function getNumberConverter()
public function getNumberConverter(): NumberConverterInterface
{
return $this->numberConverter;
}
@@ -352,7 +353,7 @@ class Uuid implements UuidInterface
/**
* @inheritdoc
*/
public function getDateTime()
public function getDateTime(): DateTimeInterface
{
if ($this->getVersion() != 1) {
throw new UnsupportedOperationException('Not a time-based UUID');
@@ -381,7 +382,7 @@ class Uuid implements UuidInterface
* @return array The UUID fields represented as integer values
* @link http://tools.ietf.org/html/rfc4122#section-4.1.2
*/
public function getFields()
public function getFields(): array
{
return [
'time_low' => $this->getTimeLow(),
@@ -393,12 +394,12 @@ class Uuid implements UuidInterface
];
}
public function getFieldsHex()
public function getFieldsHex(): array
{
return $this->fields;
}
public function getHex()
public function getHex(): string
{
return str_replace('-', '', $this->toString());
}
@@ -422,7 +423,7 @@ class Uuid implements UuidInterface
return $this->numberConverter->fromHex($this->getLeastSignificantBitsHex());
}
public function getLeastSignificantBitsHex()
public function getLeastSignificantBitsHex(): string
{
return sprintf(
'%02s%02s%012s',
@@ -443,7 +444,7 @@ class Uuid implements UuidInterface
return $this->numberConverter->fromHex($this->getMostSignificantBitsHex());
}
public function getMostSignificantBitsHex()
public function getMostSignificantBitsHex(): string
{
return sprintf(
'%08s%04s%04s',
@@ -477,12 +478,12 @@ class Uuid implements UuidInterface
* @return int Unsigned 48-bit integer value of node
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6
*/
public function getNode()
public function getNode(): int
{
return (int) hexdec($this->getNodeHex());
}
public function getNodeHex()
public function getNodeHex(): string
{
return $this->fields['node'];
}
@@ -493,12 +494,12 @@ class Uuid implements UuidInterface
*
* @return int Unsigned 16-bit integer value of time_hi_and_version
*/
public function getTimeHiAndVersion()
public function getTimeHiAndVersion(): int
{
return (int) hexdec($this->getTimeHiAndVersionHex());
}
public function getTimeHiAndVersionHex()
public function getTimeHiAndVersionHex(): string
{
return $this->fields['time_hi_and_version'];
}
@@ -508,12 +509,12 @@ class Uuid implements UuidInterface
*
* @return int Unsigned 32-bit integer value of time_low
*/
public function getTimeLow()
public function getTimeLow(): int
{
return (int) hexdec($this->getTimeLowHex());
}
public function getTimeLowHex()
public function getTimeLowHex(): string
{
return $this->fields['time_low'];
}
@@ -523,12 +524,12 @@ class Uuid implements UuidInterface
*
* @return int Unsigned 16-bit integer value of time_mid
*/
public function getTimeMid()
public function getTimeMid(): int
{
return (int) hexdec($this->getTimeMidHex());
}
public function getTimeMidHex()
public function getTimeMidHex(): string
{
return $this->fields['time_mid'];
}
@@ -549,7 +550,7 @@ class Uuid implements UuidInterface
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4
*/
public function getTimestamp()
public function getTimestamp(): int
{
if ($this->getVersion() != 1) {
throw new UnsupportedOperationException('Not a time-based UUID');
@@ -561,7 +562,7 @@ class Uuid implements UuidInterface
/**
* @inheritdoc
*/
public function getTimestampHex()
public function getTimestampHex(): string
{
if ($this->getVersion() != 1) {
throw new UnsupportedOperationException('Not a time-based UUID');
@@ -575,12 +576,12 @@ class Uuid implements UuidInterface
);
}
public function getUrn()
public function getUrn(): string
{
return 'urn:uuid:' . $this->toString();
}
public function getVariant()
public function getVariant(): int
{
$clockSeq = $this->getClockSeqHiAndReserved();
@@ -599,7 +600,7 @@ class Uuid implements UuidInterface
return self::RESERVED_FUTURE;
}
public function getVersion()
public function getVersion(): ?int
{
if ($this->getVariant() == self::RFC_4122) {
return (int) (($this->getTimeHiAndVersion() >> 12) & 0x0f);
@@ -608,7 +609,7 @@ class Uuid implements UuidInterface
return null;
}
public function toString()
public function toString(): string
{
return $this->codec->encode($this);
}
@@ -618,7 +619,7 @@ class Uuid implements UuidInterface
*
* @return UuidFactoryInterface
*/
public static function getFactory()
public static function getFactory(): UuidFactoryInterface
{
if (!self::$factory) {
self::$factory = new UuidFactory();
@@ -646,7 +647,7 @@ class Uuid implements UuidInterface
* @throws InvalidUuidStringException
* @throws InvalidArgumentException
*/
public static function fromBytes($bytes)
public static function fromBytes(string $bytes): UuidInterface
{
return self::getFactory()->fromBytes($bytes);
}
@@ -658,7 +659,7 @@ class Uuid implements UuidInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public static function fromString($name)
public static function fromString(string $name): UuidInterface
{
return self::getFactory()->fromString($name);
}
@@ -671,7 +672,7 @@ class Uuid implements UuidInterface
* @throws UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
* @throws InvalidUuidStringException
*/
public static function fromInteger($integer)
public static function fromInteger(string $integer): UuidInterface
{
return self::getFactory()->fromInteger($integer);
}
@@ -682,7 +683,7 @@ class Uuid implements UuidInterface
* @param string $uuid The string UUID to test
* @return boolean
*/
public static function isValid($uuid)
public static function isValid(string $uuid): bool
{
return self::getFactory()->getValidator()->validate($uuid);
}
@@ -701,7 +702,7 @@ class Uuid implements UuidInterface
* @throws InvalidArgumentException
* @throws Exception if it was not possible to gather sufficient entropy
*/
public static function uuid1($node = null, $clockSeq = null)
public static function uuid1($node = null, ?int $clockSeq = null): UuidInterface
{
return self::getFactory()->uuid1($node, $clockSeq);
}
@@ -715,7 +716,7 @@ class Uuid implements UuidInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public static function uuid3($ns, $name)
public static function uuid3($ns, string $name): UuidInterface
{
return self::getFactory()->uuid3($ns, $name);
}
@@ -728,7 +729,7 @@ class Uuid implements UuidInterface
* @throws InvalidArgumentException
* @throws Exception
*/
public static function uuid4()
public static function uuid4(): UuidInterface
{
return self::getFactory()->uuid4();
}
@@ -742,7 +743,7 @@ class Uuid implements UuidInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public static function uuid5($ns, $name)
public static function uuid5($ns, string $name): UuidInterface
{
return self::getFactory()->uuid5($ns, $name);
}
+21 -18
View File
@@ -83,7 +83,7 @@ class UuidFactory implements UuidFactoryInterface
*
* @return CodecInterface
*/
public function getCodec()
public function getCodec(): CodecInterface
{
return $this->codec;
}
@@ -104,7 +104,7 @@ class UuidFactory implements UuidFactoryInterface
*
* @return NodeProviderInterface
*/
public function getNodeProvider()
public function getNodeProvider(): NodeProviderInterface
{
return $this->nodeProvider;
}
@@ -114,7 +114,7 @@ class UuidFactory implements UuidFactoryInterface
*
* @return RandomGeneratorInterface
*/
public function getRandomGenerator()
public function getRandomGenerator(): RandomGeneratorInterface
{
return $this->randomGenerator;
}
@@ -124,7 +124,7 @@ class UuidFactory implements UuidFactoryInterface
*
* @return TimeGeneratorInterface
*/
public function getTimeGenerator()
public function getTimeGenerator(): TimeGeneratorInterface
{
return $this->timeGenerator;
}
@@ -145,7 +145,7 @@ class UuidFactory implements UuidFactoryInterface
*
* @return NumberConverterInterface
*/
public function getNumberConverter()
public function getNumberConverter(): NumberConverterInterface
{
return $this->numberConverter;
}
@@ -177,7 +177,7 @@ class UuidFactory implements UuidFactoryInterface
*
* @return UuidBuilderInterface $builder
*/
public function getUuidBuilder()
public function getUuidBuilder(): UuidBuilderInterface
{
return $this->uuidBuilder;
}
@@ -196,7 +196,7 @@ class UuidFactory implements UuidFactoryInterface
/**
* @return ValidatorInterface
*/
public function getValidator()
public function getValidator(): ValidatorInterface
{
return $this->validator;
}
@@ -210,7 +210,10 @@ class UuidFactory implements UuidFactoryInterface
$this->validator = $validator;
}
public function fromBytes($bytes)
/**
* @inheritdoc
*/
public function fromBytes(string $bytes): UuidInterface
{
return $this->codec->decodeBytes($bytes);
}
@@ -218,7 +221,7 @@ class UuidFactory implements UuidFactoryInterface
/**
* @inheritdoc
*/
public function fromString($uuid)
public function fromString(string $uuid): UuidInterface
{
$uuid = strtolower($uuid);
return $this->codec->decode($uuid);
@@ -227,7 +230,7 @@ class UuidFactory implements UuidFactoryInterface
/**
* @inheritdoc
*/
public function fromInteger($integer)
public function fromInteger($integer): UuidInterface
{
$hex = $this->numberConverter->toHex($integer);
$hex = str_pad($hex, 32, '0', STR_PAD_LEFT);
@@ -238,7 +241,7 @@ class UuidFactory implements UuidFactoryInterface
/**
* @inheritdoc
*/
public function uuid1($node = null, $clockSeq = null)
public function uuid1($node = null, ?int $clockSeq = null): UuidInterface
{
$bytes = $this->timeGenerator->generate($node, $clockSeq);
$hex = bin2hex($bytes);
@@ -249,7 +252,7 @@ class UuidFactory implements UuidFactoryInterface
/**
* @inheritdoc
*/
public function uuid3($ns, $name)
public function uuid3($ns, string $name): UuidInterface
{
return $this->uuidFromNsAndName($ns, $name, 3, 'md5');
}
@@ -257,14 +260,14 @@ class UuidFactory implements UuidFactoryInterface
/**
* @inheritdoc
*/
public function uuid4()
public function uuid4(): UuidInterface
{
$bytes = $this->randomGenerator->generate(16);
// When converting the bytes to hex, it turns into a 32-character
// hexadecimal string that looks a lot like an MD5 hash, so at this
// point, we can just pass it to uuidFromHashedName.
$hex = bin2hex($bytes);
$hex = bin2hex((string) $bytes);
return $this->uuidFromHashedName($hex, 4);
}
@@ -272,7 +275,7 @@ class UuidFactory implements UuidFactoryInterface
/**
* @inheritdoc
*/
public function uuid5($ns, $name)
public function uuid5($ns, string $name): UuidInterface
{
return $this->uuidFromNsAndName($ns, $name, 5, 'sha1');
}
@@ -287,7 +290,7 @@ class UuidFactory implements UuidFactoryInterface
* see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure.
* @return UuidInterface
*/
public function uuid(array $fields)
public function uuid(array $fields): UuidInterface
{
return $this->uuidBuilder->build($this->codec, $fields);
}
@@ -303,7 +306,7 @@ class UuidFactory implements UuidFactoryInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
protected function uuidFromNsAndName($ns, $name, $version, $hashFunction)
protected function uuidFromNsAndName($ns, string $name, int $version, callable $hashFunction): UuidInterface
{
if (!($ns instanceof UuidInterface)) {
$ns = $this->codec->decode($ns);
@@ -322,7 +325,7 @@ class UuidFactory implements UuidFactoryInterface
* @param int $version The UUID version to set for this hash (1, 3, 4, or 5)
* @return UuidInterface
*/
protected function uuidFromHashedName($hash, $version)
protected function uuidFromHashedName(string $hash, int $version): UuidInterface
{
$timeHi = BinaryUtils::applyVersion(substr($hash, 12, 4), $version);
$clockSeqHi = BinaryUtils::applyVariant((int) hexdec(substr($hash, 16, 2)));
+8 -8
View File
@@ -29,7 +29,7 @@ interface UuidFactoryInterface
/**
* @return ValidatorInterface
*/
public function getValidator();
public function getValidator(): ValidatorInterface;
/**
* Generate a version 1 UUID from a host ID, sequence number, and the current time.
@@ -45,7 +45,7 @@ interface UuidFactoryInterface
* @throws InvalidArgumentException
* @throws Exception if it was not possible to gather sufficient entropy
*/
public function uuid1($node = null, $clockSeq = null);
public function uuid1($node = null, ?int $clockSeq = null): UuidInterface;
/**
* Generate a version 3 UUID based on the MD5 hash of a namespace identifier
@@ -56,7 +56,7 @@ interface UuidFactoryInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function uuid3($ns, $name);
public function uuid3($ns, string $name): UuidInterface;
/**
* Generate a version 4 (random) UUID.
@@ -66,7 +66,7 @@ interface UuidFactoryInterface
* @throws InvalidArgumentException
* @throws Exception
*/
public function uuid4();
public function uuid4(): UuidInterface;
/**
* Generate a version 5 UUID based on the SHA-1 hash of a namespace
@@ -77,7 +77,7 @@ interface UuidFactoryInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function uuid5($ns, $name);
public function uuid5($ns, string $name): UuidInterface;
/**
* Creates a UUID from a byte string.
@@ -87,7 +87,7 @@ interface UuidFactoryInterface
* @throws InvalidUuidStringException
* @throws InvalidArgumentException if string has not 16 characters
*/
public function fromBytes($bytes);
public function fromBytes(string $bytes): UuidInterface;
/**
* Creates a UUID from the string standard representation
@@ -96,7 +96,7 @@ interface UuidFactoryInterface
* @return UuidInterface
* @throws InvalidUuidStringException
*/
public function fromString($uuid);
public function fromString(string $uuid): UuidInterface;
/**
* Creates a `Uuid` from an integer representation
@@ -110,5 +110,5 @@ interface UuidFactoryInterface
* @throws UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present
* @throws InvalidUuidStringException
*/
public function fromInteger($integer);
public function fromInteger($integer): UuidInterface;
}
+23 -23
View File
@@ -40,7 +40,7 @@ interface UuidInterface extends JsonSerializable, Serializable
* @param UuidInterface $other UUID to which this UUID is compared
* @return int -1, 0 or 1 as this UUID is less than, equal to, or greater than `$uuid`
*/
public function compareTo(UuidInterface $other);
public function compareTo(UuidInterface $other): int;
/**
* Compares this object to the specified object.
@@ -49,10 +49,10 @@ interface UuidInterface extends JsonSerializable, Serializable
* object, has the same variant, and contains the same value, bit for bit,
* as this UUID.
*
* @param object $other
* @param object|null $other
* @return bool True if `$other` is equal to this UUID
*/
public function equals($other);
public function equals(?object $other): bool;
/**
* Returns the UUID as a 16-byte string (containing the six integer fields
@@ -60,21 +60,21 @@ interface UuidInterface extends JsonSerializable, Serializable
*
* @return string
*/
public function getBytes();
public function getBytes(): string;
/**
* Returns the number converter to use for converting hex values to/from integers.
*
* @return NumberConverterInterface
*/
public function getNumberConverter();
public function getNumberConverter(): NumberConverterInterface;
/**
* Returns the hexadecimal value of the UUID.
*
* @return string
*/
public function getHex();
public function getHex(): string;
/**
* Returns an array of the fields of this UUID, with keys named according
@@ -93,7 +93,7 @@ interface UuidInterface extends JsonSerializable, Serializable
*
* @return array The UUID fields represented as hexadecimal values
*/
public function getFieldsHex();
public function getFieldsHex(): array;
/**
* Returns the high field of the clock sequence multiplexed with the variant
@@ -101,21 +101,21 @@ interface UuidInterface extends JsonSerializable, Serializable
*
* @return string Hexadecimal value of clock_seq_hi_and_reserved
*/
public function getClockSeqHiAndReservedHex();
public function getClockSeqHiAndReservedHex(): string;
/**
* Returns the low field of the clock sequence (bits 73-80 of the UUID).
*
* @return string Hexadecimal value of clock_seq_low
*/
public function getClockSeqLowHex();
public function getClockSeqLowHex(): string;
/**
* Returns the clock sequence value associated with this UUID.
*
* @return string Hexadecimal value of clock sequence
*/
public function getClockSequenceHex();
public function getClockSequenceHex(): string;
/**
* Returns a PHP object that implements `DateTimeInterface` representing
@@ -130,7 +130,7 @@ interface UuidInterface extends JsonSerializable, Serializable
* @throws UnsatisfiedDependencyException if called in a 32-bit system and
* `Moontoast\Math\BigNumber` is not present
*/
public function getDateTime();
public function getDateTime(): DateTimeInterface;
/**
* Returns the integer value of the UUID, converted to an appropriate number
@@ -146,14 +146,14 @@ interface UuidInterface extends JsonSerializable, Serializable
*
* @return string Hexadecimal value of least significant bits
*/
public function getLeastSignificantBitsHex();
public function getLeastSignificantBitsHex(): string;
/**
* Returns the most significant 64 bits of this UUID's 128 bit value.
*
* @return string Hexadecimal value of most significant bits
*/
public function getMostSignificantBitsHex();
public function getMostSignificantBitsHex(): string;
/**
* Returns the node value associated with this UUID
@@ -179,7 +179,7 @@ interface UuidInterface extends JsonSerializable, Serializable
* @return string Hexadecimal value of node
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6
*/
public function getNodeHex();
public function getNodeHex(): string;
/**
* Returns the high field of the timestamp multiplexed with the version
@@ -187,21 +187,21 @@ interface UuidInterface extends JsonSerializable, Serializable
*
* @return string Hexadecimal value of time_hi_and_version
*/
public function getTimeHiAndVersionHex();
public function getTimeHiAndVersionHex(): string;
/**
* Returns the low field of the timestamp (the first 32 bits of the UUID).
*
* @return string Hexadecimal value of time_low
*/
public function getTimeLowHex();
public function getTimeLowHex(): string;
/**
* Returns the middle field of the timestamp (bits 33-48 of the UUID).
*
* @return string Hexadecimal value of time_mid
*/
public function getTimeMidHex();
public function getTimeMidHex(): string;
/**
* Returns the timestamp value associated with this UUID.
@@ -219,7 +219,7 @@ interface UuidInterface extends JsonSerializable, Serializable
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4
*/
public function getTimestampHex();
public function getTimestampHex(): string;
/**
* Returns the string representation of the UUID as a URN.
@@ -227,7 +227,7 @@ interface UuidInterface extends JsonSerializable, Serializable
* @return string
* @link http://en.wikipedia.org/wiki/Uniform_Resource_Name
*/
public function getUrn();
public function getUrn(): string;
/**
* Returns the variant number associated with this UUID.
@@ -243,7 +243,7 @@ interface UuidInterface extends JsonSerializable, Serializable
* @return int
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
public function getVariant();
public function getVariant(): int;
/**
* Returns the version number associated with this UUID.
@@ -263,19 +263,19 @@ interface UuidInterface extends JsonSerializable, Serializable
* @return int|null
* @link http://tools.ietf.org/html/rfc4122#section-4.1.3
*/
public function getVersion();
public function getVersion(): ?int;
/**
* Returns a string representation of this UUID.
*
* @return string
*/
public function toString();
public function toString(): string;
/**
* Allows casting this UUID to a string representation.
*
* @return string
*/
public function __toString();
public function __toString(): string;
}
+1 -1
View File
@@ -33,7 +33,7 @@ class Validator implements ValidatorInterface
* @param string $uuid
* @return bool Returns TRUE if the string was validated as a valid UUID or FALSE on failure
*/
public function validate($uuid)
public function validate($uuid): bool
{
$uuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}'], '', $uuid);
+1 -1
View File
@@ -26,5 +26,5 @@ interface ValidatorInterface
* @param string $uuid
* @return bool Returns TRUE if the string was validated as a valid UUID or FALSE on failure
*/
public function validate($uuid);
public function validate(string $uuid): bool;
}