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

NanoSleepMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 15 3
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\LoopMiddleware;
15
16
use Reli\Lib\Loop\LoopMiddlewareInterface;
17
18
use function hrtime;
19
use function time_nanosleep;
20
21
final class NanoSleepMiddleware implements LoopMiddlewareInterface
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 LoopMiddlewareInterface $chain,
27
    ) {
28
    }
29
30
    public function invoke(): bool
31
    {
32
        $start = hrtime(true);
33
        if (!$this->chain->invoke()) {
34
            return false;
35
        }
36
        $wait = $this->sleep_nano_seconds - (hrtime(true) - $start);
37
        if ($wait > 0) {
38
            /**
39
             * @psalm-suppress UnusedFunctionCall
40
             * @psalm-suppress InvalidArgument
41
             */
42
            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

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