1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Timers; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Timers\Core\AbstractTimer; |
6
|
|
|
use const AlecRabbit\Helpers\Constants\INT_SIZE_64BIT; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* HRTimer class |
10
|
|
|
* Designed to for php version 7.3 and above. |
11
|
|
|
* |
12
|
|
|
* @package AlecRabbit\Timers |
13
|
|
|
*/ |
14
|
|
|
class HRTimer extends AbstractTimer |
15
|
|
|
{ |
16
|
|
|
public const VALUE_COEFFICIENT = HRTIMER_VALUE_COEFFICIENT; |
17
|
|
|
|
18
|
|
|
/** @var bool */ |
19
|
|
|
public static $ignoreVersionRestrictions = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return int |
23
|
|
|
*/ |
24
|
16 |
|
public function current(): int |
25
|
|
|
{ |
26
|
|
|
return |
27
|
16 |
|
(int)hrtime(true); |
28
|
|
|
} |
29
|
|
|
|
30
|
20 |
|
protected function checkEnvironment(): void |
31
|
|
|
{ |
32
|
|
|
// @codeCoverageIgnoreStart |
33
|
|
|
if (PHP_VERSION_ID < 70300 && false === static::$ignoreVersionRestrictions) { |
34
|
|
|
// `HRTimer::class` uses `hrtime()` function of PHP ^7.3. |
35
|
|
|
// There is almost no sense to use polyfill function. |
36
|
|
|
// If you're REALLY need to use HRTimer set `$ignoreVersionRestrictions` to true. |
37
|
|
|
// Otherwise use `Timer::class` instance instead. |
38
|
|
|
throw new \RuntimeException('[' . static::class . '] Your php version is below 7.3.0.'); |
39
|
|
|
} |
40
|
|
|
if (PHP_INT_SIZE < INT_SIZE_64BIT) { |
41
|
|
|
// `HRTimer::class` is designed and tested in 64bit environment |
42
|
|
|
// So it can be used in 64bit environments only |
43
|
|
|
// Maybe with some minor modification it can run on 32bit installations too |
44
|
|
|
throw new \RuntimeException(' You\'re using 32bit php installation.'); |
45
|
|
|
} |
46
|
|
|
// @codeCoverageIgnoreEnd |
47
|
20 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param int $start |
51
|
|
|
* @param int $stop |
52
|
|
|
*/ |
53
|
5 |
|
protected function assertStartAndStop($start, $stop): void |
54
|
|
|
{ |
55
|
5 |
|
$this->assertStart($start); |
56
|
4 |
|
$this->assertStop($stop); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $start |
61
|
|
|
*/ |
62
|
4 |
|
protected function assertStart(/** @scrutinizer ignore-unused */ int $start): void |
63
|
|
|
{ |
64
|
|
|
// Intentionally left blank |
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $stop |
69
|
|
|
*/ |
70
|
3 |
|
protected function assertStop(/** @scrutinizer ignore-unused */ int $stop): void |
71
|
|
|
{ |
72
|
|
|
// Intentionally left blank |
73
|
3 |
|
} |
74
|
|
|
} |
75
|
|
|
|