Skip to content

Commit 146c135

Browse files
committed
disable cloning and serialization
1 parent b75dd79 commit 146c135

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/AnyTime.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ public function inNanoseconds(): Nanoseconds
8787

8888
abstract protected function toNanoseconds(): float|int;
8989

90+
public function __clone(): void
91+
{
92+
throw new \LogicException('Please use from() to clone.');
93+
}
94+
95+
public function __wakeup(): void
96+
{
97+
throw new \LogicException('Please use from() to deserialize');
98+
}
99+
100+
public function __sleep(): array
101+
{
102+
throw new \LogicException('Time cannot be serialized.');
103+
}
104+
90105
public function add(AnyTime $time): AnyTime
91106
{
92107
if ($time->spacetime !== $this->spacetime) {

tests/Unit/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Unit/TimeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
$secondInMs = Milliseconds::fromValue(1000);
3737
$second = Seconds::from($secondInMs);
3838
expect($second)->toBe(Seconds::fromValue(1));
39+
expect($second)->toEqual(Seconds::fromValue(1));
3940
});
4041

4142
test("Date intervals work", function () {
@@ -50,3 +51,13 @@
5051

5152
expect($now->add($var->toDateInterval()))->toEqual($expected);
5253
});
54+
55+
it('cannot be serialized', function () {
56+
$var = Seconds(1);
57+
expect(fn() => serialize($var))->toThrow(LogicException::class);
58+
});
59+
60+
it('cannot be cloned', function () {
61+
$var = Seconds(1);
62+
expect(fn() => clone $var)->toThrow(LogicException::class);
63+
});

0 commit comments

Comments
 (0)