From 0997de99a2a2ce27f6b5a913dcb26442ab61a6c8 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 5 Sep 2021 12:41:13 +0100 Subject: [PATCH] Add support for PHP 8.1 --- .github/workflows/continuous-integration.yml | 5 +-- composer.json | 3 +- phpstan.neon.dist | 4 +++ psalm-baseline.xml | 35 +++++++++++++++++++- src/Fields/SerializableFieldsTrait.php | 23 +++++++++++++ src/Lazy/LazyUuidFromString.php | 26 +++++++++++++++ src/Type/Decimal.php | 22 ++++++++++++ src/Type/Hexadecimal.php | 22 ++++++++++++ src/Type/Integer.php | 22 ++++++++++++ src/Type/Time.php | 25 ++++++++++++++ src/Uuid.php | 22 ++++++++++++ 11 files changed, 203 insertions(+), 6 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index cdd15f3..5c8a0b8 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -66,6 +66,7 @@ jobs: - "7.3" - "7.4" - "8.0" + - "8.1" steps: - name: "Checkout repository" @@ -114,7 +115,6 @@ jobs: unit-tests: name: "Unit Tests" runs-on: "ubuntu-latest" - continue-on-error: ${{ matrix.experimental }} strategy: fail-fast: false @@ -124,11 +124,8 @@ jobs: - "7.3" - "7.4" - "8.0" - experimental: - - false include: - php-version: "8.1" - experimental: true composer-options: "--ignore-platform-req=php" steps: diff --git a/composer.json b/composer.json index a29d483..a2f1014 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "ext-json": "*", "brick/math": "^0.8 || ^0.9", "ramsey/collection": "^1.0", - "symfony/polyfill-ctype": "^1.8" + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-php80": "^1.14" }, "replace": { "rhumsaa/uuid": "self.version" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 131212f..1382f73 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -29,3 +29,7 @@ parameters: message: '#^Call to an undefined method Ramsey\\Uuid\\Fields\\FieldsInterface::get.*$#' count: 9 path: ./src/Lazy/LazyUuidFromString.php + - + message: '#^Result of \|\| is always false\.$#' + count: 1 + path: ./src/Type/Time.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 2744786..402c2ac 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + DegradedUuid @@ -48,6 +48,11 @@ $timeProvider + + + unserialize + + uuid_generate_md5 @@ -62,6 +67,11 @@ $this + + + unserialize + + $this @@ -108,6 +118,26 @@ $this + + + unserialize + + + + + unserialize + + + + + unserialize + + + + + __construct + + getFactory @@ -117,6 +147,9 @@ getFactory getFactory + + unserialize + diff --git a/src/Fields/SerializableFieldsTrait.php b/src/Fields/SerializableFieldsTrait.php index a076568..96c8baf 100644 --- a/src/Fields/SerializableFieldsTrait.php +++ b/src/Fields/SerializableFieldsTrait.php @@ -14,7 +14,10 @@ declare(strict_types=1); namespace Ramsey\Uuid\Fields; +use ValueError; + use function base64_decode; +use function sprintf; use function strlen; /** @@ -42,6 +45,14 @@ trait SerializableFieldsTrait return $this->getBytes(); } + /** + * @return array{bytes: string} + */ + public function __serialize(): array + { + return ['bytes' => $this->getBytes()]; + } + /** * Constructs the object from a serialized string representation * @@ -58,4 +69,16 @@ trait SerializableFieldsTrait $this->__construct(base64_decode($serialized)); } } + + /** + * @param array{bytes: string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['bytes'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->unserialize($data['bytes']); + } } diff --git a/src/Lazy/LazyUuidFromString.php b/src/Lazy/LazyUuidFromString.php index 82c156c..093cd52 100644 --- a/src/Lazy/LazyUuidFromString.php +++ b/src/Lazy/LazyUuidFromString.php @@ -24,10 +24,12 @@ use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Integer as IntegerObject; use Ramsey\Uuid\UuidFactory; use Ramsey\Uuid\UuidInterface; +use ValueError; use function assert; use function bin2hex; use function hex2bin; +use function sprintf; use function str_replace; use function substr; @@ -90,6 +92,16 @@ final class LazyUuidFromString implements UuidInterface return $this->uuid; } + /** + * @return array{string: string} + * + * @psalm-return array{string: non-empty-string} + */ + public function __serialize(): array + { + return ['string' => $this->uuid]; + } + /** * {@inheritDoc} * @@ -102,6 +114,20 @@ final class LazyUuidFromString implements UuidInterface $this->uuid = $serialized; } + /** + * @param array{string: string} $data + * + * @psalm-param array{string: non-empty-string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['string'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->unserialize($data['string']); + } + /** @psalm-suppress DeprecatedMethod */ public function getNumberConverter(): NumberConverterInterface { diff --git a/src/Type/Decimal.php b/src/Type/Decimal.php index 042fc16..6eb3623 100644 --- a/src/Type/Decimal.php +++ b/src/Type/Decimal.php @@ -15,8 +15,10 @@ declare(strict_types=1); namespace Ramsey\Uuid\Type; use Ramsey\Uuid\Exception\InvalidArgumentException; +use ValueError; use function is_numeric; +use function sprintf; /** * A value object representing a decimal @@ -98,6 +100,14 @@ final class Decimal implements NumberInterface return $this->toString(); } + /** + * @return array{string: string} + */ + public function __serialize(): array + { + return ['string' => $this->toString()]; + } + /** * Constructs the object from a serialized string representation * @@ -110,4 +120,16 @@ final class Decimal implements NumberInterface { $this->__construct($serialized); } + + /** + * @param array{string: string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['string'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->unserialize($data['string']); + } } diff --git a/src/Type/Hexadecimal.php b/src/Type/Hexadecimal.php index 7de2f82..57cb631 100644 --- a/src/Type/Hexadecimal.php +++ b/src/Type/Hexadecimal.php @@ -15,8 +15,10 @@ declare(strict_types=1); namespace Ramsey\Uuid\Type; use Ramsey\Uuid\Exception\InvalidArgumentException; +use ValueError; use function ctype_xdigit; +use function sprintf; use function strpos; use function strtolower; use function substr; @@ -77,6 +79,14 @@ final class Hexadecimal implements TypeInterface return $this->toString(); } + /** + * @return array{string: string} + */ + public function __serialize(): array + { + return ['string' => $this->toString()]; + } + /** * Constructs the object from a serialized string representation * @@ -89,4 +99,16 @@ final class Hexadecimal implements TypeInterface { $this->__construct($serialized); } + + /** + * @param array{string: string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['string'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->unserialize($data['string']); + } } diff --git a/src/Type/Integer.php b/src/Type/Integer.php index fabe5eb..42a471d 100644 --- a/src/Type/Integer.php +++ b/src/Type/Integer.php @@ -15,9 +15,11 @@ declare(strict_types=1); namespace Ramsey\Uuid\Type; use Ramsey\Uuid\Exception\InvalidArgumentException; +use ValueError; use function ctype_digit; use function ltrim; +use function sprintf; use function strpos; use function substr; @@ -114,6 +116,14 @@ final class Integer implements NumberInterface return $this->toString(); } + /** + * @return array{string: string} + */ + public function __serialize(): array + { + return ['string' => $this->toString()]; + } + /** * Constructs the object from a serialized string representation * @@ -126,4 +136,16 @@ final class Integer implements NumberInterface { $this->__construct($serialized); } + + /** + * @param array{string: string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['string'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->unserialize($data['string']); + } } diff --git a/src/Type/Time.php b/src/Type/Time.php index abceddf..55f0358 100644 --- a/src/Type/Time.php +++ b/src/Type/Time.php @@ -16,10 +16,12 @@ namespace Ramsey\Uuid\Type; use Ramsey\Uuid\Exception\UnsupportedOperationException; use Ramsey\Uuid\Type\Integer as IntegerObject; +use ValueError; use stdClass; use function json_decode; use function json_encode; +use function sprintf; /** * A value object representing a timestamp @@ -88,6 +90,17 @@ final class Time implements TypeInterface return (string) json_encode($this); } + /** + * @return array{seconds: string, microseconds: string} + */ + public function __serialize(): array + { + return [ + 'seconds' => $this->getSeconds()->toString(), + 'microseconds' => $this->getMicroseconds()->toString(), + ]; + } + /** * Constructs the object from a serialized string representation * @@ -109,4 +122,16 @@ final class Time implements TypeInterface $this->__construct($time->seconds, $time->microseconds); } + + /** + * @param array{seconds: string, microseconds: string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['seconds']) || !isset($data['microseconds'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->__construct($data['seconds'], $data['microseconds']); + } } diff --git a/src/Uuid.php b/src/Uuid.php index d80fd57..f2dab13 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -23,10 +23,12 @@ use Ramsey\Uuid\Lazy\LazyUuidFromString; use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Integer as IntegerObject; +use ValueError; use function assert; use function bin2hex; use function preg_match; +use function sprintf; use function str_replace; use function strcmp; use function strlen; @@ -289,6 +291,14 @@ class Uuid implements UuidInterface return $this->getFields()->getBytes(); } + /** + * @return array{bytes: string} + */ + public function __serialize(): array + { + return ['bytes' => $this->serialize()]; + } + /** * Re-constructs the object from its serialized form * @@ -313,6 +323,18 @@ class Uuid implements UuidInterface $this->timeConverter = $uuid->timeConverter; } + /** + * @param array{bytes: string} $data + */ + public function __unserialize(array $data): void + { + if (!isset($data['bytes'])) { + throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); + } + + $this->unserialize($data['bytes']); + } + public function compareTo(UuidInterface $other): int { $compare = strcmp($this->toString(), $other->toString());