Completed
Push — master ( 0cf860...53ed02 )
by Juliano
02:47
created

Collection::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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 54
    public function __construct($classname = null)
30
    {
31 54
        $this->classname = $classname;
32 54
    }
33
34
    /**
35
     * Add a new item to collection.
36
     *
37
     * @param string $name
38
     * @param mixed  $value
39
     *
40
     * @return self
41
     */
42 54
    public function put($name, $value)
43
    {
44 54
        if (is_object($value) && !$this->isValidObjectInstance($value)) {
45 12
            throw new InvalidCollectionItemInstanceException(sprintf('%s must implement %s', $name, $this->classname));
46
        }
47
48 42
        $this->items[$name] = $value;
49
50 42
        return $this;
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 54
    private function isValidObjectInstance($object)
61
    {
62 54
        if (!is_null($this->classname) && !$this->isInstanceOrSubclassOf($object)) {
63 12
            return false;
64
        }
65
66 42
        return true;
67
    }
68
69
    /**
70
     * Check object instance.
71
     *
72
     * @param object $object
73
     *
74
     * @return bool
75
     */
76 54
    private function isInstanceOrSubclassOf($object)
77
    {
78 54
        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 30
    public function find($name)
99
    {
100 30
        if ($this->has($name)) {
101 18
            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 39
    public function has($name)
113
    {
114 39
        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
     * Remove an item from collection.
137
     *
138
     * @param string $name
139
     *
140
     * @return self
141
     */
142 9
    public function remove($name)
143
    {
144 9
        if ($this->has($name)) {
145 9
            unset($this->items[$name]);
146 9
        }
147
148 9
        return $this;
149
    }
150
}
151