diff --git a/composer.json b/composer.json
index f034023..73d45ca 100644
--- a/composer.json
+++ b/composer.json
@@ -30,7 +30,7 @@
"phpstan/phpstan-phpunit": "^1.1",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16.1",
- "slevomat/coding-standard": "^7.0",
+ "slevomat/coding-standard": "^8.4",
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^4.22"
},
diff --git a/composer.lock b/composer.lock
index db7ccf0..1bb31e6 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "bd07f2ba512b7c039145c8e5cf5c83b6",
+ "content-hash": "35ada149231c1b6e1f276b92a905cef0",
"packages": [
{
"name": "brick/math",
@@ -2659,16 +2659,16 @@
},
{
"name": "phpstan/phpdoc-parser",
- "version": "1.4.2",
+ "version": "1.7.0",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpdoc-parser.git",
- "reference": "4cb3021a4e10ffe3d5f94a4c34cf4b3f6de2fa3d"
+ "reference": "367a8d9d5f7da2a0136422d27ce8840583926955"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/4cb3021a4e10ffe3d5f94a4c34cf4b3f6de2fa3d",
- "reference": "4cb3021a4e10ffe3d5f94a4c34cf4b3f6de2fa3d",
+ "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/367a8d9d5f7da2a0136422d27ce8840583926955",
+ "reference": "367a8d9d5f7da2a0136422d27ce8840583926955",
"shasum": ""
},
"require": {
@@ -2678,6 +2678,7 @@
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^1.5",
+ "phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^9.5",
"symfony/process": "^5.2"
@@ -2697,9 +2698,9 @@
"description": "PHPDoc parser with support for nullable, intersection and generic types",
"support": {
"issues": "https://github.com/phpstan/phpdoc-parser/issues",
- "source": "https://github.com/phpstan/phpdoc-parser/tree/1.4.2"
+ "source": "https://github.com/phpstan/phpdoc-parser/tree/1.7.0"
},
- "time": "2022-03-30T13:33:37+00:00"
+ "time": "2022-08-09T12:23:23+00:00"
},
{
"name": "phpstan/phpstan",
@@ -4695,37 +4696,37 @@
},
{
"name": "slevomat/coding-standard",
- "version": "7.1",
+ "version": "8.4.0",
"source": {
"type": "git",
"url": "https://github.com/slevomat/coding-standard.git",
- "reference": "b521bd358b5f7a7d69e9637fd139e036d8adeb6f"
+ "reference": "02f27326be19633a1b6ba76745390bbf9a4be0b6"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/b521bd358b5f7a7d69e9637fd139e036d8adeb6f",
- "reference": "b521bd358b5f7a7d69e9637fd139e036d8adeb6f",
+ "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/02f27326be19633a1b6ba76745390bbf9a4be0b6",
+ "reference": "02f27326be19633a1b6ba76745390bbf9a4be0b6",
"shasum": ""
},
"require": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7",
"php": "^7.2 || ^8.0",
- "phpstan/phpdoc-parser": "^1.4.1",
- "squizlabs/php_codesniffer": "^3.6.2"
+ "phpstan/phpdoc-parser": ">=1.7.0 <1.8.0",
+ "squizlabs/php_codesniffer": "^3.7.1"
},
"require-dev": {
- "phing/phing": "2.17.2",
+ "phing/phing": "2.17.4",
"php-parallel-lint/php-parallel-lint": "1.3.2",
- "phpstan/phpstan": "1.4.10|1.5.2",
+ "phpstan/phpstan": "1.4.10|1.8.2",
"phpstan/phpstan-deprecation-rules": "1.0.0",
- "phpstan/phpstan-phpunit": "1.0.0|1.1.0",
- "phpstan/phpstan-strict-rules": "1.1.0",
- "phpunit/phpunit": "7.5.20|8.5.21|9.5.19"
+ "phpstan/phpstan-phpunit": "1.0.0|1.1.1",
+ "phpstan/phpstan-strict-rules": "1.3.0",
+ "phpunit/phpunit": "7.5.20|8.5.21|9.5.21"
},
"type": "phpcodesniffer-standard",
"extra": {
"branch-alias": {
- "dev-master": "7.x-dev"
+ "dev-master": "8.x-dev"
}
},
"autoload": {
@@ -4740,7 +4741,7 @@
"description": "Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.",
"support": {
"issues": "https://github.com/slevomat/coding-standard/issues",
- "source": "https://github.com/slevomat/coding-standard/tree/7.1"
+ "source": "https://github.com/slevomat/coding-standard/tree/8.4.0"
},
"funding": [
{
@@ -4752,20 +4753,20 @@
"type": "tidelift"
}
],
- "time": "2022-03-29T12:44:16+00:00"
+ "time": "2022-08-09T19:03:45+00:00"
},
{
"name": "squizlabs/php_codesniffer",
- "version": "3.6.2",
+ "version": "3.7.1",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
- "reference": "5e4e71592f69da17871dba6e80dd51bce74a351a"
+ "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/5e4e71592f69da17871dba6e80dd51bce74a351a",
- "reference": "5e4e71592f69da17871dba6e80dd51bce74a351a",
+ "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1359e176e9307e906dc3d890bcc9603ff6d90619",
+ "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619",
"shasum": ""
},
"require": {
@@ -4808,7 +4809,7 @@
"source": "https://github.com/squizlabs/PHP_CodeSniffer",
"wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki"
},
- "time": "2021-12-12T21:44:58+00:00"
+ "time": "2022-06-18T07:21:10+00:00"
},
{
"name": "symfony/console",
diff --git a/phpstan.neon.dist b/phpstan.neon.dist
index 0897bfc..acd9320 100644
--- a/phpstan.neon.dist
+++ b/phpstan.neon.dist
@@ -11,5 +11,3 @@ parameters:
message: '#^Comparison operation ">" between 6 and 0 is always true\.$#'
count: 1
path: ./src/Generator/CombGenerator.php
-
- - '#^Method Ramsey\\Uuid\\Generator\\Pecl[A-Za-z]+Generator::generate\(\) should return string but returns string\|false\.$#'
diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 176ef96..87f5227 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -80,11 +80,6 @@
$macs[]
-
-
- Uuid
-
-
$this
@@ -109,6 +104,18 @@
parent::__construct($fields, $numberConverter, $codec, $timeConverter)
+
+
+ UuidV1Factory
+
+
+ format
+ format
+ generate
+ generate
+ getGenerator
+
+
parent::__construct($fields, $numberConverter, $codec, $timeConverter)
diff --git a/src/Builder/FallbackBuilder.php b/src/Builder/FallbackBuilder.php
index 8ab438a..ed9184c 100644
--- a/src/Builder/FallbackBuilder.php
+++ b/src/Builder/FallbackBuilder.php
@@ -30,7 +30,7 @@ class FallbackBuilder implements UuidBuilderInterface
/**
* @var iterable
*/
- private $builders;
+ private iterable $builders;
/**
* @param iterable $builders An array of UUID builders
@@ -45,7 +45,7 @@ class FallbackBuilder implements UuidBuilderInterface
* succeeds
*
* @param CodecInterface $codec The codec to use for building this instance
- * @param string $bytes The byte string from which to construct a UUID
+ * @param non-empty-string $bytes The byte string from which to construct a UUID
*
* @return UuidInterface an instance of a UUID object
*
diff --git a/src/Builder/UuidBuilderInterface.php b/src/Builder/UuidBuilderInterface.php
index 8e58b2b..a6638f1 100644
--- a/src/Builder/UuidBuilderInterface.php
+++ b/src/Builder/UuidBuilderInterface.php
@@ -28,7 +28,7 @@ interface UuidBuilderInterface
* Builds and returns a UuidInterface
*
* @param CodecInterface $codec The codec to use for building this UuidInterface instance
- * @param string $bytes The byte string from which to construct a UUID
+ * @param non-empty-string $bytes The byte string from which to construct a UUID
*
* @return UuidInterface Implementations may choose to return more specific
* instances of UUIDs that implement UuidInterface
diff --git a/src/Codec/CodecInterface.php b/src/Codec/CodecInterface.php
index 85f8a7e..8ef1c63 100644
--- a/src/Codec/CodecInterface.php
+++ b/src/Codec/CodecInterface.php
@@ -29,9 +29,7 @@ interface CodecInterface
* @param UuidInterface $uuid The UUID for which to create a hexadecimal
* string representation
*
- * @return string Hexadecimal string representation of a UUID
- *
- * @psalm-return non-empty-string
+ * @return non-empty-string Hexadecimal string representation of a UUID
*/
public function encode(UuidInterface $uuid): string;
@@ -41,17 +39,15 @@ interface CodecInterface
* @param UuidInterface $uuid The UUID for which to create a binary string
* representation
*
- * @return string Binary string representation of a UUID
- *
- * @psalm-return non-empty-string
+ * @return non-empty-string Binary string representation of a UUID
*/
public function encodeBinary(UuidInterface $uuid): string;
/**
* Returns a UuidInterface derived from a hexadecimal string representation
*
- * @param string $encodedUuid The hexadecimal string representation to
- * convert into a UuidInterface instance
+ * @param non-empty-string $encodedUuid The hexadecimal string
+ * representation to convert into a UuidInterface instance
*
* @return UuidInterface An instance of a UUID decoded from a hexadecimal
* string representation
@@ -61,8 +57,8 @@ interface CodecInterface
/**
* Returns a UuidInterface derived from a binary string representation
*
- * @param string $bytes The binary string representation to convert into a
- * UuidInterface instance
+ * @param non-empty-string $bytes The binary string representation to
+ * convert into a UuidInterface instance
*
* @return UuidInterface An instance of a UUID decoded from a binary string
* representation
diff --git a/src/Codec/GuidStringCodec.php b/src/Codec/GuidStringCodec.php
index f11e9d5..f4c9123 100644
--- a/src/Codec/GuidStringCodec.php
+++ b/src/Codec/GuidStringCodec.php
@@ -38,12 +38,19 @@ class GuidStringCodec extends StringCodec
public function decodeBytes(string $bytes): UuidInterface
{
+ /** @var non-empty-string $hex */
+ $hex = bin2hex($bytes);
+
// Specifically call parent::decode to preserve correct byte order
- return parent::decode(bin2hex($bytes));
+ return parent::decode($hex);
}
/**
* Swaps bytes according to the GUID rules
+ *
+ * @param non-empty-string $bytes
+ *
+ * @return non-empty-string
*/
private function swapBytes(string $bytes): string
{
diff --git a/src/Codec/OrderedTimeCodec.php b/src/Codec/OrderedTimeCodec.php
index 0798ebc..ea6e5bf 100644
--- a/src/Codec/OrderedTimeCodec.php
+++ b/src/Codec/OrderedTimeCodec.php
@@ -50,9 +50,6 @@ class OrderedTimeCodec extends StringCodec
* fields rearranged for optimized storage
*
* @inheritDoc
- * @psalm-return non-empty-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
*/
public function encodeBinary(UuidInterface $uuid): string
{
@@ -67,7 +64,6 @@ class OrderedTimeCodec extends StringCodec
$bytes = $uuid->getFields()->getBytes();
- /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
return $bytes[6] . $bytes[7]
. $bytes[4] . $bytes[5]
. $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]
diff --git a/src/Codec/StringCodec.php b/src/Codec/StringCodec.php
index 2374485..a702d35 100644
--- a/src/Codec/StringCodec.php
+++ b/src/Codec/StringCodec.php
@@ -36,10 +36,7 @@ use function substr;
*/
class StringCodec implements CodecInterface
{
- /**
- * @var UuidBuilderInterface
- */
- private $builder;
+ private UuidBuilderInterface $builder;
/**
* Constructs a StringCodec
@@ -104,6 +101,8 @@ class StringCodec implements CodecInterface
/**
* Returns a byte string of the UUID
+ *
+ * @return non-empty-string
*/
protected function getBytes(string $encodedUuid): string
{
@@ -127,6 +126,7 @@ class StringCodec implements CodecInterface
);
}
+ /** @var non-empty-string */
return (string) hex2bin($parsedUuid);
}
}
diff --git a/src/Codec/TimestampFirstCombCodec.php b/src/Codec/TimestampFirstCombCodec.php
index 0e0042d..81e084f 100644
--- a/src/Codec/TimestampFirstCombCodec.php
+++ b/src/Codec/TimestampFirstCombCodec.php
@@ -50,15 +50,11 @@ use function substr_replace;
*/
class TimestampFirstCombCodec extends StringCodec
{
- /**
- * @psalm-return non-empty-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
- */
public function encode(UuidInterface $uuid): string
{
$bytes = $this->swapBytes($uuid->getFields()->getBytes());
+ /** @var non-empty-string */
return sprintf(
'%08s-%04s-%04s-%04s-%012s',
bin2hex(substr($bytes, 0, 4)),
@@ -69,14 +65,8 @@ class TimestampFirstCombCodec extends StringCodec
);
}
- /**
- * @psalm-return non-empty-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
- */
public function encodeBinary(UuidInterface $uuid): string
{
- /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
return $this->swapBytes($uuid->getFields()->getBytes());
}
@@ -99,6 +89,8 @@ class TimestampFirstCombCodec extends StringCodec
/**
* Swaps bytes according to the timestamp-first COMB rules
+ *
+ * @return non-empty-string
*/
private function swapBytes(string $bytes): string
{
@@ -106,8 +98,8 @@ class TimestampFirstCombCodec extends StringCodec
$last48Bits = substr($bytes, -6);
$bytes = substr_replace($bytes, $last48Bits, 0, 6);
- $bytes = substr_replace($bytes, $first48Bits, -6);
- return $bytes;
+ /** @var non-empty-string */
+ return substr_replace($bytes, $first48Bits, -6);
}
}
diff --git a/src/Converter/Number/GenericNumberConverter.php b/src/Converter/Number/GenericNumberConverter.php
index 501eac0..2c8e471 100644
--- a/src/Converter/Number/GenericNumberConverter.php
+++ b/src/Converter/Number/GenericNumberConverter.php
@@ -26,10 +26,7 @@ use Ramsey\Uuid\Type\Integer as IntegerObject;
*/
class GenericNumberConverter implements NumberConverterInterface
{
- /**
- * @var CalculatorInterface
- */
- private $calculator;
+ private CalculatorInterface $calculator;
public function __construct(CalculatorInterface $calculator)
{
@@ -37,11 +34,7 @@ class GenericNumberConverter implements NumberConverterInterface
}
/**
- * @inheritDoc
* @psalm-pure
- * @psalm-return numeric-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
*/
public function fromHex(string $hex): string
{
@@ -49,15 +42,10 @@ class GenericNumberConverter implements NumberConverterInterface
}
/**
- * @inheritDoc
* @psalm-pure
- * @psalm-return non-empty-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
*/
public function toHex(string $number): string
{
- /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
return $this->calculator->toBase(new IntegerObject($number), 16);
}
}
diff --git a/src/Converter/NumberConverterInterface.php b/src/Converter/NumberConverterInterface.php
index b33ec31..5df2bad 100644
--- a/src/Converter/NumberConverterInterface.php
+++ b/src/Converter/NumberConverterInterface.php
@@ -23,17 +23,15 @@ namespace Ramsey\Uuid\Converter;
interface NumberConverterInterface
{
/**
- * Converts a hexadecimal number into an string integer representation of
+ * Converts a hexadecimal number into a string integer representation of
* the number
*
* The integer representation returned is a string representation of the
* integer, to accommodate unsigned integers greater than PHP_INT_MAX.
*
- * @param string $hex The hexadecimal string representation to convert
+ * @param non-empty-string $hex The hexadecimal string representation to convert
*
- * @return string String representation of an integer
- *
- * @psalm-return numeric-string
+ * @return numeric-string String representation of an integer
*
* @psalm-pure
*/
@@ -43,13 +41,11 @@ interface NumberConverterInterface
* Converts a string integer representation into a hexadecimal string
* representation of the number
*
- * @param string $number A string integer representation to convert; this
- * must be a numeric string to accommodate unsigned integers greater
- * than PHP_INT_MAX.
+ * @param numeric-string $number A string integer representation to convert;
+ * this must be a numeric string to accommodate unsigned integers
+ * greater than PHP_INT_MAX.
*
- * @return string Hexadecimal string
- *
- * @psalm-return non-empty-string
+ * @return non-empty-string Hexadecimal string
*
* @psalm-pure
*/
diff --git a/src/Converter/Time/GenericTimeConverter.php b/src/Converter/Time/GenericTimeConverter.php
index a8aa64b..4c808ce 100644
--- a/src/Converter/Time/GenericTimeConverter.php
+++ b/src/Converter/Time/GenericTimeConverter.php
@@ -50,14 +50,8 @@ class GenericTimeConverter implements TimeConverterInterface
*/
private const MICROSECOND_INTERVALS = '10';
- /**
- * @var CalculatorInterface
- */
- private $calculator;
-
- public function __construct(CalculatorInterface $calculator)
+ public function __construct(private readonly CalculatorInterface $calculator)
{
- $this->calculator = $calculator;
}
public function calculateTime(string $seconds, string $microseconds): Hexadecimal
@@ -103,7 +97,7 @@ class GenericTimeConverter implements TimeConverterInterface
// From the total, subtract the number of 100-nanosecond intervals from
// the Gregorian calendar epoch to the Unix epoch. This gives us the
// number of 100-nanosecond intervals from the Unix epoch, which also
- // includes the microtime.
+ // includes the micro-time.
$epochNanoseconds = $this->calculator->subtract(
$this->calculator->toInteger($uuidTimestamp),
new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS)
diff --git a/src/Converter/Time/PhpTimeConverter.php b/src/Converter/Time/PhpTimeConverter.php
index 538d2f2..a2815a0 100644
--- a/src/Converter/Time/PhpTimeConverter.php
+++ b/src/Converter/Time/PhpTimeConverter.php
@@ -31,7 +31,6 @@ use function strlen;
use function substr;
use const STR_PAD_LEFT;
-use const STR_PAD_RIGHT;
/**
* PhpTimeConverter uses built-in PHP functions and standard math operations
@@ -58,35 +57,12 @@ class PhpTimeConverter implements TimeConverterInterface
*/
private const MICROSECOND_INTERVALS = 10;
- /**
- * @var CalculatorInterface
- */
- private $calculator;
-
- /**
- * @var TimeConverterInterface
- */
- private $fallbackConverter;
-
- /**
- * @var int
- */
- private $phpPrecision;
+ private readonly int $phpPrecision;
public function __construct(
- ?CalculatorInterface $calculator = null,
- ?TimeConverterInterface $fallbackConverter = null
+ private readonly CalculatorInterface $calculator = new BrickMathCalculator(),
+ private readonly TimeConverterInterface $fallbackConverter = new GenericTimeConverter(new BrickMathCalculator())
) {
- if ($calculator === null) {
- $calculator = new BrickMathCalculator();
- }
-
- if ($fallbackConverter === null) {
- $fallbackConverter = new GenericTimeConverter($calculator);
- }
-
- $this->calculator = $calculator;
- $this->fallbackConverter = $fallbackConverter;
$this->phpPrecision = (int) ini_get('precision');
}
@@ -124,7 +100,7 @@ class PhpTimeConverter implements TimeConverterInterface
/ self::SECOND_INTERVALS
);
- if (count($splitTime) === 0) {
+ if (!isset($splitTime['sec']) || !isset($splitTime['usec'])) {
return $this->fallbackConverter->convertTime($uuidTimestamp);
}
@@ -132,12 +108,13 @@ class PhpTimeConverter implements TimeConverterInterface
}
/**
- * @param int|float $time The time to split into seconds and microseconds
+ * @param float|int $time The time to split into seconds and microseconds
*
- * @return string[]
+ * @return array{sec?: numeric-string, usec?: numeric-string}
*/
- private function splitTime($time): array
+ private function splitTime(float | int $time): array
{
+ /** @var numeric-string[] $split */
$split = explode('.', (string) $time, 2);
// If the $time value is a float but $split only has 1 element, then the
@@ -175,9 +152,12 @@ class PhpTimeConverter implements TimeConverterInterface
}
}
+ /** @var numeric-string $microseconds */
+ $microseconds = str_pad((string) $microseconds, 6, '0');
+
return [
'sec' => $split[0],
- 'usec' => str_pad((string) $microseconds, 6, '0', STR_PAD_RIGHT),
+ 'usec' => $microseconds,
];
}
}
diff --git a/src/Converter/TimeConverterInterface.php b/src/Converter/TimeConverterInterface.php
index 1e84807..c399947 100644
--- a/src/Converter/TimeConverterInterface.php
+++ b/src/Converter/TimeConverterInterface.php
@@ -32,10 +32,10 @@ interface TimeConverterInterface
*
* @link http://tools.ietf.org/html/rfc4122#section-4.2.2 RFC 4122, ยง 4.2.2: Generation Details
*
- * @param string $seconds A string representation of the number of seconds
- * since the Unix epoch for the time to calculate
- * @param string $microseconds A string representation of the micro-seconds
- * associated with the time to calculate
+ * @param numeric-string $seconds A string representation of the number of
+ * seconds since the Unix epoch for the time to calculate
+ * @param numeric-string $microseconds A string representation of the
+ * micro-seconds associated with the time to calculate
*
* @return Hexadecimal The full UUID timestamp as a Hexadecimal value
*
diff --git a/src/DeprecatedUuidFactoryInterface.php b/src/DeprecatedUuidFactoryInterface.php
index 2ea38f8..0b3d6a3 100644
--- a/src/DeprecatedUuidFactoryInterface.php
+++ b/src/DeprecatedUuidFactoryInterface.php
@@ -69,17 +69,17 @@ interface DeprecatedUuidFactoryInterface
* methods specific to creating subtypes. Instead, version 5 will use
* dedicated factories for each subtype.
*
- * @param Hexadecimal|int|string|null $node A 48-bit number representing the
- * hardware address; this number may be represented as an integer or a
- * hexadecimal string
- * @param int|null $clockSeq A 14-bit number used to help avoid duplicates
- * that could arise when the clock is set backwards in time or if the
- * node ID changes
+ * @param Hexadecimal|positive-int|non-empty-string|null $node A 48-bit
+ * number representing the hardware address; this number may be
+ * represented as an integer or a hexadecimal string
+ * @param positive-int|null $clockSeq A 14-bit number used to help avoid
+ * duplicates that could arise when the clock is set backwards in time
+ * or if the node ID changes
*
* @return UuidInterface A UuidInterface instance that represents a
* version 1 UUID
*/
- public function uuid1($node = null, ?int $clockSeq = null): UuidInterface;
+ public function uuid1(Hexadecimal | int | string | null $node = null, ?int $clockSeq = null): UuidInterface;
/**
* Returns a version 2 (DCE Security) UUID from a local domain, local
@@ -119,7 +119,7 @@ interface DeprecatedUuidFactoryInterface
* methods specific to creating subtypes. Instead, version 5 will use
* dedicated factories for each subtype.
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to use for creating a UUID
*
* @return UuidInterface A UuidInterface instance that represents a
@@ -127,7 +127,7 @@ interface DeprecatedUuidFactoryInterface
*
* @psalm-pure
*/
- public function uuid3($ns, string $name): UuidInterface;
+ public function uuid3(UuidInterface | string $ns, string $name): UuidInterface;
/**
* Returns a version 4 (random) UUID
@@ -149,7 +149,7 @@ interface DeprecatedUuidFactoryInterface
* methods specific to creating subtypes. Instead, version 5 will use
* dedicated factories for each subtype.
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to use for creating a UUID
*
* @return UuidInterface A UuidInterface instance that represents a
@@ -157,7 +157,7 @@ interface DeprecatedUuidFactoryInterface
*
* @psalm-pure
*/
- public function uuid5($ns, string $name): UuidInterface;
+ public function uuid5(UuidInterface | string $ns, string $name): UuidInterface;
/**
* Returns a version 6 (ordered-time) UUID from a host ID, sequence number,
diff --git a/src/Fields/SerializableFieldsTrait.php b/src/Fields/SerializableFieldsTrait.php
index 57f54bf..a326c2f 100644
--- a/src/Fields/SerializableFieldsTrait.php
+++ b/src/Fields/SerializableFieldsTrait.php
@@ -28,17 +28,19 @@ use function strlen;
trait SerializableFieldsTrait
{
/**
- * @param string $bytes The bytes that comprise the fields
+ * @param non-empty-string $bytes The bytes that comprise the fields
*/
abstract public function __construct(string $bytes);
/**
* Returns the bytes that comprise the fields
+ *
+ * @return non-empty-string
*/
abstract public function getBytes(): string;
/**
- * @return array{bytes: string}
+ * @return array{bytes: non-empty-string}
*/
public function __serialize(): array
{
@@ -46,7 +48,7 @@ trait SerializableFieldsTrait
}
/**
- * @inheritdoc
+ * @inheritDoc
*/
public function __unserialize(array $data): void
{
@@ -54,12 +56,14 @@ trait SerializableFieldsTrait
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));
}
- assert(is_string($data['bytes']));
+ assert(is_string($data['bytes']) && $data['bytes'] !== '');
if (strlen($data['bytes']) === 16) {
$this->__construct($data['bytes']);
} else {
- $this->__construct(base64_decode($data['bytes']));
+ /** @var non-empty-string $bytes */
+ $bytes = base64_decode($data['bytes']);
+ $this->__construct($bytes);
}
}
}
diff --git a/src/Generator/CombGenerator.php b/src/Generator/CombGenerator.php
index 25b7988..49f769f 100644
--- a/src/Generator/CombGenerator.php
+++ b/src/Generator/CombGenerator.php
@@ -61,25 +61,18 @@ class CombGenerator implements RandomGeneratorInterface
{
public const TIMESTAMP_BYTES = 6;
- /**
- * @var RandomGeneratorInterface
- */
- private $randomGenerator;
-
- /**
- * @var NumberConverterInterface
- */
- private $converter;
-
public function __construct(
- RandomGeneratorInterface $generator,
- NumberConverterInterface $numberConverter
+ private readonly RandomGeneratorInterface $generator,
+ private readonly NumberConverterInterface $numberConverter
) {
- $this->converter = $numberConverter;
- $this->randomGenerator = $generator;
}
/**
+ * @param positive-int $length The number of bytes of random binary data to
+ * generate
+ *
+ * @return non-empty-string
+ *
* @throws InvalidArgumentException if $length is not a positive integer
* greater than or equal to CombGenerator::TIMESTAMP_BYTES
*
@@ -95,16 +88,17 @@ class CombGenerator implements RandomGeneratorInterface
$hash = '';
if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {
- $hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES);
+ $hash = $this->generator->generate($length - self::TIMESTAMP_BYTES);
}
$lsbTime = str_pad(
- $this->converter->toHex($this->timestamp()),
+ $this->numberConverter->toHex($this->timestamp()),
self::TIMESTAMP_BYTES * 2,
'0',
STR_PAD_LEFT
);
+ /** @var non-empty-string */
return (string) hex2bin(
str_pad(
bin2hex($hash),
@@ -116,12 +110,15 @@ class CombGenerator implements RandomGeneratorInterface
}
/**
- * Returns current timestamp a string integer, precise to 0.00001 seconds
+ * Returns current timestamp as string integer, precise to 0.00001 seconds
+ *
+ * @return numeric-string
*/
private function timestamp(): string
{
- $time = explode(' ', microtime(false));
+ $time = explode(' ', microtime());
+ /** @var numeric-string */
return $time[1] . substr($time[0], 2, 5);
}
}
diff --git a/src/Generator/DceSecurityGenerator.php b/src/Generator/DceSecurityGenerator.php
index aca8c5d..de0c71f 100644
--- a/src/Generator/DceSecurityGenerator.php
+++ b/src/Generator/DceSecurityGenerator.php
@@ -52,35 +52,17 @@ class DceSecurityGenerator implements DceSecurityGeneratorInterface
*/
private const CLOCK_SEQ_LOW = 0;
- /**
- * @var NumberConverterInterface
- */
- private $numberConverter;
-
- /**
- * @var TimeGeneratorInterface
- */
- private $timeGenerator;
-
- /**
- * @var DceSecurityProviderInterface
- */
- private $dceSecurityProvider;
-
public function __construct(
- NumberConverterInterface $numberConverter,
- TimeGeneratorInterface $timeGenerator,
- DceSecurityProviderInterface $dceSecurityProvider
+ private readonly NumberConverterInterface $numberConverter,
+ private readonly TimeGeneratorInterface $timeGenerator,
+ private readonly DceSecurityProviderInterface $dceSecurityProvider
) {
- $this->numberConverter = $numberConverter;
- $this->timeGenerator = $timeGenerator;
- $this->dceSecurityProvider = $dceSecurityProvider;
}
public function generate(
int $localDomain,
?IntegerObject $localIdentifier = null,
- ?Hexadecimal $node = null,
+ Hexadecimal | int | string | null $node = null,
?int $clockSeq = null
): string {
if (!in_array($localDomain, self::DOMAINS)) {
@@ -140,10 +122,6 @@ class DceSecurityGenerator implements DceSecurityGeneratorInterface
$domainByte = pack('n', $localDomain)[1];
$identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT));
- if ($node instanceof Hexadecimal) {
- $node = $node->toString();
- }
-
// Shift the clock sequence 8 bits to the left, so it matches 0x3f00.
if ($clockSeq !== null) {
$clockSeq = $clockSeq << 8;
@@ -153,8 +131,8 @@ class DceSecurityGenerator implements DceSecurityGeneratorInterface
// Replace bytes in the time-based UUID with DCE Security values.
$bytes = substr_replace($bytes, $identifierBytes, 0, 4);
- $bytes = substr_replace($bytes, $domainByte, 9, 1);
- return $bytes;
+ /** @var non-empty-string */
+ return substr_replace($bytes, $domainByte, 9, 1);
}
}
diff --git a/src/Generator/DceSecurityGeneratorInterface.php b/src/Generator/DceSecurityGeneratorInterface.php
index faa29a5..4912224 100644
--- a/src/Generator/DceSecurityGeneratorInterface.php
+++ b/src/Generator/DceSecurityGeneratorInterface.php
@@ -36,18 +36,18 @@ interface DceSecurityGeneratorInterface
* given domain; this may be a UID or GID on POSIX systems, if the local
* domain is person or group, or it may be a site-defined identifier
* if the local domain is org
- * @param Hexadecimal|null $node A 48-bit number representing the hardware
- * address
+ * @param Hexadecimal|positive-int|non-empty-string|null $node A 48-bit
+ * number representing the hardware address
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
* that could arise when the clock is set backwards in time or if the
* node ID changes
*
- * @return string A binary string
+ * @return non-empty-string A binary string
*/
public function generate(
int $localDomain,
?IntegerObject $localIdentifier = null,
- ?Hexadecimal $node = null,
+ Hexadecimal | int | string | null $node = null,
?int $clockSeq = null
): string;
}
diff --git a/src/Generator/DefaultNameGenerator.php b/src/Generator/DefaultNameGenerator.php
index 7303e9f..e029c07 100644
--- a/src/Generator/DefaultNameGenerator.php
+++ b/src/Generator/DefaultNameGenerator.php
@@ -26,11 +26,14 @@ use function hash;
*/
class DefaultNameGenerator implements NameGeneratorInterface
{
- /** @psalm-pure */
+ /**
+ * @inheritDoc
+ * @psalm-pure
+ */
public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string
{
try {
- /** @var string|bool $bytes */
+ /** @var non-empty-string|false $bytes */
$bytes = @hash($hashAlgorithm, $ns->getBytes() . $name, true);
} catch (ValueError $e) {
$bytes = false; // keep same behavior than PHP 7
@@ -43,6 +46,6 @@ class DefaultNameGenerator implements NameGeneratorInterface
));
}
- return (string) $bytes;
+ return $bytes;
}
}
diff --git a/src/Generator/DefaultTimeGenerator.php b/src/Generator/DefaultTimeGenerator.php
index c2a8748..10a1716 100644
--- a/src/Generator/DefaultTimeGenerator.php
+++ b/src/Generator/DefaultTimeGenerator.php
@@ -40,29 +40,11 @@ use const STR_PAD_LEFT;
*/
class DefaultTimeGenerator implements TimeGeneratorInterface
{
- /**
- * @var NodeProviderInterface
- */
- private $nodeProvider;
-
- /**
- * @var TimeConverterInterface
- */
- private $timeConverter;
-
- /**
- * @var TimeProviderInterface
- */
- private $timeProvider;
-
public function __construct(
- NodeProviderInterface $nodeProvider,
- TimeConverterInterface $timeConverter,
- TimeProviderInterface $timeProvider
+ private readonly NodeProviderInterface $nodeProvider,
+ private readonly TimeConverterInterface $timeConverter,
+ private readonly TimeProviderInterface $timeProvider
) {
- $this->nodeProvider = $nodeProvider;
- $this->timeConverter = $timeConverter;
- $this->timeProvider = $timeProvider;
}
/**
@@ -71,7 +53,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
*
* @inheritDoc
*/
- public function generate($node = null, ?int $clockSeq = null): string
+ public function generate(Hexadecimal | int | string | null $node = null, ?int $clockSeq = null): string
{
if ($node instanceof Hexadecimal) {
$node = $node->toString();
@@ -121,13 +103,14 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
* Uses the node provider given when constructing this instance to get
* the node ID (usually a MAC address)
*
- * @param string|int|null $node A node value that may be used to override the node provider
+ * @param non-empty-string|positive-int|null $node A node value that may be
+ * used to override the node provider
*
- * @return string 6-byte binary string representation of the node
+ * @return non-empty-string 6-byte binary string representation of the node
*
* @throws InvalidArgumentException
*/
- private function getValidNode($node): string
+ private function getValidNode(int | string | null $node): string
{
if ($node === null) {
$node = $this->nodeProvider->getNode();
@@ -142,6 +125,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
throw new InvalidArgumentException('Invalid node value');
}
+ /** @var non-empty-string */
return (string) hex2bin(str_pad((string) $node, 12, '0', STR_PAD_LEFT));
}
}
diff --git a/src/Generator/NameGeneratorInterface.php b/src/Generator/NameGeneratorInterface.php
index cc43dd0..28d834f 100644
--- a/src/Generator/NameGeneratorInterface.php
+++ b/src/Generator/NameGeneratorInterface.php
@@ -28,9 +28,9 @@ interface NameGeneratorInterface
*
* @param UuidInterface $ns The namespace
* @param string $name The name to use for creating a UUID
- * @param string $hashAlgorithm The hashing algorithm to use
+ * @param non-empty-string $hashAlgorithm The hashing algorithm to use
*
- * @return string A binary string
+ * @return non-empty-string A binary string
*
* @psalm-pure
*/
diff --git a/src/Generator/PeclUuidNameGenerator.php b/src/Generator/PeclUuidNameGenerator.php
index 3780c5c..1f477de 100644
--- a/src/Generator/PeclUuidNameGenerator.php
+++ b/src/Generator/PeclUuidNameGenerator.php
@@ -30,25 +30,23 @@ use function uuid_parse;
*/
class PeclUuidNameGenerator implements NameGeneratorInterface
{
- /** @psalm-pure */
+ /**
+ * @inheritDoc
+ * @psalm-pure
+ */
public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string
{
- switch ($hashAlgorithm) {
- case 'md5':
- $uuid = uuid_generate_md5($ns->toString(), $name);
-
- break;
- case 'sha1':
- $uuid = uuid_generate_sha1($ns->toString(), $name);
-
- break;
- default:
- throw new NameException(sprintf(
+ $uuid = match ($hashAlgorithm) {
+ 'md5' => uuid_generate_md5($ns->toString(), $name),
+ 'sha1' => uuid_generate_sha1($ns->toString(), $name),
+ default => throw new NameException(
+ sprintf(
'Unable to hash namespace and name with algorithm \'%s\'',
$hashAlgorithm
- ));
- }
+ )
+ ),
+ };
- return uuid_parse($uuid);
+ return uuid_parse($uuid) ?: throw new NameException('Unable to generate UUID from ext-uuid');
}
}
diff --git a/src/Generator/PeclUuidRandomGenerator.php b/src/Generator/PeclUuidRandomGenerator.php
index 07c47d2..84306e5 100644
--- a/src/Generator/PeclUuidRandomGenerator.php
+++ b/src/Generator/PeclUuidRandomGenerator.php
@@ -14,6 +14,8 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
+use Ramsey\Uuid\Exception\NameException;
+
use function uuid_create;
use function uuid_parse;
@@ -30,6 +32,6 @@ class PeclUuidRandomGenerator implements RandomGeneratorInterface
{
$uuid = uuid_create(UUID_TYPE_RANDOM);
- return uuid_parse($uuid);
+ return uuid_parse($uuid) ?: throw new NameException('Unable to generate UUID from ext-uuid');
}
}
diff --git a/src/Generator/PeclUuidTimeGenerator.php b/src/Generator/PeclUuidTimeGenerator.php
index e01f44e..306edce 100644
--- a/src/Generator/PeclUuidTimeGenerator.php
+++ b/src/Generator/PeclUuidTimeGenerator.php
@@ -14,6 +14,9 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
+use Ramsey\Uuid\Exception\NameException;
+use Ramsey\Uuid\Type\Hexadecimal;
+
use function uuid_create;
use function uuid_parse;
@@ -27,13 +30,10 @@ use const UUID_TYPE_TIME;
*/
class PeclUuidTimeGenerator implements TimeGeneratorInterface
{
- /**
- * @inheritDoc
- */
- public function generate($node = null, ?int $clockSeq = null): string
+ public function generate(Hexadecimal | int | string | null $node = null, ?int $clockSeq = null): string
{
$uuid = uuid_create(UUID_TYPE_TIME);
- return uuid_parse($uuid);
+ return uuid_parse($uuid) ?: throw new NameException('Unable to generate UUID from ext-uuid');
}
}
diff --git a/src/Generator/RandomGeneratorInterface.php b/src/Generator/RandomGeneratorInterface.php
index 80c219b..172d976 100644
--- a/src/Generator/RandomGeneratorInterface.php
+++ b/src/Generator/RandomGeneratorInterface.php
@@ -22,9 +22,10 @@ interface RandomGeneratorInterface
/**
* Generates a string of randomized binary data
*
- * @param positive-int $length The number of bytes of random binary data to generate
+ * @param positive-int $length The number of bytes of random binary data to
+ * generate
*
- * @return string A binary string
+ * @return non-empty-string A binary string
*/
public function generate(int $length): string;
}
diff --git a/src/Generator/TimeGeneratorFactory.php b/src/Generator/TimeGeneratorFactory.php
index 3d55fc4..6a8f51b 100644
--- a/src/Generator/TimeGeneratorFactory.php
+++ b/src/Generator/TimeGeneratorFactory.php
@@ -24,29 +24,11 @@ use Ramsey\Uuid\Provider\TimeProviderInterface;
*/
class TimeGeneratorFactory
{
- /**
- * @var NodeProviderInterface
- */
- private $nodeProvider;
-
- /**
- * @var TimeConverterInterface
- */
- private $timeConverter;
-
- /**
- * @var TimeProviderInterface
- */
- private $timeProvider;
-
public function __construct(
- NodeProviderInterface $nodeProvider,
- TimeConverterInterface $timeConverter,
- TimeProviderInterface $timeProvider
+ private readonly NodeProviderInterface $nodeProvider,
+ private readonly TimeConverterInterface $timeConverter,
+ private readonly TimeProviderInterface $timeProvider
) {
- $this->nodeProvider = $nodeProvider;
- $this->timeConverter = $timeConverter;
- $this->timeProvider = $timeProvider;
}
/**
diff --git a/src/Generator/TimeGeneratorInterface.php b/src/Generator/TimeGeneratorInterface.php
index 18f21c4..7103e0e 100644
--- a/src/Generator/TimeGeneratorInterface.php
+++ b/src/Generator/TimeGeneratorInterface.php
@@ -25,14 +25,14 @@ interface TimeGeneratorInterface
/**
* Generate a binary string from a node ID, clock sequence, and current time
*
- * @param Hexadecimal|int|string|null $node A 48-bit number representing the
- * hardware address; this number may be represented as an integer or a
- * hexadecimal string
+ * @param Hexadecimal|non-empty-string|positive-int|null $node A 48-bit
+ * number representing the hardware address; this number may be
+ * represented as an integer or a hexadecimal string
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
* that could arise when the clock is set backwards in time or if the
* node ID changes
*
- * @return string A binary string
+ * @return non-empty-string A binary string
*/
- public function generate($node = null, ?int $clockSeq = null): string;
+ public function generate(Hexadecimal | int | string | null $node = null, ?int $clockSeq = null): string;
}
diff --git a/src/Guid/Fields.php b/src/Guid/Fields.php
index eda6e6a..094cdce 100644
--- a/src/Guid/Fields.php
+++ b/src/Guid/Fields.php
@@ -50,28 +50,21 @@ final class Fields implements FieldsInterface
use VersionTrait;
/**
- * @var string
- */
- private $bytes;
-
- /**
- * @param string $bytes A 16-byte binary string representation of a UUID
+ * @param non-empty-string $bytes A 16-byte binary string representation of a UUID
*
* @throws InvalidArgumentException if the byte string is not exactly 16 bytes
* @throws InvalidArgumentException if the byte string does not represent a GUID
* @throws InvalidArgumentException if the byte string does not contain a valid version
*/
- public function __construct(string $bytes)
+ public function __construct(private readonly string $bytes)
{
- if (strlen($bytes) !== 16) {
+ if (strlen($this->bytes) !== 16) {
throw new InvalidArgumentException(
'The byte string must be 16 bytes long; '
- . 'received ' . strlen($bytes) . ' bytes'
+ . 'received ' . strlen($this->bytes) . ' bytes'
);
}
- $this->bytes = $bytes;
-
if (!$this->isCorrectVariant()) {
throw new InvalidArgumentException(
'The byte string received does not conform to the RFC '
@@ -94,7 +87,7 @@ final class Fields implements FieldsInterface
public function getTimeLow(): Hexadecimal
{
// Swap the bytes from little endian to network byte order.
- /** @var array $hex */
+ /** @var array{mixed, non-empty-string} $hex */
$hex = unpack(
'H*',
pack(
@@ -104,13 +97,13 @@ final class Fields implements FieldsInterface
)
);
- return new Hexadecimal((string) ($hex[1] ?? ''));
+ return new Hexadecimal($hex[1]);
}
public function getTimeMid(): Hexadecimal
{
// Swap the bytes from little endian to network byte order.
- /** @var array $hex */
+ /** @var array{mixed, non-empty-string} $hex */
$hex = unpack(
'H*',
pack(
@@ -119,13 +112,13 @@ final class Fields implements FieldsInterface
)
);
- return new Hexadecimal((string) ($hex[1] ?? ''));
+ return new Hexadecimal($hex[1]);
}
public function getTimeHiAndVersion(): Hexadecimal
{
// Swap the bytes from little endian to network byte order.
- /** @var array $hex */
+ /** @var array{mixed, non-empty-string} $hex */
$hex = unpack(
'H*',
pack(
@@ -134,39 +127,52 @@ final class Fields implements FieldsInterface
)
);
- return new Hexadecimal((string) ($hex[1] ?? ''));
+ return new Hexadecimal($hex[1]);
}
public function getTimestamp(): Hexadecimal
{
- return new Hexadecimal(sprintf(
+ /** @var non-empty-string $timestamp */
+ $timestamp = sprintf(
'%03x%04s%08s',
hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
$this->getTimeMid()->toString(),
$this->getTimeLow()->toString()
- ));
+ );
+
+ return new Hexadecimal($timestamp);
}
public function getClockSeq(): Hexadecimal
{
$clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff;
+ $clockSeqHex = str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT);
- return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT));
+ return new Hexadecimal($clockSeqHex);
}
public function getClockSeqHiAndReserved(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1)));
+ /** @var non-empty-string $clockSeqHiAndReserved */
+ $clockSeqHiAndReserved = bin2hex(substr($this->bytes, 8, 1));
+
+ return new Hexadecimal($clockSeqHiAndReserved);
}
public function getClockSeqLow(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1)));
+ /** @var non-empty-string $clockSeqLow */
+ $clockSeqLow = bin2hex(substr($this->bytes, 9, 1));
+
+ return new Hexadecimal($clockSeqLow);
}
public function getNode(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 10)));
+ /** @var non-empty-string $node */
+ $node = bin2hex(substr($this->bytes, 10));
+
+ return new Hexadecimal($node);
}
public function getVersion(): ?int
diff --git a/src/Guid/GuidBuilder.php b/src/Guid/GuidBuilder.php
index 758dd6b..b6ba6f8 100644
--- a/src/Guid/GuidBuilder.php
+++ b/src/Guid/GuidBuilder.php
@@ -31,16 +31,6 @@ use Throwable;
*/
class GuidBuilder implements UuidBuilderInterface
{
- /**
- * @var NumberConverterInterface
- */
- private $numberConverter;
-
- /**
- * @var TimeConverterInterface
- */
- private $timeConverter;
-
/**
* @param NumberConverterInterface $numberConverter The number converter to
* use when constructing the Guid
@@ -48,18 +38,16 @@ class GuidBuilder implements UuidBuilderInterface
* for converting timestamps extracted from a UUID to Unix timestamps
*/
public function __construct(
- NumberConverterInterface $numberConverter,
- TimeConverterInterface $timeConverter
+ private readonly NumberConverterInterface $numberConverter,
+ private readonly TimeConverterInterface $timeConverter,
) {
- $this->numberConverter = $numberConverter;
- $this->timeConverter = $timeConverter;
}
/**
* Builds and returns a Guid
*
* @param CodecInterface $codec The codec to use for building this Guid instance
- * @param string $bytes The byte string from which to construct a UUID
+ * @param non-empty-string $bytes The byte string from which to construct a UUID
*
* @return Guid The GuidBuilder returns an instance of Ramsey\Uuid\Guid\Guid
*
@@ -81,6 +69,8 @@ class GuidBuilder implements UuidBuilderInterface
/**
* Proxy method to allow injecting a mock, for testing
+ *
+ * @param non-empty-string $bytes
*/
protected function buildFields(string $bytes): Fields
{
diff --git a/src/Lazy/LazyUuidFromString.php b/src/Lazy/LazyUuidFromString.php
index f63db47..41a1701 100644
--- a/src/Lazy/LazyUuidFromString.php
+++ b/src/Lazy/LazyUuidFromString.php
@@ -55,21 +55,19 @@ use function substr;
final class LazyUuidFromString implements TimeBasedUuidInterface
{
public const VALID_REGEX = '/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ms';
- /**
- * @var string
- * @psalm-var non-empty-string
- */
- private $uuid;
- /** @var UuidInterface|null */
- private $unwrapped;
- /** @psalm-param non-empty-string $uuid */
- public function __construct(string $uuid)
+ private ?UuidInterface $unwrapped = null;
+
+ /**
+ * @psalm-param non-empty-string $uuid
+ */
+ public function __construct(private readonly string $uuid)
{
- $this->uuid = $uuid;
}
- /** @psalm-pure */
+ /**
+ * @psalm-pure
+ */
public static function fromBytes(string $bytes): self
{
$base16Uuid = bin2hex($bytes);
@@ -96,7 +94,7 @@ final class LazyUuidFromString implements TimeBasedUuidInterface
}
/**
- * @inheritdoc
+ * @inheritDoc
*/
public function __unserialize(array $data): void
{
diff --git a/src/Math/BrickMathCalculator.php b/src/Math/BrickMathCalculator.php
index f2d8678..33e725f 100644
--- a/src/Math/BrickMathCalculator.php
+++ b/src/Math/BrickMathCalculator.php
@@ -114,6 +114,7 @@ final class BrickMathCalculator implements CalculatorInterface
public function toBase(IntegerObject $value, int $base): string
{
try {
+ /** @var non-empty-string */
return BigInteger::of($value->toString())->toBase($base);
} catch (MathException | \InvalidArgumentException $exception) {
throw new InvalidArgumentException(
diff --git a/src/Math/CalculatorInterface.php b/src/Math/CalculatorInterface.php
index f03645d..95aa76a 100644
--- a/src/Math/CalculatorInterface.php
+++ b/src/Math/CalculatorInterface.php
@@ -28,8 +28,8 @@ interface CalculatorInterface
/**
* Returns the sum of all the provided parameters
*
- * @param NumberInterface $augend The first addend (the integer being added to)
- * @param NumberInterface ...$addends The additional integers to a add to the augend
+ * @param NumberInterface $augend The integer being added to
+ * @param NumberInterface ...$addends The additional integers to add to the augend
*
* @return NumberInterface The sum of all the parameters
*/
@@ -77,7 +77,7 @@ interface CalculatorInterface
/**
* Converts a value from an arbitrary base to a base-10 integer value
*
- * @param string $value The value to convert
+ * @param non-empty-string $value The value to convert
* @param int $base The base to convert from (i.e., 2, 16, 32, etc.)
*
* @return IntegerObject The base-10 integer value of the converted value
@@ -90,7 +90,7 @@ interface CalculatorInterface
* @param IntegerObject $value The integer value to convert
* @param int $base The base to convert to (i.e., 2, 16, 32, etc.)
*
- * @return string The value represented in the specified base
+ * @return non-empty-string The value represented in the specified base
*/
public function toBase(IntegerObject $value, int $base): string;
diff --git a/src/Nonstandard/Fields.php b/src/Nonstandard/Fields.php
index 927bc6a..a85a147 100644
--- a/src/Nonstandard/Fields.php
+++ b/src/Nonstandard/Fields.php
@@ -48,25 +48,18 @@ final class Fields implements FieldsInterface
use VariantTrait;
/**
- * @var string
- */
- private $bytes;
-
- /**
- * @param string $bytes A 16-byte binary string representation of a UUID
+ * @param non-empty-string $bytes A 16-byte binary string representation of a UUID
*
* @throws InvalidArgumentException if the byte string is not exactly 16 bytes
*/
- public function __construct(string $bytes)
+ public function __construct(private readonly string $bytes)
{
- if (strlen($bytes) !== 16) {
+ if (strlen($this->bytes) !== 16) {
throw new InvalidArgumentException(
'The byte string must be 16 bytes long; '
- . 'received ' . strlen($bytes) . ' bytes'
+ . 'received ' . strlen($this->bytes) . ' bytes'
);
}
-
- $this->bytes = $bytes;
}
public function getBytes(): string
@@ -77,48 +70,70 @@ final class Fields implements FieldsInterface
public function getClockSeq(): Hexadecimal
{
$clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff;
+ $clockSeqHex = str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT);
- return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT));
+ return new Hexadecimal($clockSeqHex);
}
public function getClockSeqHiAndReserved(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1)));
+ /** @var non-empty-string $clockSeqHiAndReserved */
+ $clockSeqHiAndReserved = bin2hex(substr($this->bytes, 8, 1));
+
+ return new Hexadecimal($clockSeqHiAndReserved);
}
public function getClockSeqLow(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1)));
+ /** @var non-empty-string $clockSeqLow */
+ $clockSeqLow = bin2hex(substr($this->bytes, 9, 1));
+
+ return new Hexadecimal($clockSeqLow);
}
public function getNode(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 10)));
+ /** @var non-empty-string $node */
+ $node = bin2hex(substr($this->bytes, 10));
+
+ return new Hexadecimal($node);
}
public function getTimeHiAndVersion(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2)));
+ /** @var non-empty-string $timeHiAndVersion */
+ $timeHiAndVersion = bin2hex(substr($this->bytes, 6, 2));
+
+ return new Hexadecimal($timeHiAndVersion);
}
public function getTimeLow(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4)));
+ /** @var non-empty-string $timeLow */
+ $timeLow = bin2hex(substr($this->bytes, 0, 4));
+
+ return new Hexadecimal($timeLow);
}
public function getTimeMid(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2)));
+ /** @var non-empty-string $timeMid */
+ $timeMid = bin2hex(substr($this->bytes, 4, 2));
+
+ return new Hexadecimal($timeMid);
}
public function getTimestamp(): Hexadecimal
{
- return new Hexadecimal(sprintf(
+ /** @var non-empty-string $timestamp */
+ $timestamp = sprintf(
'%03x%04s%08s',
hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
$this->getTimeMid()->toString(),
$this->getTimeLow()->toString()
- ));
+ );
+
+ return new Hexadecimal($timestamp);
}
public function getVersion(): ?int
diff --git a/src/Nonstandard/UuidBuilder.php b/src/Nonstandard/UuidBuilder.php
index 0c89277..d5c8e42 100644
--- a/src/Nonstandard/UuidBuilder.php
+++ b/src/Nonstandard/UuidBuilder.php
@@ -29,16 +29,6 @@ use Throwable;
*/
class UuidBuilder implements UuidBuilderInterface
{
- /**
- * @var NumberConverterInterface
- */
- private $numberConverter;
-
- /**
- * @var TimeConverterInterface
- */
- private $timeConverter;
-
/**
* @param NumberConverterInterface $numberConverter The number converter to
* use when constructing the Nonstandard\Uuid
@@ -46,18 +36,16 @@ class UuidBuilder implements UuidBuilderInterface
* for converting timestamps extracted from a UUID to Unix timestamps
*/
public function __construct(
- NumberConverterInterface $numberConverter,
- TimeConverterInterface $timeConverter
+ private readonly NumberConverterInterface $numberConverter,
+ private readonly TimeConverterInterface $timeConverter
) {
- $this->numberConverter = $numberConverter;
- $this->timeConverter = $timeConverter;
}
/**
* Builds and returns a Nonstandard\Uuid
*
* @param CodecInterface $codec The codec to use for building this instance
- * @param string $bytes The byte string from which to construct a UUID
+ * @param non-empty-string $bytes The byte string from which to construct a UUID
*
* @return Uuid The Nonstandard\UuidBuilder returns an instance of
* Nonstandard\Uuid
@@ -80,6 +68,8 @@ class UuidBuilder implements UuidBuilderInterface
/**
* Proxy method to allow injecting a mock, for testing
+ *
+ * @param non-empty-string $bytes
*/
protected function buildFields(string $bytes): Fields
{
diff --git a/src/Nonstandard/UuidV6.php b/src/Nonstandard/UuidV6.php
index f5da1da..2bccdf2 100644
--- a/src/Nonstandard/UuidV6.php
+++ b/src/Nonstandard/UuidV6.php
@@ -75,7 +75,7 @@ final class UuidV6 extends Uuid implements UuidInterface, TimeBasedUuidInterface
public function getDateTime(): DateTimeInterface
{
- $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
+ $time = $this->getTimeConverter()->convertTime($this->getFields()->getTimestamp());
try {
return new DateTimeImmutable(
@@ -101,8 +101,11 @@ final class UuidV6 extends Uuid implements UuidInterface, TimeBasedUuidInterface
. '1' . substr($hex, 0, 3)
. substr($hex, 16);
+ /** @var non-empty-string $bin */
+ $bin = (string) hex2bin($hex);
+
/** @var LazyUuidFromString $uuid */
- $uuid = Uuid::fromBytes((string) hex2bin($hex));
+ $uuid = Uuid::fromBytes($bin);
return $uuid->toUuidV1();
}
@@ -119,8 +122,11 @@ final class UuidV6 extends Uuid implements UuidInterface, TimeBasedUuidInterface
. '6' . substr($hex, 5, 3)
. substr($hex, 16);
+ /** @var non-empty-string $bin */
+ $bin = (string) hex2bin($hex);
+
/** @var LazyUuidFromString $uuid */
- $uuid = Uuid::fromBytes((string) hex2bin($hex));
+ $uuid = Uuid::fromBytes($bin);
return $uuid->toUuidV6();
}
diff --git a/src/Provider/Dce/SystemDceSecurityProvider.php b/src/Provider/Dce/SystemDceSecurityProvider.php
index 16003df..9ed84c8 100644
--- a/src/Provider/Dce/SystemDceSecurityProvider.php
+++ b/src/Provider/Dce/SystemDceSecurityProvider.php
@@ -20,8 +20,8 @@ use Ramsey\Uuid\Type\Integer as IntegerObject;
use function escapeshellarg;
use function preg_split;
+use function str_contains;
use function str_getcsv;
-use function strpos;
use function strrpos;
use function strtolower;
use function strtoupper;
@@ -110,15 +110,10 @@ class SystemDceSecurityProvider implements DceSecurityProviderInterface
return '';
}
- switch ($this->getOs()) {
- case 'WIN':
- return $this->getWindowsUid();
- case 'DAR':
- case 'FRE':
- case 'LIN':
- default:
- return trim((string) shell_exec('id -u'));
- }
+ return match ($this->getOs()) {
+ 'WIN' => $this->getWindowsUid(),
+ default => trim((string) shell_exec('id -u')),
+ };
}
/**
@@ -130,15 +125,10 @@ class SystemDceSecurityProvider implements DceSecurityProviderInterface
return '';
}
- switch ($this->getOs()) {
- case 'WIN':
- return $this->getWindowsGid();
- case 'DAR':
- case 'FRE':
- case 'LIN':
- default:
- return trim((string) shell_exec('id -g'));
- }
+ return match ($this->getOs()) {
+ 'WIN' => $this->getWindowsGid(),
+ default => trim((string) shell_exec('id -g')),
+ };
}
/**
@@ -148,7 +138,7 @@ class SystemDceSecurityProvider implements DceSecurityProviderInterface
{
$disabledFunctions = strtolower((string) ini_get('disable_functions'));
- return strpos($disabledFunctions, 'shell_exec') === false;
+ return !str_contains($disabledFunctions, 'shell_exec');
}
/**
diff --git a/src/Provider/Node/FallbackNodeProvider.php b/src/Provider/Node/FallbackNodeProvider.php
index fe890cc..10d8aa2 100644
--- a/src/Provider/Node/FallbackNodeProvider.php
+++ b/src/Provider/Node/FallbackNodeProvider.php
@@ -24,24 +24,18 @@ use Ramsey\Uuid\Type\Hexadecimal;
*/
class FallbackNodeProvider implements NodeProviderInterface
{
- /**
- * @var iterable
- */
- private $nodeProviders;
-
/**
* @param iterable $providers Array of node providers
*/
- public function __construct(iterable $providers)
+ public function __construct(private readonly iterable $providers)
{
- $this->nodeProviders = $providers;
}
public function getNode(): Hexadecimal
{
$lastProviderException = null;
- foreach ($this->nodeProviders as $provider) {
+ foreach ($this->providers as $provider) {
try {
return $provider->getNode();
} catch (NodeException $exception) {
diff --git a/src/Provider/Node/StaticNodeProvider.php b/src/Provider/Node/StaticNodeProvider.php
index 51f1b02..81936b4 100644
--- a/src/Provider/Node/StaticNodeProvider.php
+++ b/src/Provider/Node/StaticNodeProvider.php
@@ -21,6 +21,7 @@ use Ramsey\Uuid\Type\Hexadecimal;
use function dechex;
use function hexdec;
use function str_pad;
+use function strlen;
use function substr;
use const STR_PAD_LEFT;
@@ -32,10 +33,7 @@ use const STR_PAD_LEFT;
*/
class StaticNodeProvider implements NodeProviderInterface
{
- /**
- * @var Hexadecimal
- */
- private $node;
+ private readonly Hexadecimal $node;
/**
* @param Hexadecimal $node The static node value to use
diff --git a/src/Provider/Time/FixedTimeProvider.php b/src/Provider/Time/FixedTimeProvider.php
index 90b1c6a..4da47a7 100644
--- a/src/Provider/Time/FixedTimeProvider.php
+++ b/src/Provider/Time/FixedTimeProvider.php
@@ -26,14 +26,8 @@ use Ramsey\Uuid\Type\Time;
*/
class FixedTimeProvider implements TimeProviderInterface
{
- /**
- * @var Time
- */
- private $fixedTime;
-
- public function __construct(Time $time)
+ public function __construct(private Time $time)
{
- $this->fixedTime = $time;
}
/**
@@ -41,9 +35,9 @@ class FixedTimeProvider implements TimeProviderInterface
*
* @param int|string|IntegerObject $value The `usec` value to set
*/
- public function setUsec($value): void
+ public function setUsec(int | string | IntegerObject $value): void
{
- $this->fixedTime = new Time($this->fixedTime->getSeconds(), $value);
+ $this->time = new Time($this->time->getSeconds(), $value);
}
/**
@@ -51,13 +45,13 @@ class FixedTimeProvider implements TimeProviderInterface
*
* @param int|string|IntegerObject $value The `sec` value to set
*/
- public function setSec($value): void
+ public function setSec(int | string | IntegerObject $value): void
{
- $this->fixedTime = new Time($value, $this->fixedTime->getMicroseconds());
+ $this->time = new Time($value, $this->time->getMicroseconds());
}
public function getTime(): Time
{
- return $this->fixedTime;
+ return $this->time;
}
}
diff --git a/src/Rfc4122/Fields.php b/src/Rfc4122/Fields.php
index 735e219..99a6539 100644
--- a/src/Rfc4122/Fields.php
+++ b/src/Rfc4122/Fields.php
@@ -46,26 +46,22 @@ final class Fields implements FieldsInterface
use VariantTrait;
use VersionTrait;
- private string $bytes;
-
/**
- * @param string $bytes A 16-byte binary string representation of a UUID
+ * @param non-empty-string $bytes A 16-byte binary string representation of a UUID
*
* @throws InvalidArgumentException if the byte string is not exactly 16 bytes
* @throws InvalidArgumentException if the byte string does not represent an RFC 4122 UUID
* @throws InvalidArgumentException if the byte string does not contain a valid version
*/
- public function __construct(string $bytes)
+ public function __construct(private readonly string $bytes)
{
- if (strlen($bytes) !== 16) {
+ if (strlen($this->bytes) !== 16) {
throw new InvalidArgumentException(
'The byte string must be 16 bytes long; '
- . 'received ' . strlen($bytes) . ' bytes'
+ . 'received ' . strlen($this->bytes) . ' bytes'
);
}
- $this->bytes = $bytes;
-
if (!$this->isCorrectVariant()) {
throw new InvalidArgumentException(
'The byte string received does not conform to the RFC 4122 variant'
@@ -87,38 +83,57 @@ final class Fields implements FieldsInterface
public function getClockSeq(): Hexadecimal
{
$clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff;
+ $clockSeqHex = str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT);
- return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT));
+ return new Hexadecimal($clockSeqHex);
}
public function getClockSeqHiAndReserved(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1)));
+ /** @var non-empty-string $clockSeqHiAndReserved */
+ $clockSeqHiAndReserved = bin2hex(substr($this->bytes, 8, 1));
+
+ return new Hexadecimal($clockSeqHiAndReserved);
}
public function getClockSeqLow(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1)));
+ /** @var non-empty-string $clockSeqLow */
+ $clockSeqLow = bin2hex(substr($this->bytes, 9, 1));
+
+ return new Hexadecimal($clockSeqLow);
}
public function getNode(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 10)));
+ /** @var non-empty-string $node */
+ $node = bin2hex(substr($this->bytes, 10));
+
+ return new Hexadecimal($node);
}
public function getTimeHiAndVersion(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2)));
+ /** @var non-empty-string $timeHiAndVersion */
+ $timeHiAndVersion = bin2hex(substr($this->bytes, 6, 2));
+
+ return new Hexadecimal($timeHiAndVersion);
}
public function getTimeLow(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4)));
+ /** @var non-empty-string $timeLow */
+ $timeLow = bin2hex(substr($this->bytes, 0, 4));
+
+ return new Hexadecimal($timeLow);
}
public function getTimeMid(): Hexadecimal
{
- return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2)));
+ /** @var non-empty-string $timeMid */
+ $timeMid = bin2hex(substr($this->bytes, 4, 2));
+
+ return new Hexadecimal($timeMid);
}
/**
@@ -138,33 +153,27 @@ final class Fields implements FieldsInterface
*/
public function getTimestamp(): Hexadecimal
{
- switch ($this->getVersion()) {
- case Uuid::UUID_TYPE_DCE_SECURITY:
- $timestamp = sprintf(
- '%03x%04s%08s',
- hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
- $this->getTimeMid()->toString(),
- ''
- );
-
- break;
- case Uuid::UUID_TYPE_PEABODY:
- $timestamp = sprintf(
- '%08s%04s%03x',
- $this->getTimeLow()->toString(),
- $this->getTimeMid()->toString(),
- hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff
- );
-
- break;
- default:
- $timestamp = sprintf(
- '%03x%04s%08s',
- hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
- $this->getTimeMid()->toString(),
- $this->getTimeLow()->toString()
- );
- }
+ /** @var non-empty-string $timestamp */
+ $timestamp = match ($this->getVersion()) {
+ Uuid::UUID_TYPE_DCE_SECURITY => sprintf(
+ '%03x%04s%08s',
+ hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
+ $this->getTimeMid()->toString(),
+ ''
+ ),
+ Uuid::UUID_TYPE_PEABODY => sprintf(
+ '%08s%04s%03x',
+ $this->getTimeLow()->toString(),
+ $this->getTimeMid()->toString(),
+ hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff
+ ),
+ default => sprintf(
+ '%03x%04s%08s',
+ hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
+ $this->getTimeMid()->toString(),
+ $this->getTimeLow()->toString()
+ ),
+ };
return new Hexadecimal($timestamp);
}
diff --git a/src/Rfc4122/UuidBuilder.php b/src/Rfc4122/UuidBuilder.php
index 919804e..95c104d 100644
--- a/src/Rfc4122/UuidBuilder.php
+++ b/src/Rfc4122/UuidBuilder.php
@@ -32,16 +32,6 @@ use Throwable;
*/
class UuidBuilder implements UuidBuilderInterface
{
- /**
- * @var NumberConverterInterface
- */
- private $numberConverter;
-
- /**
- * @var TimeConverterInterface
- */
- private $timeConverter;
-
/**
* Constructs the UUID builder
*
@@ -51,18 +41,16 @@ class UuidBuilder implements UuidBuilderInterface
* for converting timestamps extracted from a UUID to Unix timestamps
*/
public function __construct(
- NumberConverterInterface $numberConverter,
- TimeConverterInterface $timeConverter
+ private readonly NumberConverterInterface $numberConverter,
+ private readonly TimeConverterInterface $timeConverter
) {
- $this->numberConverter = $numberConverter;
- $this->timeConverter = $timeConverter;
}
/**
* Builds and returns a Uuid
*
* @param CodecInterface $codec The codec to use for building this Uuid instance
- * @param string $bytes The byte string from which to construct a UUID
+ * @param non-empty-string $bytes The byte string from which to construct a UUID
*
* @return Rfc4122UuidInterface UuidBuilder returns instances of Rfc4122UuidInterface
*
@@ -103,6 +91,8 @@ class UuidBuilder implements UuidBuilderInterface
/**
* Proxy method to allow injecting a mock, for testing
+ *
+ * @param non-empty-string $bytes
*/
protected function buildFields(string $bytes): FieldsInterface
{
diff --git a/src/Rfc4122/UuidV1.php b/src/Rfc4122/UuidV1.php
index 8a13fa0..90004b7 100644
--- a/src/Rfc4122/UuidV1.php
+++ b/src/Rfc4122/UuidV1.php
@@ -67,7 +67,7 @@ final class UuidV1 extends Uuid implements UuidInterface, TimeBasedUuidInterface
public function getDateTime(): DateTimeInterface
{
- $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
+ $time = $this->getTimeConverter()->convertTime($this->getFields()->getTimestamp());
try {
return new DateTimeImmutable(
diff --git a/src/Rfc4122/UuidV2.php b/src/Rfc4122/UuidV2.php
index 18fc36f..0588ee5 100644
--- a/src/Rfc4122/UuidV2.php
+++ b/src/Rfc4122/UuidV2.php
@@ -95,7 +95,7 @@ final class UuidV2 extends Uuid implements UuidInterface
*/
public function getDateTime(): DateTimeInterface
{
- $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
+ $time = $this->getTimeConverter()->convertTime($this->getFields()->getTimestamp());
try {
return new DateTimeImmutable(
@@ -111,16 +111,21 @@ final class UuidV2 extends Uuid implements UuidInterface
/**
* Returns the local domain used to create this version 2 UUID
+ *
+ * @return positive-int
*/
public function getLocalDomain(): int
{
$fields = $this->getFields();
+ /** @var positive-int */
return (int) hexdec($fields->getClockSeqLow()->toString());
}
/**
* Returns the string name of the local domain
+ *
+ * @return non-empty-string
*/
public function getLocalDomainName(): string
{
@@ -135,7 +140,7 @@ final class UuidV2 extends Uuid implements UuidInterface
$fields = $this->getFields();
return new IntegerObject(
- $this->numberConverter->fromHex($fields->getTimeLow()->toString())
+ $this->getNumberConverter()->fromHex($fields->getTimeLow()->toString())
);
}
}
diff --git a/src/Rfc4122/Validator.php b/src/Rfc4122/Validator.php
index ed43c98..10b65ca 100644
--- a/src/Rfc4122/Validator.php
+++ b/src/Rfc4122/Validator.php
@@ -30,11 +30,6 @@ final class Validator implements ValidatorInterface
private const VALID_PATTERN = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-'
. '[1-5]{1}[0-9A-Fa-f]{3}-[ABab89]{1}[0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
- /**
- * @psalm-return non-empty-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
- */
public function getPattern(): string
{
return self::VALID_PATTERN;
diff --git a/src/Rfc4122/VersionTrait.php b/src/Rfc4122/VersionTrait.php
index cee55fb..f4b2eea 100644
--- a/src/Rfc4122/VersionTrait.php
+++ b/src/Rfc4122/VersionTrait.php
@@ -42,16 +42,9 @@ trait VersionTrait
return true;
}
- switch ($this->getVersion()) {
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- return true;
- }
-
- return false;
+ return match ($this->getVersion()) {
+ 1, 2, 3, 4, 5, 6 => true,
+ default => false,
+ };
}
}
diff --git a/src/Type/Decimal.php b/src/Type/Decimal.php
index 3ea1ad8..45deb64 100644
--- a/src/Type/Decimal.php
+++ b/src/Type/Decimal.php
@@ -40,7 +40,7 @@ final class Decimal implements NumberInterface
/**
* @var numeric-string
*/
- private string $value;
+ private readonly string $value;
private bool $isNegative = false;
@@ -67,7 +67,7 @@ final class Decimal implements NumberInterface
*/
public function __toString(): string
{
- return $this->toString();
+ return $this->value;
}
/**
@@ -75,7 +75,7 @@ final class Decimal implements NumberInterface
*/
public function jsonSerialize(): string
{
- return $this->toString();
+ return $this->value;
}
/**
@@ -83,7 +83,7 @@ final class Decimal implements NumberInterface
*/
public function __serialize(): array
{
- return ['string' => $this->toString()];
+ return ['string' => $this->value];
}
/**
diff --git a/src/Type/Hexadecimal.php b/src/Type/Hexadecimal.php
index 74da411..983d9d3 100644
--- a/src/Type/Hexadecimal.php
+++ b/src/Type/Hexadecimal.php
@@ -34,10 +34,13 @@ use function substr;
*/
final class Hexadecimal implements TypeInterface
{
- private string $value;
+ /**
+ * @var non-empty-string
+ */
+ private readonly string $value;
/**
- * @param string | self $value The hexadecimal value to store
+ * @param non-empty-string|self $value The hexadecimal value to store
*/
public function __construct(self | string $value)
{
@@ -51,12 +54,12 @@ final class Hexadecimal implements TypeInterface
public function __toString(): string
{
- return $this->toString();
+ return $this->value;
}
public function jsonSerialize(): string
{
- return $this->toString();
+ return $this->value;
}
/**
@@ -64,7 +67,7 @@ final class Hexadecimal implements TypeInterface
*/
public function __serialize(): array
{
- return ['string' => $this->toString()];
+ return ['string' => $this->value];
}
/**
@@ -76,11 +79,16 @@ final class Hexadecimal implements TypeInterface
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));
}
- assert(is_string($data['string']));
+ assert(is_string($data['string']) && $data['string'] !== '');
$this->value = $this->prepareValue($data['string']);
}
+ /**
+ * @param non-empty-string $value
+ *
+ * @return non-empty-string
+ */
private function prepareValue(string $value): string
{
$value = strtolower($value);
@@ -95,6 +103,7 @@ final class Hexadecimal implements TypeInterface
);
}
+ /** @var non-empty-string */
return $value;
}
}
diff --git a/src/Type/Integer.php b/src/Type/Integer.php
index 36f2356..b3c9f19 100644
--- a/src/Type/Integer.php
+++ b/src/Type/Integer.php
@@ -40,9 +40,9 @@ use function substr;
final class Integer implements NumberInterface
{
/**
- * @psalm-var numeric-string
+ * @var numeric-string
*/
- private string $value;
+ private readonly string $value;
private bool $isNegative = false;
@@ -69,7 +69,7 @@ final class Integer implements NumberInterface
*/
public function __toString(): string
{
- return $this->toString();
+ return $this->value;
}
/**
@@ -77,7 +77,7 @@ final class Integer implements NumberInterface
*/
public function jsonSerialize(): string
{
- return $this->toString();
+ return $this->value;
}
/**
@@ -85,7 +85,7 @@ final class Integer implements NumberInterface
*/
public function __serialize(): array
{
- return ['string' => $this->toString()];
+ return ['string' => $this->value];
}
/**
diff --git a/src/Type/Time.php b/src/Type/Time.php
index b29cc86..8e03643 100644
--- a/src/Type/Time.php
+++ b/src/Type/Time.php
@@ -30,8 +30,8 @@ use function sprintf;
*/
final class Time implements TypeInterface
{
- private IntegerObject $seconds;
- private IntegerObject $microseconds;
+ private readonly IntegerObject $seconds;
+ private readonly IntegerObject $microseconds;
public function __construct(
int | float | string | IntegerObject $seconds,
diff --git a/src/Type/TypeInterface.php b/src/Type/TypeInterface.php
index ff1bef1..452543b 100644
--- a/src/Type/TypeInterface.php
+++ b/src/Type/TypeInterface.php
@@ -33,7 +33,13 @@ interface TypeInterface extends JsonSerializable
*/
public function __unserialize(array $data): void;
+ /**
+ * @return non-empty-string
+ */
public function toString(): string;
+ /**
+ * @return non-empty-string
+ */
public function __toString(): string;
}
diff --git a/src/Uuid.php b/src/Uuid.php
index fd2b23c..215a034 100644
--- a/src/Uuid.php
+++ b/src/Uuid.php
@@ -158,38 +158,13 @@ class Uuid implements Rfc4122UuidInterface
self::DCE_DOMAIN_ORG => 'org',
];
- /**
- * @var UuidFactoryInterface|null
- */
- private static $factory = null;
+ private static ?UuidFactoryInterface $factory = null;
/**
* @var bool flag to detect if the UUID factory was replaced internally, which disables all optimizations
* for the default/happy path internal scenarios
*/
- private static $factoryReplaced = false;
-
- /**
- * @var CodecInterface
- */
- protected $codec;
-
- /**
- * The fields that make up this UUID
- *
- * @var Rfc4122FieldsInterface
- */
- protected $fields;
-
- /**
- * @var NumberConverterInterface
- */
- protected $numberConverter;
-
- /**
- * @var TimeConverterInterface
- */
- protected $timeConverter;
+ private static bool $factoryReplaced = false;
/**
* Creates a universally unique identifier (UUID) from an array of fields
@@ -220,15 +195,11 @@ class Uuid implements Rfc4122UuidInterface
* for converting timestamps extracted from a UUID to unix timestamps
*/
public function __construct(
- Rfc4122FieldsInterface $fields,
- NumberConverterInterface $numberConverter,
- CodecInterface $codec,
- TimeConverterInterface $timeConverter
+ private readonly Rfc4122FieldsInterface $fields,
+ private readonly NumberConverterInterface $numberConverter,
+ private readonly CodecInterface $codec,
+ private readonly TimeConverterInterface $timeConverter
) {
- $this->fields = $fields;
- $this->codec = $codec;
- $this->numberConverter = $numberConverter;
- $this->timeConverter = $timeConverter;
}
/**
@@ -264,7 +235,7 @@ class Uuid implements Rfc4122UuidInterface
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));
}
- assert(is_string($data['bytes']));
+ assert(is_string($data['bytes']) && $data['bytes'] !== '');
if (strlen($data['bytes']) === 16) {
/** @var Uuid $uuid */
@@ -304,9 +275,6 @@ class Uuid implements Rfc4122UuidInterface
return $this->compareTo($other) === 0;
}
- /**
- * @psalm-return non-empty-string
- */
public function getBytes(): string
{
return $this->codec->encodeBinary($this);
@@ -319,7 +287,10 @@ class Uuid implements Rfc4122UuidInterface
public function getHex(): Hexadecimal
{
- return new Hexadecimal(str_replace('-', '', $this->toString()));
+ /** @var non-empty-string $hex */
+ $hex = str_replace('-', '', $this->toString());
+
+ return new Hexadecimal($hex);
}
public function getInteger(): IntegerObject
@@ -332,14 +303,27 @@ class Uuid implements Rfc4122UuidInterface
return 'urn:uuid:' . $this->toString();
}
- /**
- * @psalm-return non-empty-string
- */
public function toString(): string
{
return $this->codec->encode($this);
}
+ /**
+ * @internal
+ */
+ protected function getNumberConverter(): NumberConverterInterface
+ {
+ return $this->numberConverter;
+ }
+
+ /**
+ * @internal
+ */
+ protected function getTimeConverter(): TimeConverterInterface
+ {
+ return $this->timeConverter;
+ }
+
/**
* Returns the factory used to create UUIDs
*
@@ -379,7 +363,7 @@ class Uuid implements Rfc4122UuidInterface
/**
* Creates a UUID from a byte string
*
- * @param string $bytes A binary string
+ * @param non-empty-string $bytes A binary string
*
* @return UuidInterface A UuidInterface instance created from a binary
* string representation
@@ -417,7 +401,7 @@ class Uuid implements Rfc4122UuidInterface
/**
* Creates a UUID from the string standard representation
*
- * @param string $uuid A hexadecimal string
+ * @param non-empty-string $uuid A hexadecimal string
*
* @return UuidInterface A UuidInterface instance created from a hexadecimal
* string representation
@@ -434,6 +418,7 @@ class Uuid implements Rfc4122UuidInterface
{
$uuid = strtolower($uuid);
if (! self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) {
+ /** @psalm-suppress DocblockTypeContradiction */
assert($uuid !== '');
return new LazyUuidFromString($uuid);
@@ -466,7 +451,7 @@ class Uuid implements Rfc4122UuidInterface
/**
* Creates a UUID from a 128-bit integer string
*
- * @param string $integer String representation of 128-bit integer
+ * @param numeric-string $integer String representation of 128-bit integer
*
* @return UuidInterface A UuidInterface instance created from the string
* representation of a 128-bit integer
@@ -500,17 +485,17 @@ class Uuid implements Rfc4122UuidInterface
* Returns a version 1 (time-based) UUID from a host ID, sequence number,
* and the current time
*
- * @param Hexadecimal|int|string|null $node A 48-bit number representing the
- * hardware address; this number may be represented as an integer or a
- * hexadecimal string
- * @param int $clockSeq A 14-bit number used to help avoid duplicates that
- * could arise when the clock is set backwards in time or if the node ID
- * changes
+ * @param Hexadecimal|positive-int|non-empty-string|null $node A 48-bit
+ * number representing the hardware address; this number may be
+ * represented as an integer or a hexadecimal string
+ * @param positive-int|null $clockSeq A 14-bit number used to help avoid duplicates
+ * that could arise when the clock is set backwards in time or if the
+ * node ID changes
*
* @return UuidInterface A UuidInterface instance that represents a
* version 1 UUID
*/
- public static function uuid1($node = null, ?int $clockSeq = null): UuidInterface
+ public static function uuid1(Hexadecimal | int | string | null $node = null, ?int $clockSeq = null): UuidInterface
{
return self::getFactory()->uuid1($node, $clockSeq);
}
@@ -548,7 +533,7 @@ class Uuid implements Rfc4122UuidInterface
* Returns a version 3 (name-based) UUID based on the MD5 hash of a
* namespace ID and a name
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to use for creating a UUID
*
* @return UuidInterface A UuidInterface instance that represents a
@@ -562,7 +547,7 @@ class Uuid implements Rfc4122UuidInterface
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*/
- public static function uuid3($ns, string $name): UuidInterface
+ public static function uuid3(UuidInterface | string $ns, string $name): UuidInterface
{
return self::getFactory()->uuid3($ns, $name);
}
@@ -582,7 +567,7 @@ class Uuid implements Rfc4122UuidInterface
* Returns a version 5 (name-based) UUID based on the SHA-1 hash of a
* namespace ID and a name
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to use for creating a UUID
*
* @return UuidInterface A UuidInterface instance that represents a
@@ -596,7 +581,7 @@ class Uuid implements Rfc4122UuidInterface
* is generally to be discouraged. Until the factory is kept
* un-replaced, this method is effectively pure.
*/
- public static function uuid5($ns, string $name): UuidInterface
+ public static function uuid5(UuidInterface | string $ns, string $name): UuidInterface
{
return self::getFactory()->uuid5($ns, $name);
}
@@ -607,7 +592,7 @@ class Uuid implements Rfc4122UuidInterface
*
* @param Hexadecimal|null $node A 48-bit number representing the hardware
* address
- * @param int $clockSeq A 14-bit number used to help avoid duplicates that
+ * @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
* could arise when the clock is set backwards in time or if the node ID
* changes
*
diff --git a/src/UuidFactory.php b/src/UuidFactory.php
index 67ed8ee..1d06721 100644
--- a/src/UuidFactory.php
+++ b/src/UuidFactory.php
@@ -379,7 +379,7 @@ class UuidFactory implements UuidFactoryInterface
* @inheritDoc
* @psalm-pure
*/
- public function uuid3($ns, string $name): UuidInterface
+ public function uuid3(UuidInterface | string $ns, string $name): UuidInterface
{
return $this->uuidFromNsAndName($ns, $name, 3, 'md5');
}
@@ -395,14 +395,14 @@ class UuidFactory implements UuidFactoryInterface
* @inheritDoc
* @psalm-pure
*/
- public function uuid5($ns, string $name): UuidInterface
+ public function uuid5(UuidInterface | string $ns, string $name): UuidInterface
{
return $this->uuidFromNsAndName($ns, $name, 5, 'sha1');
}
public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface
{
- $nodeHex = $node ? $node->toString() : null;
+ $nodeHex = $node?->toString();
$bytes = $this->timeGenerator->generate($nodeHex, $clockSeq);
// Rearrange the bytes, according to the UUID version 6 specification.
@@ -413,6 +413,7 @@ class UuidFactory implements UuidFactoryInterface
// Drop the first four bits, while adding an empty four bits for the
// version field. This allows us to reconstruct the correct time from
// the bytes of this UUID.
+ /** @var non-empty-string $v6Bytes */
$v6Bytes = hex2bin(substr($v6, 1, 12) . '0' . substr($v6, -3));
$v6Bytes .= substr($bytes, 8);
@@ -425,7 +426,7 @@ class UuidFactory implements UuidFactoryInterface
* Uses the configured builder and codec and the provided byte string to
* construct a Uuid object.
*
- * @param string $bytes The byte string from which to construct a UUID
+ * @param non-empty-string $bytes The byte string from which to construct a UUID
*
* @return UuidInterface An instance of UuidInterface, created from the
* provided bytes
@@ -440,33 +441,40 @@ class UuidFactory implements UuidFactoryInterface
/**
* Returns a version 3 or 5 namespaced Uuid
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to hash together with the namespace
- * @param int $version The version of UUID to create (3 or 5)
- * @param string $hashAlgorithm The hashing algorithm to use when hashing
- * together the namespace and name
+ * @param positive-int $version The version of UUID to create (3 or 5)
+ * @param non-empty-string $hashAlgorithm The hashing algorithm to use when
+ * hashing together the namespace and name
*
* @return UuidInterface An instance of UuidInterface, created by hashing
* together the provided namespace and name
*
* @psalm-pure
*/
- private function uuidFromNsAndName($ns, string $name, int $version, string $hashAlgorithm): UuidInterface
- {
+ private function uuidFromNsAndName(
+ UuidInterface | string $ns,
+ string $name,
+ int $version,
+ string $hashAlgorithm,
+ ): UuidInterface {
if (!($ns instanceof UuidInterface)) {
$ns = $this->fromString($ns);
}
$bytes = $this->nameGenerator->generate($ns, $name, $hashAlgorithm);
- return $this->uuidFromBytesAndVersion(substr($bytes, 0, 16), $version);
+ /** @var non-empty-string $bytes */
+ $bytes = substr($bytes, 0, 16);
+
+ return $this->uuidFromBytesAndVersion($bytes, $version);
}
/**
* Returns an RFC 4122 variant Uuid, created from the provided bytes and version
*
- * @param string $bytes The byte string to convert to a UUID
- * @param int $version The RFC 4122 version to apply to the UUID
+ * @param non-empty-string $bytes The byte string to convert to a UUID
+ * @param positive-int $version The RFC 4122 version to apply to the UUID
*
* @return UuidInterface An instance of UuidInterface, created from the
* byte string and version
@@ -486,6 +494,8 @@ class UuidFactory implements UuidFactoryInterface
$clockSeqHiAndReserved = pack('n*', BinaryUtils::applyVariant($clockSeqHi));
$bytes = substr_replace($bytes, $timeHiAndVersion, 6, 2);
+
+ /** @var non-empty-string $bytes */
$bytes = substr_replace($bytes, $clockSeqHiAndReserved, 8, 2);
if ($this->isDefaultFeatureSet) {
diff --git a/src/UuidFactoryInterface.php b/src/UuidFactoryInterface.php
index e2a1236..9f08c29 100644
--- a/src/UuidFactoryInterface.php
+++ b/src/UuidFactoryInterface.php
@@ -23,7 +23,7 @@ interface UuidFactoryInterface extends DeprecatedUuidFactoryInterface
/**
* Creates a UUID from a byte string
*
- * @param string $bytes A binary string
+ * @param non-empty-string $bytes A binary string
*
* @return UuidInterface A UuidInterface instance created from a binary
* string representation
@@ -35,7 +35,7 @@ interface UuidFactoryInterface extends DeprecatedUuidFactoryInterface
/**
* Creates a UUID from a 128-bit integer string
*
- * @param string $integer String representation of 128-bit integer
+ * @param numeric-string $integer String representation of 128-bit integer
*
* @return UuidInterface A UuidInterface instance created from the string
* representation of a 128-bit integer
@@ -47,7 +47,7 @@ interface UuidFactoryInterface extends DeprecatedUuidFactoryInterface
/**
* Creates a UUID from the string standard representation
*
- * @param string $uuid A hexadecimal string
+ * @param non-empty-string $uuid A hexadecimal string
*
* @return UuidInterface A UuidInterface instance created from a hexadecimal
* string representation
diff --git a/src/Validator/GenericValidator.php b/src/Validator/GenericValidator.php
index fd60955..5c0ebdf 100644
--- a/src/Validator/GenericValidator.php
+++ b/src/Validator/GenericValidator.php
@@ -31,11 +31,6 @@ final class GenericValidator implements ValidatorInterface
*/
private const VALID_PATTERN = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\z';
- /**
- * @psalm-return non-empty-string
- * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
- * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
- */
public function getPattern(): string
{
return self::VALID_PATTERN;
diff --git a/src/Validator/ValidatorInterface.php b/src/Validator/ValidatorInterface.php
index 3d4bd6f..95833f8 100644
--- a/src/Validator/ValidatorInterface.php
+++ b/src/Validator/ValidatorInterface.php
@@ -24,9 +24,7 @@ interface ValidatorInterface
/**
* Returns the regular expression pattern used by this validator
*
- * @return string The regular expression pattern this validator uses
- *
- * @psalm-return non-empty-string
+ * @return non-empty-string The regular expression pattern this validator uses
*/
public function getPattern(): string;
diff --git a/src/Variant.php b/src/Variant.php
index fc969f2..185cb21 100644
--- a/src/Variant.php
+++ b/src/Variant.php
@@ -1,9 +1,13 @@
+ * @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
diff --git a/src/functions.php b/src/functions.php
index d722f06..aa956e9 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -22,16 +22,16 @@ use Ramsey\Uuid\Type\Integer as IntegerObject;
* Returns a version 1 (time-based) UUID from a host ID, sequence number,
* and the current time
*
- * @param Hexadecimal|int|string|null $node A 48-bit number representing the
- * hardware address; this number may be represented as an integer or a
- * hexadecimal string
- * @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
- * could arise when the clock is set backwards in time or if the node ID
- * changes
+ * @param Hexadecimal|positive-int|non-empty-string|null $node A 48-bit number
+ * representing the hardware address; this number may be represented as an
+ * integer or a hexadecimal string
+ * @param positive-int|null $clockSeq A 14-bit number used to help avoid
+ * duplicates that could arise when the clock is set backwards in time or
+ * if the node ID changes
*
* @return non-empty-string Version 1 UUID as a string
*/
-function v1($node = null, ?int $clockSeq = null): string
+function v1(Hexadecimal | int | string | null $node = null, ?int $clockSeq = null): string
{
return Uuid::uuid1($node, $clockSeq)->toString();
}
@@ -67,14 +67,15 @@ function v2(
* Returns a version 3 (name-based) UUID based on the MD5 hash of a
* namespace ID and a name
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string $name The name to use for creating a UUID
*
* @return non-empty-string Version 3 UUID as a string
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*/
-function v3($ns, string $name): string
+function v3(UuidInterface | string $ns, string $name): string
{
return Uuid::uuid3($ns, $name)->toString();
}
@@ -93,14 +94,15 @@ function v4(): string
* Returns a version 5 (name-based) UUID based on the SHA-1 hash of a
* namespace ID and a name
*
- * @param string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID)
+ * @param non-empty-string $name The name to use for creating a UUID
*
* @return non-empty-string Version 5 UUID as a string
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*/
-function v5($ns, string $name): string
+function v5(UuidInterface | string $ns, string $name): string
{
return Uuid::uuid5($ns, $name)->toString();
}
diff --git a/tests/Builder/FallbackBuilderTest.php b/tests/Builder/FallbackBuilderTest.php
index 7fe2adb..2e605ee 100644
--- a/tests/Builder/FallbackBuilderTest.php
+++ b/tests/Builder/FallbackBuilderTest.php
@@ -63,6 +63,8 @@ class FallbackBuilderTest extends TestCase
}
/**
+ * @param non-empty-string $bytes
+ *
* @dataProvider provideBytes
*/
public function testSerializationOfBuilderCollection(string $bytes): void
@@ -118,7 +120,7 @@ class FallbackBuilderTest extends TestCase
}
/**
- * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
+ * @return array
*/
public function provideBytes(): array
{
diff --git a/tests/Codec/GuidStringCodecTest.php b/tests/Codec/GuidStringCodecTest.php
index 4394586..e770434 100644
--- a/tests/Codec/GuidStringCodecTest.php
+++ b/tests/Codec/GuidStringCodecTest.php
@@ -21,27 +21,19 @@ use function pack;
class GuidStringCodecTest extends TestCase
{
- /**
- * @var UuidBuilderInterface & MockObject
- */
- private $builder;
-
- /**
- * @var UuidInterface & MockObject
- */
- private $uuid;
-
- /**
- * @var Fields
- */
- private $fields;
+ private UuidBuilderInterface & MockObject $builder;
+ private UuidInterface & MockObject $uuid;
+ private Fields $fields;
protected function setUp(): void
{
parent::setUp();
$this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock();
$this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock();
- $this->fields = new Fields((string) hex2bin('785634123412cd4babef1234abcd4321'));
+
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('785634123412cd4babef1234abcd4321');
+ $this->fields = new Fields($bytes);
}
protected function tearDown(): void
@@ -70,6 +62,7 @@ class GuidStringCodecTest extends TestCase
public function testEncodeBinary(): void
{
+ /** @var non-empty-string $expectedBytes */
$expectedBytes = (string) hex2bin('785634123412cd4babef1234abcd4321');
$fields = new Fields($expectedBytes);
@@ -112,7 +105,10 @@ class GuidStringCodecTest extends TestCase
public function testDecodeBytesReturnsUuid(): void
{
$string = '1234567812344bcd4bef1234abcd4321';
+
+ /** @var non-empty-string $bytes */
$bytes = pack('H*', $string);
+
$codec = new GuidStringCodec($this->builder);
$this->builder->method('build')
->willReturn($this->uuid);
diff --git a/tests/Codec/OrderedTimeCodecTest.php b/tests/Codec/OrderedTimeCodecTest.php
index ecc7c08..ddf4d22 100644
--- a/tests/Codec/OrderedTimeCodecTest.php
+++ b/tests/Codec/OrderedTimeCodecTest.php
@@ -32,37 +32,30 @@ use function unserialize;
class OrderedTimeCodecTest extends TestCase
{
- /**
- * @var UuidBuilderInterface & MockObject
- */
- private $builder;
+ private UuidBuilderInterface & MockObject $builder;
+ private UuidInterface & MockObject $uuid;
+ private Fields $fields;
/**
- * @var UuidInterface & MockObject
+ * @var non-empty-string
*/
- private $uuid;
+ private string $uuidString = '58e0a7d7-eebc-11d8-9669-0800200c9a66';
/**
- * @var Fields
+ * @var non-empty-string
*/
- private $fields;
-
- /**
- * @var string
- */
- private $uuidString = '58e0a7d7-eebc-11d8-9669-0800200c9a66';
-
- /**
- * @var string
- */
- private $optimizedHex = '11d8eebc58e0a7d796690800200c9a66';
+ private string $optimizedHex = '11d8eebc58e0a7d796690800200c9a66';
protected function setUp(): void
{
parent::setUp();
$this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock();
$this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock();
- $this->fields = new Fields((string) hex2bin('58e0a7d7eebc11d896690800200c9a66'));
+
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('58e0a7d7eebc11d896690800200c9a66');
+
+ $this->fields = new Fields($bytes);
}
protected function tearDown(): void
@@ -109,9 +102,11 @@ class OrderedTimeCodecTest extends TestCase
public function testDecodeBytesThrowsExceptionWhenBytesStringNotSixteenCharacters(): void
{
$string = '61';
- $bytes = pack('H*', $string);
$codec = new OrderedTimeCodec($this->builder);
+ /** @var non-empty-string $bytes */
+ $bytes = pack('H*', $string);
+
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$bytes string should contain 16 characters.');
$codec->decodeBytes($bytes);
@@ -129,6 +124,7 @@ class OrderedTimeCodecTest extends TestCase
public function testDecodeBytesRearrangesFields(): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin($this->optimizedHex);
$calculator = new BrickMathCalculator();
@@ -150,7 +146,10 @@ class OrderedTimeCodecTest extends TestCase
{
$nonRfc4122Uuid = '58e0a7d7-eebc-11d8-d669-0800200c9a66';
- $fields = new NonstandardFields((string) hex2bin(str_replace('-', '', $nonRfc4122Uuid)));
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin(str_replace('-', '', $nonRfc4122Uuid));
+
+ $fields = new NonstandardFields($bytes);
$numberConverter = Mockery::mock(NumberConverterInterface::class);
$timeConverter = Mockery::mock(TimeConverterInterface::class);
$builder = new Rfc4122UuidBuilder($numberConverter, $timeConverter);
@@ -195,6 +194,8 @@ class OrderedTimeCodecTest extends TestCase
public function testDecodeBytesThrowsExceptionsForNonRfc4122Uuid(): void
{
$nonRfc4122OptimizedHex = '11d8eebc58e0a7d716690800200c9a66';
+
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin($nonRfc4122OptimizedHex);
$calculator = new BrickMathCalculator();
@@ -215,6 +216,8 @@ class OrderedTimeCodecTest extends TestCase
public function testDecodeBytesThrowsExceptionsForNonTimeBasedUuid(): void
{
$nonTimeBasedOptimizedHex = '41d8eebc58e0a7d796690800200c9a66';
+
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin($nonTimeBasedOptimizedHex);
$numberConverter = Mockery::mock(NumberConverterInterface::class);
diff --git a/tests/Codec/StringCodecTest.php b/tests/Codec/StringCodecTest.php
index 2412dcd..e60615d 100644
--- a/tests/Codec/StringCodecTest.php
+++ b/tests/Codec/StringCodecTest.php
@@ -20,32 +20,24 @@ use function pack;
class StringCodecTest extends TestCase
{
- /**
- * @var UuidBuilderInterface & MockObject
- */
- private $builder;
+ private UuidBuilderInterface & MockObject $builder;
+ private UuidInterface & MockObject $uuid;
+ private Fields $fields;
/**
- * @var UuidInterface & MockObject
+ * @var non-empty-string
*/
- private $uuid;
-
- /**
- * @var Fields
- */
- private $fields;
-
- /**
- * @var string
- */
- private $uuidString = '12345678-1234-4bcd-abef-1234abcd4321';
+ private string $uuidString = '12345678-1234-4bcd-abef-1234abcd4321';
protected function setUp(): void
{
parent::setUp();
$this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock();
$this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock();
- $this->fields = new Fields((string) hex2bin('1234567812344bcdabef1234abcd4321'));
+
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('1234567812344bcdabef1234abcd4321');
+ $this->fields = new Fields($bytes);
}
protected function tearDown(): void
@@ -130,7 +122,10 @@ class StringCodecTest extends TestCase
public function testDecodeBytesThrowsExceptionWhenBytesStringNotSixteenCharacters(): void
{
$string = '61';
+
+ /** @var non-empty-string $bytes */
$bytes = pack('H*', $string);
+
$codec = new StringCodec($this->builder);
$this->expectException(InvalidArgumentException::class);
@@ -141,7 +136,10 @@ class StringCodecTest extends TestCase
public function testDecodeBytesReturnsUuid(): void
{
$string = '123456781234abcdabef1234abcd4321';
+
+ /** @var non-empty-string $bytes */
$bytes = pack('H*', $string);
+
$codec = new StringCodec($this->builder);
$this->builder->method('build')
->willReturn($this->uuid);
diff --git a/tests/Converter/Time/GenericTimeConverterTest.php b/tests/Converter/Time/GenericTimeConverterTest.php
index aba4106..1b0dab4 100644
--- a/tests/Converter/Time/GenericTimeConverterTest.php
+++ b/tests/Converter/Time/GenericTimeConverterTest.php
@@ -12,6 +12,9 @@ use Ramsey\Uuid\Type\Hexadecimal;
class GenericTimeConverterTest extends TestCase
{
/**
+ * @param numeric-string $seconds
+ * @param numeric-string $microseconds
+ *
* @dataProvider provideCalculateTime
*/
public function testCalculateTime(string $seconds, string $microseconds, string $expected): void
diff --git a/tests/Converter/Time/PhpTimeConverterTest.php b/tests/Converter/Time/PhpTimeConverterTest.php
index f3209b0..a55f56c 100644
--- a/tests/Converter/Time/PhpTimeConverterTest.php
+++ b/tests/Converter/Time/PhpTimeConverterTest.php
@@ -36,7 +36,14 @@ class PhpTimeConverterTest extends TestCase
$expected .= sprintf('%08s', $calculatedTime->and($maskLow)->toBase(16));
$converter = new PhpTimeConverter();
- $returned = $converter->calculateTime((string) $seconds, (string) $microseconds);
+
+ /** @var numeric-string $numericSeconds */
+ $numericSeconds = (string) $seconds;
+
+ /** @var numeric-string $numericMicroseconds */
+ $numericMicroseconds = (string) $microseconds;
+
+ $returned = $converter->calculateTime($numericSeconds, $numericMicroseconds);
$this->assertSame($expected, $returned->toString());
}
@@ -134,6 +141,9 @@ class PhpTimeConverterTest extends TestCase
}
/**
+ * @param numeric-string $seconds
+ * @param numeric-string $microseconds
+ *
* @dataProvider provideCalculateTime
*/
public function testCalculateTime(string $seconds, string $microseconds, string $expected): void
diff --git a/tests/Encoder/TimestampFirstCombCodecTest.php b/tests/Encoder/TimestampFirstCombCodecTest.php
index 875695f..8dfb19f 100644
--- a/tests/Encoder/TimestampFirstCombCodecTest.php
+++ b/tests/Encoder/TimestampFirstCombCodecTest.php
@@ -36,7 +36,9 @@ class TimestampFirstCombCodecTest extends TestCase
public function testEncoding(): void
{
- $fields = new Fields((string) hex2bin('ff6f8cb0c57d11e19b210800200c9a66'));
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('ff6f8cb0c57d11e19b210800200c9a66');
+ $fields = new Fields($bytes);
$uuidMock = Mockery::mock(UuidInterface::class, [
'getFields' => $fields,
@@ -49,7 +51,9 @@ class TimestampFirstCombCodecTest extends TestCase
public function testBinaryEncoding(): void
{
- $fields = new Fields((string) hex2bin('ff6f8cb0c57d11e19b210800200c9a66'));
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('ff6f8cb0c57d11e19b210800200c9a66');
+ $fields = new Fields($bytes);
$uuidMock = Mockery::mock(UuidInterface::class, [
'getFields' => $fields,
@@ -93,6 +97,9 @@ class TimestampFirstCombCodecTest extends TestCase
'node' => '0800200c9a66',
]))
);
- $this->codec->decodeBytes((string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'));
+
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d');
+ $this->codec->decodeBytes($bytes);
}
}
diff --git a/tests/Encoder/TimestampLastCombCodecTest.php b/tests/Encoder/TimestampLastCombCodecTest.php
index 5bd41c2..195bc4c 100644
--- a/tests/Encoder/TimestampLastCombCodecTest.php
+++ b/tests/Encoder/TimestampLastCombCodecTest.php
@@ -37,7 +37,9 @@ class TimestampLastCombCodecTest extends TestCase
public function testEncoding(): void
{
- $fields = new Fields((string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'));
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d');
+ $fields = new Fields($bytes);
$uuidMock = Mockery::mock(UuidInterface::class, [
'getFields' => $fields,
@@ -96,6 +98,9 @@ class TimestampLastCombCodecTest extends TestCase
'node' => 'ff6f8cb0c57d',
]))
);
- $this->codec->decodeBytes((string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'));
+
+ /** @var non-empty-string $bytes */
+ $bytes = (string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d');
+ $this->codec->decodeBytes($bytes);
}
}
diff --git a/tests/Generator/DceSecurityGeneratorTest.php b/tests/Generator/DceSecurityGeneratorTest.php
index 418496b..63e7932 100644
--- a/tests/Generator/DceSecurityGeneratorTest.php
+++ b/tests/Generator/DceSecurityGeneratorTest.php
@@ -28,6 +28,9 @@ use function substr;
class DceSecurityGeneratorTest extends TestCase
{
/**
+ * @param non-empty-string $node
+ * @param positive-int $providedDomain
+ *
* @dataProvider provideValuesForDceSecurityGenerator
*/
public function testGenerateBytesReplacesBytesWithDceValues(
diff --git a/tests/Generator/DefaultNameGeneratorTest.php b/tests/Generator/DefaultNameGeneratorTest.php
index 5c15421..a58f52e 100644
--- a/tests/Generator/DefaultNameGeneratorTest.php
+++ b/tests/Generator/DefaultNameGeneratorTest.php
@@ -15,6 +15,7 @@ class DefaultNameGeneratorTest extends TestCase
{
/**
* @param non-empty-string $ns
+ * @param non-empty-string $algorithm
*
* @dataProvider provideNamesForHashingTest
*/
diff --git a/tests/Generator/DefaultTimeGeneratorTest.php b/tests/Generator/DefaultTimeGeneratorTest.php
index f12a93d..706477d 100644
--- a/tests/Generator/DefaultTimeGeneratorTest.php
+++ b/tests/Generator/DefaultTimeGeneratorTest.php
@@ -26,40 +26,26 @@ use function hex2bin;
class DefaultTimeGeneratorTest extends TestCase
{
- /**
- * @var TimeProviderInterface & MockInterface
- */
- private $timeProvider;
+ private TimeProviderInterface & MockInterface $timeProvider;
+ private NodeProviderInterface & MockObject $nodeProvider;
+ private TimeConverterInterface & MockObject $timeConverter;
/**
- * @var NodeProviderInterface & MockObject
+ * @var non-empty-string
*/
- private $nodeProvider;
-
- /**
- * @var TimeConverterInterface & MockObject
- */
- private $timeConverter;
-
- /**
- * @var string
- */
- private $nodeId = '122f80ca9e06';
+ private string $nodeId = '122f80ca9e06';
/**
* @var int[]
*/
- private $currentTime;
+ private array $currentTime;
+
+ private Hexadecimal $calculatedTime;
/**
- * @var Hexadecimal
+ * @var positive-int
*/
- private $calculatedTime;
-
- /**
- * @var int
- */
- private $clockSeq = 4066;
+ private int $clockSeq = 4066;
protected function setUp(): void
{
diff --git a/tests/Generator/PeclUuidNameGeneratorTest.php b/tests/Generator/PeclUuidNameGeneratorTest.php
index 6501b24..6908335 100644
--- a/tests/Generator/PeclUuidNameGeneratorTest.php
+++ b/tests/Generator/PeclUuidNameGeneratorTest.php
@@ -20,6 +20,7 @@ class PeclUuidNameGeneratorTest extends TestCase
{
/**
* @param non-empty-string $ns
+ * @param non-empty-string $algorithm
*
* @dataProvider provideNamesForHashingTest
* @requires extension uuid
diff --git a/tests/Guid/FieldsTest.php b/tests/Guid/FieldsTest.php
index 42f1f75..9507a75 100644
--- a/tests/Guid/FieldsTest.php
+++ b/tests/Guid/FieldsTest.php
@@ -31,6 +31,7 @@ class FieldsTest extends TestCase
*/
public function testConstructorThrowsExceptionIfNotRfc4122Variant(string $guid): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin($guid);
$this->expectException(InvalidArgumentException::class);
@@ -70,6 +71,7 @@ class FieldsTest extends TestCase
*/
public function testConstructorThrowsExceptionIfInvalidVersion(string $guid): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin($guid);
$this->expectException(InvalidArgumentException::class);
@@ -97,12 +99,14 @@ class FieldsTest extends TestCase
}
/**
- * @param string|int $expectedValue
- *
* @dataProvider fieldGetterMethodProvider
*/
- public function testFieldGetterMethods(string $bytes, string $methodName, $expectedValue): void
- {
+ public function testFieldGetterMethods(
+ string $bytes,
+ string $methodName,
+ bool | int | string | Variant | null $expectedValue
+ ): void {
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin($bytes);
$fields = new Fields($bytes);
@@ -193,6 +197,7 @@ class FieldsTest extends TestCase
public function testSerializingFields(): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin('b08c6fff7dc5e111cb210800200c9a66');
$fields = new Fields($bytes);
diff --git a/tests/Nonstandard/FieldsTest.php b/tests/Nonstandard/FieldsTest.php
index 9fe4873..cbe377f 100644
--- a/tests/Nonstandard/FieldsTest.php
+++ b/tests/Nonstandard/FieldsTest.php
@@ -34,6 +34,7 @@ class FieldsTest extends TestCase
*/
public function testFieldGetterMethods(string $uuid, string $methodName, $expectedValue): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', $uuid));
$fields = new Fields($bytes);
@@ -68,6 +69,7 @@ class FieldsTest extends TestCase
public function testSerializingFields(): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', 'ff6f8cb0-c57d-91e1-0b21-0800200c9a66'));
$fields = new Fields($bytes);
diff --git a/tests/Rfc4122/FieldsTest.php b/tests/Rfc4122/FieldsTest.php
index 8a356b0..2ce62fe 100644
--- a/tests/Rfc4122/FieldsTest.php
+++ b/tests/Rfc4122/FieldsTest.php
@@ -32,6 +32,7 @@ class FieldsTest extends TestCase
*/
public function testConstructorThrowsExceptionIfNotRfc4122Variant(string $uuid): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', $uuid));
$this->expectException(InvalidArgumentException::class);
@@ -68,6 +69,7 @@ class FieldsTest extends TestCase
*/
public function testConstructorThrowsExceptionIfInvalidVersion(string $uuid): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', $uuid));
$this->expectException(InvalidArgumentException::class);
@@ -98,6 +100,7 @@ class FieldsTest extends TestCase
*/
public function testFieldGetterMethods(string $uuid, string $methodName, $expectedValue): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', $uuid));
$fields = new Fields($bytes);
@@ -204,6 +207,7 @@ class FieldsTest extends TestCase
public function testSerializingFields(): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', 'ff6f8cb0-c57d-11e1-9b21-0800200c9a66'));
$fields = new Fields($bytes);
diff --git a/tests/Rfc4122/UuidBuilderTest.php b/tests/Rfc4122/UuidBuilderTest.php
index 9e39067..acd18a3 100644
--- a/tests/Rfc4122/UuidBuilderTest.php
+++ b/tests/Rfc4122/UuidBuilderTest.php
@@ -33,6 +33,7 @@ class UuidBuilderTest extends TestCase
*/
public function testBuild(string $uuid, string $expectedClass, int $expectedVersion): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', $uuid));
$calculator = new BrickMathCalculator();
@@ -91,6 +92,7 @@ class UuidBuilderTest extends TestCase
public function testBuildThrowsUnableToBuildException(): void
{
+ /** @var non-empty-string $bytes */
$bytes = (string) hex2bin(str_replace('-', '', 'ff6f8cb0-c57d-51e1-9b21-0800200c9a'));
$calculator = new BrickMathCalculator();
diff --git a/tests/Rfc4122/UuidV2Test.php b/tests/Rfc4122/UuidV2Test.php
index 086e5dd..b0226e2 100644
--- a/tests/Rfc4122/UuidV2Test.php
+++ b/tests/Rfc4122/UuidV2Test.php
@@ -72,6 +72,8 @@ class UuidV2Test extends TestCase
}
/**
+ * @param positive-int $domain
+ *
* @dataProvider provideLocalDomainAndIdentifierForTests
*/
public function testGetLocalDomainAndIdentifier(
diff --git a/tests/Type/HexadecimalTest.php b/tests/Type/HexadecimalTest.php
index 2509bb3..4b40978 100644
--- a/tests/Type/HexadecimalTest.php
+++ b/tests/Type/HexadecimalTest.php
@@ -16,6 +16,8 @@ use function unserialize;
class HexadecimalTest extends TestCase
{
/**
+ * @param Hexadecimal|non-empty-string $value
+ *
* @dataProvider provideHex
*/
public function testHexadecimalType(Hexadecimal | string $value, string $expected): void
@@ -27,7 +29,7 @@ class HexadecimalTest extends TestCase
}
/**
- * @return array
+ * @return array
*/
public function provideHex(): array
{
@@ -52,6 +54,8 @@ class HexadecimalTest extends TestCase
}
/**
+ * @param non-empty-string $value
+ *
* @dataProvider provideHexBadValues
*/
public function testHexadecimalTypeThrowsExceptionForBadValues(string $value): void
@@ -78,6 +82,8 @@ class HexadecimalTest extends TestCase
}
/**
+ * @param Hexadecimal|non-empty-string $value
+ *
* @dataProvider provideHex
*/
public function testSerializeUnserializeHexadecimal(Hexadecimal | string $value, string $expected): void
@@ -92,6 +98,8 @@ class HexadecimalTest extends TestCase
}
/**
+ * @param Hexadecimal|non-empty-string $value
+ *
* @dataProvider provideHex
*/
public function testJsonSerialize(Hexadecimal | string $value, string $expected): void
diff --git a/tests/UuidTest.php b/tests/UuidTest.php
index 5a49a7f..99af9af 100644
--- a/tests/UuidTest.php
+++ b/tests/UuidTest.php
@@ -125,6 +125,7 @@ class UuidTest extends TestCase
$this->expectException(InvalidUuidStringException::class);
$this->expectExceptionMessage('Invalid UUID string: ');
+ /** @phpstan-ignore-next-line */
Uuid::fromString('');
}
@@ -1112,6 +1113,8 @@ class UuidTest extends TestCase
* @param non-empty-string $string
* @param non-empty-string $curly
* @param non-empty-string $hex
+ * @param non-empty-string $bytes
+ * @param numeric-string $int
* @param string[] $fields
* @param non-empty-string $urn
*
@@ -1130,11 +1133,14 @@ class UuidTest extends TestCase
int $variant,
?int $version
): void {
+ /** @var non-empty-string $base64DecodedBytes */
+ $base64DecodedBytes = base64_decode($bytes);
+
$uuids = [
Uuid::fromString($string),
Uuid::fromString($curly),
Uuid::fromString($hex),
- Uuid::fromBytes(base64_decode($bytes)),
+ Uuid::fromBytes($base64DecodedBytes),
Uuid::fromString($urn),
Uuid::fromInteger($int),
];
@@ -1143,7 +1149,7 @@ class UuidTest extends TestCase
foreach ($uuids as $uuid) {
$this->assertSame($string, $uuid->toString());
$this->assertSame($hex, $uuid->getHex()->toString());
- $this->assertSame(base64_decode($bytes), $uuid->getBytes());
+ $this->assertSame($base64DecodedBytes, $uuid->getBytes());
$this->assertSame($int, $uuid->getInteger()->toString());
$this->assertSame($fields['time_low'], $uuid->getFields()->getTimeLow()->toString());
$this->assertSame($fields['time_mid'], $uuid->getFields()->getTimeMid()->toString());
@@ -1484,6 +1490,7 @@ class UuidTest extends TestCase
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid UUID string:');
+ /** @phpstan-ignore-next-line */
Uuid::uuid3('', '');
}
@@ -1506,6 +1513,7 @@ class UuidTest extends TestCase
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid UUID string:');
+ /** @phpstan-ignore-next-line */
Uuid::uuid5('', '');
}
diff --git a/tests/benchmark/UuidGenerationBench.php b/tests/benchmark/UuidGenerationBench.php
index c547a9f..e1f3281 100644
--- a/tests/benchmark/UuidGenerationBench.php
+++ b/tests/benchmark/UuidGenerationBench.php
@@ -22,14 +22,14 @@ use Ramsey\Uuid\UuidInterface;
final class UuidGenerationBench
{
- /** @var Hexadecimal */
- private $node;
- /** @var int */
- private $clockSequence;
- /** @var IntegerIdentifier */
- private $localIdentifier;
- /** @var UuidInterface */
- private $namespace;
+ /**
+ * @var positive-int
+ */
+ private int $clockSequence;
+
+ private Hexadecimal $node;
+ private IntegerIdentifier $localIdentifier;
+ private UuidInterface $namespace;
public function __construct()
{