for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* This file is part of the reliforp/reli-prof package.
*
* (c) sji <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Reli\Lib\Loop\LoopMiddleware;
use Reli\Lib\Loop\LoopMiddlewareInterface;
use function hrtime;
use function time_nanosleep;
final class NanoSleepMiddleware implements LoopMiddlewareInterface
{
/** @param positive-int $sleep_nano_seconds */
positive-int
0
public function __construct(
private int $sleep_nano_seconds,
private LoopMiddlewareInterface $chain,
) {
}
public function invoke(): bool
$start = hrtime(true);
if (!$this->chain->invoke()) {
return false;
$wait = $this->sleep_nano_seconds - (hrtime(true) - $start);
if ($wait > 0) {
* @psalm-suppress UnusedFunctionCall
* @psalm-suppress InvalidArgument
time_nanosleep(0, $wait);
$wait
double
integer
$nanoseconds
time_nanosleep()
If this is a false-positive, you can also ignore this issue in your code via the ignore-type annotation
ignore-type
time_nanosleep(0, /** @scrutinizer ignore-type */ $wait);
return true;