Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MonotonicClock | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| now | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Phexium\Plugin\Clock\Adapter; |
| 11 | |
| 12 | use DateTimeImmutable; |
| 13 | use Override; |
| 14 | use Phexium\Plugin\Clock\Port\ClockInterface; |
| 15 | |
| 16 | final class MonotonicClock implements ClockInterface |
| 17 | { |
| 18 | private ?DateTimeImmutable $lastTime = null; |
| 19 | |
| 20 | public function __construct( |
| 21 | private readonly ClockInterface $inner, |
| 22 | ) {} |
| 23 | |
| 24 | #[Override] |
| 25 | public function now(): DateTimeImmutable |
| 26 | { |
| 27 | $current = $this->inner->now(); |
| 28 | |
| 29 | if ($this->lastTime instanceof DateTimeImmutable && $current <= $this->lastTime) { |
| 30 | $this->lastTime = $this->lastTime->modify('+1 microsecond'); |
| 31 | } else { |
| 32 | $this->lastTime = $current; |
| 33 | } |
| 34 | |
| 35 | return $this->lastTime; |
| 36 | } |
| 37 | } |