Completed
Pull Request — master (#39)
by
unknown
24:08
created

Fork   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A join() 0 11 2
A disjoin() 0 12 2
A __invoke() 0 8 2
1
<?php
2
3
namespace League\Pipeline;
4
5
6
class Fork implements ForkInterface
7
{
8
    /**
9
     * @var Pipeline
10
     */
11
    private $parent;
12
13
    /**
14
     * @var array callable
15
     */
16
    protected $forks = [];
17
18
    /**
19
     * @var callable
20
     */
21
    protected $resolver;
22
23
    /**
24
     * Fork constructor.
25
     *
26
     * @param Pipeline $pipeline
27
     * @param callable|null $resolver
28
     */
29
    public function __construct(Pipeline $pipeline, callable $resolver = null)
30
    {
31
        $this->parent = $pipeline;
32
        $this->resolver = $resolver;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function join(callable $resolver = null)
39
    {
40
        if($resolver != null)
41
        {
42
            $this->resolver = $resolver;
43
        }
44
45
        assertNotNull($this->resolver);
46
47
        return $this->parent;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function disjoin($tag, callable $stage = null)
54
    {
55
        $disjointPipeline = new DisjointAwarePipeline($this);
56
57
        if($stage != null)
58
        {
59
            $disjointPipeline->pipe($stage);
60
        }
61
62
        $forks[$tag] = $disjointPipeline;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$forks was never initialized. Although not strictly required by PHP, it is generally a good practice to add $forks = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
63
        return $disjointPipeline;
64
    }
65
66
    /**
67
     * Chooses a fork or short-circuits based on $resolver
68
     *
69
     * @param mixed $payload
70
     * @return mixed
71
     */
72
    public function __invoke($payload)
73
    {
74
        $flowTo = call_user_func($this->resolver, $payload);
75
76
        if($flowTo === false) return $payload;
77
78
        return $this->forks[$flowTo]->process($payload);
79
    }
80
}
81