diff --git a/src/BigDecimal.php b/src/BigDecimal.php index 45e3cf8..ecf1336 100644 --- a/src/BigDecimal.php +++ b/src/BigDecimal.php @@ -670,6 +670,10 @@ class BigDecimal implements \Serializable */ public function unserialize($value) { + if ($this->value !== null || $this->scale !== null) { + throw new \LogicException('unserialize() is an internal function, it must not be called directly.'); + } + list ($value, $scale) = explode(':', $value); $this->value = $value; diff --git a/src/BigInteger.php b/src/BigInteger.php index 683e345..14bc6a2 100644 --- a/src/BigInteger.php +++ b/src/BigInteger.php @@ -562,16 +562,20 @@ class BigInteger implements \Serializable /** * This method is required by interface Serializable and MUST NOT be accessed directly. * - * Accessing this method directly would bypass consistency checks and break immutability. - * * @internal * * @param string $value * * @return void + * + * @throws \LogicException */ public function unserialize($value) { + if ($this->value !== null) { + throw new \LogicException('unserialize() is an internal function, it must not be called directly.'); + } + $this->value = $value; } } diff --git a/tests/BigDecimalTest.php b/tests/BigDecimalTest.php index c112507..0e02c04 100644 --- a/tests/BigDecimalTest.php +++ b/tests/BigDecimalTest.php @@ -1956,4 +1956,12 @@ class BigDecimalTest extends AbstractTestCase $this->assertBigDecimalEquals($value, $scale, unserialize(serialize($number))); } + + /** + * @expectedException \LogicException + */ + public function testDirectCallToUnserialize() + { + BigDecimal::zero()->unserialize('123:0'); + } } diff --git a/tests/BigIntegerTest.php b/tests/BigIntegerTest.php index 6e6cac0..4c0a1b7 100644 --- a/tests/BigIntegerTest.php +++ b/tests/BigIntegerTest.php @@ -1577,4 +1577,12 @@ class BigIntegerTest extends AbstractTestCase $this->assertBigIntegerEquals($value, unserialize(serialize($number))); } + + /** + * @expectedException \LogicException + */ + public function testDirectCallToUnserialize() + { + BigInteger::zero()->unserialize('123'); + } }