Completed
Push — master ( b2a65d...63a169 )
by Kirill
02:18
created

Process::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend;
11
12
use Railt\SDL\Frontend\Context\ContextInterface;
13
use Railt\SDL\Frontend\Interceptor\Factory;
14
15
/**
16
 * Class Process
17
 */
18
class Process
19
{
20
    /**
21
     * @var Factory
22
     */
23
    private $factory;
24
25
    /**
26
     * Process constructor.
27
     * @param Factory $factory
28
     */
29
    public function __construct(Factory $factory)
30
    {
31
        $this->factory = $factory;
32
    }
33
34
    /**
35
     * @param ContextInterface $ctx
36
     * @param mixed $result
37
     * @return array
38
     */
39
    public function run(ContextInterface $ctx, $result): array
40
    {
41
        if ($result instanceof \Generator) {
42
            return $this->await($ctx, $result);
43
        }
44
45
        return $this->factory->resolve($ctx, $result);
46
    }
47
48
    /**
49
     * @param ContextInterface $ctx
50
     * @param \Generator $process
51
     * @return array
52
     */
53
    public function await(ContextInterface $ctx, \Generator $process): array
54
    {
55
        while ($process->valid()) {
56
            [$ctx, $value] = $this->factory->resolve($ctx, $process->current());
0 ignored issues
show
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
57
58
            $process->send($value);
0 ignored issues
show
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
59
        }
60
61
        if ($value = $process->getReturn()) {
62
            return $this->factory->resolve($ctx, $value);
63
        }
64
65
        return [$ctx, $value];
66
    }
67
}
68