FutureToPromiseConverter::convert()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Parallel;
4
5
use parallel\Future;
6
use React\EventLoop\LoopInterface;
7
use React\EventLoop\TimerInterface;
8
use React\Promise\Promise;
9
use React\Promise\PromiseInterface;
10
11
final class FutureToPromiseConverter
12
{
13
    /** @var LoopInterface */
14
    private $loop;
15
16
    public function __construct(LoopInterface $loop)
17
    {
18
        $this->loop = $loop;
19
    }
20
21
    public function convert(Future $future): PromiseInterface
22
    {
23
        return new Promise(function ($resolve, $reject) use ($future): void {
24
            /** @var TimerInterface|null $timer */
25
            $timer = $this->loop->addPeriodicTimer(0.001, function () use (&$timer, $future, $resolve, $reject): void {
0 ignored issues
show
Unused Code introduced by
$timer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
                if (!$future->done()) {
27
                    return;
28
                }
29
30
                if ($timer instanceof TimerInterface) {
31
                    $this->loop->cancelTimer($timer);
32
                }
33
34
                try {
35
                    $resolve($future->value());
36
                } catch (\Throwable $throwable) {
37
                    $reject($throwable);
38
                }
39
            });
40
        });
41
    }
42
}
43