Completed
Push — master ( 7b1e73...4bb639 )
by Harry
02:39
created

NodeCollection::last()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
ccs 6
cts 7
cp 0.8571
rs 8.8571
cc 5
eloc 7
nc 5
nop 2
crap 5.0729
1
<?php
2
3
namespace Graze\DataNode;
4
5
use Graze\DataStructure\Collection\Collection;
6
use InvalidArgumentException;
7
8
/**
9
 * Class NodeCollection
10
 *
11
 * A Collection of DataNodes that can be acted upon by a flow
12
 *
13
 * @package Graze\DataFlow\Node
14
 */
15
class NodeCollection extends Collection implements NodeCollectionInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 9
    public function add($value)
21
    {
22 9
        if (!($value instanceof NodeInterface)) {
23 1
            throw new InvalidArgumentException("The specified value does not implement NodeInterface");
24
        }
25 8
        return parent::add($value);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function apply($fn)
32
    {
33 2
        foreach ($this->items as &$item) {
34 2
            $out = call_user_func($fn, $item);
35 1
            if (isset($out) && ($out instanceof NodeInterface)) {
36 1
                $item = $out;
37
            }
38
        }
39
40 1
        return $this;
41
    }
42
43
    /**
44
     * On clone, clone all flows too
45
     */
46
    public function __clone()
47
    {
48
        foreach ($this->items as &$item) {
49
            $item = clone $item;
50
        }
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function __toString()
57
    {
58
        return "NodeCollection";
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getClone()
65
    {
66
        return clone $this;
67
    }
68
69
    /**
70
     * @param callable   $fn
71
     * @param mixed|null $default
72
     *
73
     * @return NodeInterface|null
74
     */
75 3 View Code Duplication
    public function first(callable $fn = null, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 3
        if (is_null($fn)) {
78 1
            return count($this->items) > 0 ? reset($this->items) : $default;
79
        }
80
81 2
        foreach ($this->getIterator() as $value) {
82 2
            if (call_user_func($fn, $value)) {
83 2
                return $value;
84
            }
85
        }
86
87 1
        return $default;
88
    }
89
90
    /**
91
     * @param callable   $fn
92
     * @param mixed|null $default
93
     *
94
     * @return NodeInterface|null
95
     */
96 2 View Code Duplication
    public function last(callable $fn = null, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98 2
        if (is_null($fn)) {
99 1
            return count($this->items) > 0 ? end($this->items) : $default;
100
        }
101
102 1
        foreach (array_reverse($this->items) as $value) {
103 1
            if (call_user_func($fn, $value)) {
104 1
                return $value;
105
            }
106
        }
107
108
        return $default;
109
    }
110
}
111