From b0fdd5e850f13fadb66750b4fbabea7c148d51ea Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Thu, 10 Aug 2023 00:45:39 +0200 Subject: [PATCH] Remove Serializable See: - https://wiki.php.net/rfc/custom_object_serialization - https://wiki.php.net/rfc/phase_out_serializable --- CHANGELOG.md | 1 + src/BigDecimal.php | 30 ------------------------------ src/BigInteger.php | 27 --------------------------- src/BigNumber.php | 2 +- src/BigRational.php | 30 ------------------------------ tests/BigDecimalTest.php | 2 +- tests/BigIntegerTest.php | 2 +- tests/BigRationalTest.php | 2 +- 8 files changed, 5 insertions(+), 91 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 324fc10..27dfef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - Minimum PHP version is now 8.1 - `RoundingMode` is now an `enum`; if you're type-hinting rounding modes, you need to type-hint against `RoundingMode` instead of `int` now +- `BigNumber` classes do not implement the `Serializable` interface anymore (they use the [new custom object serialization mechanism](https://wiki.php.net/rfc/custom_object_serialization)) ## [0.11.0](https://github.com/brick/math/releases/tag/0.11.0) - 2023-01-16 diff --git a/src/BigDecimal.php b/src/BigDecimal.php index a01a2f2..551c698 100644 --- a/src/BigDecimal.php +++ b/src/BigDecimal.php @@ -693,36 +693,6 @@ public function __unserialize(array $data): void $this->scale = $data['scale']; } - /** - * This method is required by interface Serializable and SHOULD NOT be accessed directly. - * - * @internal - */ - public function serialize() : string - { - return $this->value . ':' . $this->scale; - } - - /** - * This method is only here to implement interface Serializable and cannot be accessed directly. - * - * @internal - * @psalm-suppress RedundantPropertyInitializationCheck - * - * @throws \LogicException - */ - public function unserialize($value) : void - { - if (isset($this->value)) { - throw new \LogicException('unserialize() is an internal function, it must not be called directly.'); - } - - [$value, $scale] = \explode(':', $value); - - $this->value = $value; - $this->scale = (int) $scale; - } - /** * Puts the internal values of the given decimal numbers on the same scale. * diff --git a/src/BigInteger.php b/src/BigInteger.php index e173d77..c39335b 100644 --- a/src/BigInteger.php +++ b/src/BigInteger.php @@ -1050,31 +1050,4 @@ public function __unserialize(array $data): void $this->value = $data['value']; } - - /** - * This method is required by interface Serializable and SHOULD NOT be accessed directly. - * - * @internal - */ - public function serialize() : string - { - return $this->value; - } - - /** - * This method is only here to implement interface Serializable and cannot be accessed directly. - * - * @internal - * @psalm-suppress RedundantPropertyInitializationCheck - * - * @throws \LogicException - */ - public function unserialize($value) : void - { - if (isset($this->value)) { - throw new \LogicException('unserialize() is an internal function, it must not be called directly.'); - } - - $this->value = $value; - } } diff --git a/src/BigNumber.php b/src/BigNumber.php index 034e886..015823a 100644 --- a/src/BigNumber.php +++ b/src/BigNumber.php @@ -14,7 +14,7 @@ * * @psalm-immutable */ -abstract class BigNumber implements \Serializable, \JsonSerializable +abstract class BigNumber implements \JsonSerializable { /** * The regular expression used to parse integer, decimal and rational numbers. diff --git a/src/BigRational.php b/src/BigRational.php index 2eaec50..65caeb0 100644 --- a/src/BigRational.php +++ b/src/BigRational.php @@ -412,34 +412,4 @@ public function __unserialize(array $data): void $this->numerator = $data['numerator']; $this->denominator = $data['denominator']; } - - /** - * This method is required by interface Serializable and SHOULD NOT be accessed directly. - * - * @internal - */ - public function serialize() : string - { - return $this->numerator . '/' . $this->denominator; - } - - /** - * This method is only here to implement interface Serializable and cannot be accessed directly. - * - * @internal - * @psalm-suppress RedundantPropertyInitializationCheck - * - * @throws \LogicException - */ - public function unserialize($value) : void - { - if (isset($this->numerator)) { - throw new \LogicException('unserialize() is an internal function, it must not be called directly.'); - } - - [$numerator, $denominator] = \explode('/', $value); - - $this->numerator = BigInteger::of($numerator); - $this->denominator = BigInteger::of($denominator); - } } diff --git a/tests/BigDecimalTest.php b/tests/BigDecimalTest.php index 261c874..ef6ba27 100644 --- a/tests/BigDecimalTest.php +++ b/tests/BigDecimalTest.php @@ -2585,6 +2585,6 @@ public function testSerialize() : void public function testDirectCallToUnserialize() : void { $this->expectException(\LogicException::class); - BigDecimal::zero()->unserialize('123:0'); + BigDecimal::zero()->__unserialize([]); } } diff --git a/tests/BigIntegerTest.php b/tests/BigIntegerTest.php index 87184a8..ca67514 100644 --- a/tests/BigIntegerTest.php +++ b/tests/BigIntegerTest.php @@ -3762,7 +3762,7 @@ public function testSerialize() : void public function testDirectCallToUnserialize() : void { $this->expectException(\LogicException::class); - BigInteger::zero()->unserialize('123'); + BigInteger::zero()->__unserialize([]); } public function testJsonSerialize() : void diff --git a/tests/BigRationalTest.php b/tests/BigRationalTest.php index db2c3ef..8e39d0a 100644 --- a/tests/BigRationalTest.php +++ b/tests/BigRationalTest.php @@ -983,6 +983,6 @@ public function testSerialize() : void public function testDirectCallToUnserialize() : void { $this->expectException(\LogicException::class); - BigRational::nd(1, 2)->unserialize('123/456'); + BigRational::nd(1, 2)->__unserialize([]); } }