Completed
Push — master ( 76bd48...3507b0 )
by Harry
15s
created

NodeCollection::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of graze/data-node
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-node/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-node
12
 */
13
14
namespace Graze\DataNode;
15
16
use Graze\DataStructure\Collection\Collection;
17
use InvalidArgumentException;
18
19
/**
20
 * Class NodeCollection
21
 *
22
 * A Collection of DataNodes that can be acted upon by a flow
23
 *
24
 * @package Graze\DataFlow\Node
25
 */
26
class NodeCollection extends Collection implements NodeCollectionInterface
27
{
28
    /**
29
     * @inheritdoc
30
     */
31 11
    public function add($value)
32
    {
33 11
        if (!($value instanceof NodeInterface)) {
34 1
            throw new InvalidArgumentException("The specified value does not implement NodeInterface");
35
        }
36 10
        return parent::add($value);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 2
    public function apply(callable $fn)
43
    {
44 2
        foreach ($this->items as &$item) {
45 2
            $out = call_user_func($fn, $item);
46 2
            if (isset($out) && ($out instanceof NodeInterface)) {
47 1
                $item = $out;
48 1
            }
49 2
        }
50
51 2
        return $this;
52
    }
53
54
    /**
55
     * On clone, clone all flows too
56
     */
57 1
    public function __clone()
58
    {
59 1
        foreach ($this->items as &$item) {
60 1
            $item = clone $item;
61 1
        }
62 1
    }
63
64
    /**
65
     * @return string
66
     */
67 1
    public function __toString()
68
    {
69 1
        return "NodeCollection";
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 1
    public function getClone()
76
    {
77 1
        return clone $this;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 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...
84
    {
85 3
        if (is_null($fn)) {
86 1
            return count($this->items) > 0 ? reset($this->items) : $default;
87
        }
88
89 2
        foreach ($this->getIterator() as $value) {
90 2
            if (call_user_func($fn, $value)) {
91 1
                return $value;
92
            }
93 2
        }
94
95 1
        return $default;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 3 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...
102
    {
103 3
        if (is_null($fn)) {
104 1
            return count($this->items) > 0 ? end($this->items) : $default;
105
        }
106
107 2
        foreach (array_reverse($this->items) as $value) {
108 2
            if (call_user_func($fn, $value)) {
109 1
                return $value;
110
            }
111 2
        }
112
113 1
        return $default;
114
    }
115
}
116