• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NexusPHP / framework / 15768680547

27 May 2025 01:19PM UTC coverage: 100.0%. Remained the same
15768680547

push

github

paulbalandan
Add clock extension API for writing time-sensitive classes

30 of 30 new or added lines in 4 files covered. (100.0%)

988 of 988 relevant lines covered (100.0%)

7.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Nexus/Clock/InternalClock.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of the Nexus framework.
7
 *
8
 * (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Nexus\Clock;
15

16
/**
17
 * A reference to the internal clock.
18
 *
19
 * This is primarily useful for mocking the clock in tests.
20
 * Classes should not use this class directly, but instead
21
 * use the `Clock` interface or the two traits `ClockAware`
22
 * and `ImmutableClockAware`.
23
 *
24
 * @internal
25
 */
26
final class InternalClock implements Clock
27
{
28
    private static ?Clock $internalClock;
29

30
    public function __construct(
31
        private readonly ?Clock $innerClock = null,
32
        private ?\DateTimeZone $timezone = null,
33
    ) {}
8✔
34

35
    /**
36
     * Returns the current internal clock.
37
     *
38
     * If no internal clock has been set, a new instance of SystemClock
39
     * will be created with the UTC timezone.
40
     */
41
    public static function getCurrent(): Clock
42
    {
43
        self::$internalClock ??= new SystemClock('UTC');
14✔
44

45
        return self::$internalClock;
14✔
46
    }
47

48
    /**
49
     * Sets the internal clock.
50
     */
51
    public static function set(?Clock $clock): void
52
    {
53
        self::$internalClock = $clock;
14✔
54
    }
55

56
    public function now(): \DateTimeImmutable
57
    {
58
        $now = ($this->innerClock ?? self::getCurrent())->now();
8✔
59

60
        if (null !== $this->timezone) {
8✔
61
            $now = $now->setTimezone($this->timezone);
4✔
62
        }
63

64
        return $now;
8✔
65
    }
66

67
    public function sleep(float|int $seconds): void
68
    {
69
        ($this->innerClock ?? self::getCurrent())->sleep($seconds);
8✔
70
    }
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc