Completed
Push — develop ( a410d7...efad38 )
by Alec
04:48 queued 01:47
created

HRTimer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 54
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkEnvironment() 0 15 4
A assertStartAndStop() 0 15 5
A current() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use const AlecRabbit\Helpers\Constants\INT_SIZE_64BIT;
6
use function AlecRabbit\typeOf;
7
8
class HRTimer extends AbstractTimer
9
{
10
    public const VALUE_COEFFICIENT = HRTIMER_VALUE_COEFFICIENT;
11
12
    /** @var bool */
13
    public static $ignoreVersionRestrictions = false;
14
15
    /**
16
     * @return int
17
     */
18 14
    public function current(): int
19
    {
20
        return
21 14
            (int)hrtime(true);
22
    }
23
24 18
    protected function checkEnvironment(): void
25
    {
26 18
        if (PHP_VERSION_ID < 70300 && false === static::$ignoreVersionRestrictions) {
27
            // `HRTimer::class` uses `hrtime()` function of PHP ^7.3.
28
            // There is almost no sense in using polyfill function.
29
            // If you're REALLY need to use HRTimer set `$ignoreVersionRestrictions` to true.
30
            // Otherwise use `Timer::class` instance instead.
31
            throw new \RuntimeException('[' . static::class . '] Your php version is below 7.3.0.');
32
        }
33
        // @codeCoverageIgnoreStart
34
        if (PHP_INT_SIZE < INT_SIZE_64BIT) {
35
            // `HRTimer::class` is designed and tested in 64bit environment
36
            // So it can be used in 64bit environments only
37
            // Maybe with some minor modification it can run on 32bit installations too
38
            throw new \RuntimeException(' You\'re using 32bit php installation.');
39
        }
40
        // @codeCoverageIgnoreEnd
41 18
    }
42
43
    /**
44
     * @param int $start
45
     * @param int $stop
46
     */
47 3
    protected function assertStartAndStop($start, $stop): void
48
    {
49 3
        $start_ok = false;
50 3
        $stop_ok = false;
51 3
        if (is_int($start)) {
0 ignored issues
show
introduced by
The condition is_int($start) is always true.
Loading history...
52 3
            $start_ok = true;
53
        }
54 3
        if (is_int($stop)) {
0 ignored issues
show
introduced by
The condition is_int($stop) is always true.
Loading history...
55 3
            $stop_ok = true;
56
        }
57 3
        if (!$start_ok) {
0 ignored issues
show
introduced by
The condition $start_ok is always true.
Loading history...
58
            throw new \RuntimeException('Start value is NOT ok. [' . typeOf($start) . ']');
59
        }
60 3
        if (!$stop_ok) {
0 ignored issues
show
introduced by
The condition $stop_ok is always true.
Loading history...
61
            throw new \RuntimeException('Stop value is NOT ok. [' . typeOf($stop) . ']');
62
        }
63 3
    }
64
65
}
66