Completed
Push — master ( 6866f8...494812 )
by Juliano
12:29 queued 09:02
created

Collection::each()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3.0261
1
<?php
2
3
namespace Raidros\Collection;
4
5
use Raidros\Collection\Exceptions\CollectionItemNotFoundException;
6
use Raidros\Collection\Exceptions\InvalidCollectionItemInstanceException;
7
8
class Collection
9
{
10
    /**
11
     * Collection items.
12
     *
13
     * @var array
14
     */
15
    private $items = [];
16
17
    /**
18
     * Implementation namespace.
19
     *
20
     * @var string
21
     */
22
    private $classname;
23
24
    /**
25
     * Create a new collection instance.
26
     *
27
     * @param string|null $classname Class or interface of namespace for implementation
28
     */
29 57
    public function __construct($classname = null)
30
    {
31 57
        $this->classname = $classname;
32 57
    }
33
34
    /**
35
     * Add a new item to collection.
36
     *
37
     * @param string $name
38
     * @param mixed  $value
39
     *
40
     * @return self
41
     */
42 57
    public function put($name, $value)
43
    {
44 57
        if (is_object($value) && !$this->isValidObjectInstance($value)) {
45 12
            throw new InvalidCollectionItemInstanceException(sprintf('%s must implement %s', $name, $this->classname));
46
        }
47
48 45
        $this->items[$name] = $value;
49
50 45
        return $this->items[$name];
51
    }
52
53
    /**
54
     * Verify's if the given object has a valid instance.
55
     *
56
     * @param object $object
57
     *
58
     * @return bool
59
     */
60 57
    private function isValidObjectInstance($object)
61
    {
62 57
        if (!is_null($this->classname) && !$this->isInstanceOrSubclassOf($object)) {
63 12
            return false;
64
        }
65
66 45
        return true;
67
    }
68
69
    /**
70
     * Check object instance.
71
     *
72
     * @param object $object
73
     *
74
     * @return bool
75
     */
76 57
    private function isInstanceOrSubclassOf($object)
77
    {
78 57
        return $object instanceof $this->classname or is_subclass_of($object, $this->classname);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
79
    }
80
81
    /**
82
     * Get all items from collection.
83
     *
84
     * @return array
85
     */
86 3
    public function all()
87
    {
88 3
        return $this->items;
89
    }
90
91
    /**
92
     * Find collection item by name.
93
     *
94
     * @param string $name
95
     *
96
     * @return mixed|null
97
     */
98 33
    public function find($name)
99
    {
100 33
        if ($this->has($name)) {
101 21
            return $this->items[$name];
102
        }
103 12
    }
104
105
    /**
106
     * Check if the given item exists on collection.
107
     *
108
     * @param string $name
109
     *
110
     * @return bool
111
     */
112 42
    public function has($name)
113
    {
114 42
        return array_key_exists($name, $this->items);
115
    }
116
117
    /**
118
     * Find a specific collection item or throw's an exception.
119
     *
120
     * @param string $name
121
     *
122
     * @return mixed
123
     */
124 12
    public function findOrFail($name)
125
    {
126 12
        $result = $this->find($name);
127
128 12
        if (is_null($result)) {
129 3
            throw new CollectionItemNotFoundException(sprintf('Collection item "%s" not found', $name));
130
        }
131
132 9
        return $result;
133
    }
134
135
    /**
136
     * Run a callback over each item.
137
     *
138
     * @param callable $callback
139
     *
140
     * @return self
141
     */
142 3
    public function each(callable $callback)
143
    {
144 3
        if (!count($this->items)) {
145
            return $this;
146
        }
147
148 3
        foreach ($this->items as $key => $value) {
149 3
            $callback($value, $key);
150 3
        }
151 3
    }
152
153
    /**
154
     * Remove an item from collection.
155
     *
156
     * @param string $name
157
     *
158
     * @return self
159
     */
160 9
    public function remove($name)
161
    {
162 9
        if ($this->has($name)) {
163 9
            unset($this->items[$name]);
164 9
        }
165
166 9
        return $this;
167
    }
168
}
169