1
0
mirror of https://github.com/danog/math.git synced 2024-11-27 04:14:40 +01:00

unserialize() now throws an exception when called directly

This guarantees that immutability cannot be broken.
This commit is contained in:
Benjamin Morel 2015-06-04 12:14:42 +02:00
parent 53a61b46a7
commit 908e73e8e4
4 changed files with 26 additions and 2 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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');
}
}

View File

@ -1577,4 +1577,12 @@ class BigIntegerTest extends AbstractTestCase
$this->assertBigIntegerEquals($value, unserialize(serialize($number)));
}
/**
* @expectedException \LogicException
*/
public function testDirectCallToUnserialize()
{
BigInteger::zero()->unserialize('123');
}
}