Skip to content
This repository was archived by the owner on Jan 8, 2022. It is now read-only.

Commit adaed6a

Browse files
author
Charlotte Dunois
committed
Wrap into checks
1 parent 573af85 commit adaed6a

File tree

8 files changed

+172
-165
lines changed

8 files changed

+172
-165
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
vendor
3-
composer.lock
3+
composer.lock
4+
coverage.clover.xml

bootstrap.php

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

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"autoload": {
1717
"files": [
18-
"bootstrap.php"
18+
"src/bootstrap.php"
1919
]
2020
},
2121
"autoload-dev": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
</filter>
1717
<logging>
1818
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" />
19-
<log type="coverage-clover" target="./coverage.clover.xml"/>
19+
<log type="coverage-clover" target="./coverage.clover.xml" />
2020
</logging>
2121
</phpunit>

src/bootstrap.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Obsidian
4+
* Copyright 2020 ObsidianPHP, All Rights Reserved
5+
*
6+
* License: https://github.com/ObsidianPHP/polyfill-hrtime/blob/master/LICENSE
7+
*/
8+
9+
if(!function_exists('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback\\hrtime_fallback')) {
10+
require 'functions.php';
11+
}
12+
13+
if(function_exists('hrtime')) {
14+
return;
15+
}
16+
17+
use function Obsidian\Polyfill\Hrtime\hrtime_ext_uv;
18+
use function Obsidian\Polyfill\Hrtime\hrtime_ext_hrtime;
19+
use function Obsidian\Polyfill\Hrtime\hrtime_fallback;
20+
21+
if(function_exists('uv_hrtime')) {
22+
function hrtime(bool $get_as_number = false) {
23+
return hrtime_ext_uv($get_as_number);
24+
}
25+
} elseif(extension_loaded('hrtime')) {
26+
function hrtime(bool $get_as_number = false) {
27+
return hrtime_ext_hrtime($get_as_number);
28+
}
29+
30+
hrtime();
31+
} else {
32+
function hrtime(bool $get_as_number = false) {
33+
return hrtime_fallback($get_as_number);
34+
}
35+
36+
hrtime();
37+
}

src/functions.php

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,61 @@
1313
use HrTime\Unit;
1414

1515
function internal_conversion_nanoseconds_to_seconds(int $nanoseconds): array {
16-
$seconds = \intdiv($nanoseconds, 1e9);
17-
$nanoseconds -= (int) ($seconds * 1e9);
18-
19-
return array($seconds, $nanoseconds);
16+
$seconds = \intdiv($nanoseconds, 1e9);
17+
$nanoseconds -= (int) ($seconds * 1e9);
18+
19+
return array($seconds, $nanoseconds);
2020
}
2121

22-
function hrtime_ext_uv(bool $get_as_number = false) {
23-
$nanoseconds = \uv_hrtime();
24-
25-
if($get_as_number) {
26-
return $nanoseconds;
27-
}
28-
29-
return internal_conversion_nanoseconds_to_seconds($nanoseconds);
22+
if(function_exists('uv_hrtime')) {
23+
function hrtime_ext_uv(bool $get_as_number = false) {
24+
$nanoseconds = \uv_hrtime();
25+
26+
if($get_as_number) {
27+
return $nanoseconds;
28+
}
29+
30+
return internal_conversion_nanoseconds_to_seconds($nanoseconds);
31+
}
3032
}
3133

32-
function hrtime_ext_hrtime(bool $get_as_number = false) {
33-
/** @var StopWatch $timer */
34-
static $timer;
35-
36-
if(!$timer) {
37-
$timer = new StopWatch();
38-
$timer->start();
39-
}
40-
41-
$timer->stop();
42-
$timer->start();
43-
44-
$nanoseconds = (int) $timer->getElapsedTime(Unit::NANOSECOND);
45-
46-
if($get_as_number) {
47-
return $nanoseconds;
48-
}
49-
50-
return internal_conversion_nanoseconds_to_seconds($nanoseconds);
34+
if(extension_loaded('hrtime')) {
35+
function hrtime_ext_hrtime(bool $get_as_number = false) {
36+
/** @var StopWatch $timer */
37+
static $timer;
38+
39+
if(!$timer) {
40+
$timer = new StopWatch();
41+
$timer->start();
42+
}
43+
44+
$timer->stop();
45+
$timer->start();
46+
47+
$nanoseconds = (int) $timer->getElapsedTime(Unit::NANOSECOND);
48+
49+
if($get_as_number) {
50+
return $nanoseconds;
51+
}
52+
53+
return internal_conversion_nanoseconds_to_seconds($nanoseconds);
54+
}
5155
}
5256

5357
function hrtime_fallback(bool $get_as_number = false) {
54-
/** @var int $timer */
55-
static $timer;
56-
57-
if(!$timer) {
58-
$timer = (int) (\microtime(true) * 1e6);
59-
}
60-
61-
$now = (int) (\microtime(true) * 1e6);
62-
$nanoseconds = (int) ($now - $timer);
63-
64-
if($get_as_number) {
65-
return $nanoseconds;
66-
}
67-
68-
return internal_conversion_nanoseconds_to_seconds($nanoseconds);
58+
/** @var int $timer */
59+
static $timer;
60+
61+
if(!$timer) {
62+
$timer = (int) (\microtime(true) * 1e6);
63+
}
64+
65+
$now = (int) (\microtime(true) * 1e6);
66+
$nanoseconds = (int) ($now - $timer);
67+
68+
if($get_as_number) {
69+
return $nanoseconds;
70+
}
71+
72+
return internal_conversion_nanoseconds_to_seconds($nanoseconds);
6973
}

tests/HrtimeLoadingTest.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@
1111
use PHPUnit\Framework\TestCase;
1212

1313
class HrtimeLoadingTest extends TestCase {
14-
function testLoading() {
15-
if(\PHP_VERSION_ID >= 70300) {
16-
$this->markTestSkipped('Test unnecessary, hrtime natively available');
17-
}
18-
19-
\xdebug_start_function_monitor(array(
20-
'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv',
21-
'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime',
22-
'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback'
23-
));
24-
25-
\hrtime();
26-
27-
[ [ 'function' => $function ] ] = \xdebug_get_monitored_functions();
28-
\xdebug_stop_function_monitor();
29-
30-
switch(\getenv('EXT_INSTALL', true)) {
31-
case 'all':
32-
case 'uv':
33-
$fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv';
34-
break;
35-
case 'hrtime':
36-
$fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime';
37-
break;
38-
case 'none':
39-
$fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback';
40-
break;
41-
default:
42-
throw new \RuntimeException('Unknown "EXT_INSTALL" env var value');
43-
}
44-
45-
$this->assertSame($fun, $function);
46-
}
14+
function testLoading() {
15+
if(\PHP_VERSION_ID >= 70300) {
16+
$this->markTestSkipped('Test unnecessary, hrtime natively available');
17+
}
18+
19+
\xdebug_start_function_monitor(array(
20+
'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv',
21+
'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime',
22+
'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback'
23+
));
24+
25+
\hrtime();
26+
27+
[ [ 'function' => $function ] ] = \xdebug_get_monitored_functions();
28+
\xdebug_stop_function_monitor();
29+
30+
switch(\getenv('EXT_INSTALL', true)) {
31+
case 'all':
32+
case 'uv':
33+
$fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv';
34+
break;
35+
case 'hrtime':
36+
$fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime';
37+
break;
38+
case 'none':
39+
$fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback';
40+
break;
41+
default:
42+
throw new \RuntimeException('Unknown "EXT_INSTALL" env var value');
43+
}
44+
45+
$this->assertSame($fun, $function);
46+
}
4747
}

tests/HrtimeTest.php

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,52 @@
1515
* @requires extension uv
1616
*/
1717
class HrtimeTest extends TestCase {
18-
function providerTestFunctions() {
19-
return array(
20-
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', false),
21-
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', false),
22-
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback', true)
23-
);
24-
}
25-
26-
/**
27-
* @dataProvider providerTestFunctions
28-
* @param callable $function
29-
* @param bool $sleep
30-
*/
31-
function testHrtimeAsNumber(callable $function, bool $sleep) {
32-
$number = $function(true);
33-
$this->assertIsInt($number);
34-
35-
if($sleep) {
36-
\sleep(1);
37-
}
38-
39-
$number2 = $function(true);
40-
$this->assertIsInt($number2);
41-
42-
$this->assertGreaterThan($number, $number2);
43-
}
44-
45-
/**
46-
* @dataProvider providerTestFunctions
47-
* @param callable $function
48-
* @param bool $sleep
49-
*/
50-
function testHrtimeAsArray(callable $function, bool $sleep) {
51-
$arr = $function(false);
52-
$this->assertIsArray($arr);
53-
$this->assertCount(2, $arr);
54-
55-
if($sleep) {
56-
\sleep(1);
57-
}
58-
59-
$arr2 = $function(false);
60-
$this->assertIsArray($arr2);
61-
$this->assertCount(2, $arr2);
62-
63-
$this->assertSame($arr[0], $arr2[0]);
64-
$this->assertGreaterThan($arr[1], $arr2[1]);
65-
}
18+
function providerTestFunctions() {
19+
return array(
20+
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', false),
21+
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', false),
22+
array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback', true)
23+
);
24+
}
25+
26+
/**
27+
* @dataProvider providerTestFunctions
28+
* @param callable $function
29+
* @param bool $sleep
30+
*/
31+
function testHrtimeAsNumber(callable $function, bool $sleep) {
32+
$number = $function(true);
33+
$this->assertIsInt($number);
34+
35+
if($sleep) {
36+
\sleep(1);
37+
}
38+
39+
$number2 = $function(true);
40+
$this->assertIsInt($number2);
41+
42+
$this->assertGreaterThan($number, $number2);
43+
}
44+
45+
/**
46+
* @dataProvider providerTestFunctions
47+
* @param callable $function
48+
* @param bool $sleep
49+
*/
50+
function testHrtimeAsArray(callable $function, bool $sleep) {
51+
$arr = $function(false);
52+
$this->assertIsArray($arr);
53+
$this->assertCount(2, $arr);
54+
55+
if($sleep) {
56+
\sleep(1);
57+
}
58+
59+
$arr2 = $function(false);
60+
$this->assertIsArray($arr2);
61+
$this->assertCount(2, $arr2);
62+
63+
$this->assertSame($arr[0], $arr2[0]);
64+
$this->assertGreaterThan($arr[1], $arr2[1]);
65+
}
6666
}

0 commit comments

Comments
 (0)