Passed
Push — 0.6.x ( 9a1948...a98caa )
by Shinji
03:23 queued 01:32
created

NanoSleepMiddlewareAsync   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 12 2
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Reli\Lib\Loop\AsyncLoopMiddleware;
15
16
use Reli\Lib\Loop\AsyncLoopMiddlewareInterface;
17
18
use function hrtime;
19
use function time_nanosleep;
20
21
final class NanoSleepMiddlewareAsync implements AsyncLoopMiddlewareInterface
22
{
23
    /** @param positive-int $sleep_nano_seconds */
0 ignored issues
show
Documentation Bug introduced by
The doc comment positive-int at position 0 could not be parsed: Unknown type name 'positive-int' at position 0 in positive-int.
Loading history...
24
    public function __construct(
25
        private int $sleep_nano_seconds,
26
        private AsyncLoopMiddlewareInterface $chain
27
    ) {
28
    }
29
30
    public function invoke(): \Generator
31
    {
32
        $start = hrtime(true);
33
        yield from $this->chain->invoke();
34
35
        $wait = $this->sleep_nano_seconds - (hrtime(true) - $start);
36
        if ($wait > 0) {
37
            /**
38
             * @psalm-suppress UnusedFunctionCall
39
             * @psalm-suppress InvalidArgument
40
             */
41
            time_nanosleep(0, $wait);
0 ignored issues
show
Bug introduced by
$wait of type double is incompatible with the type integer expected by parameter $nanoseconds of time_nanosleep(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            time_nanosleep(0, /** @scrutinizer ignore-type */ $wait);
Loading history...
42
        }
43
    }
44
}
45