Completed
Pull Request — master (#1)
by Lucas Pires
60:57 queued 45:23
created

Collection::put()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
crap 3
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 18
     * Implementation namespace.
19
     *
20 18
     * @var string
21 18
     */
22 18
    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
    public function __construct($classname = null)
30
    {
31
        $this->classname = $classname;
32 15
    }
33
34 15
    /**
35
     * Add a new item to collection.
36 12
     *
37
     * @param string $name
38
     * @param mixed  $value
39
     *
40
     * @return self
41
     */
42
    public function put($name, $value)
43
    {
44
        if (is_object($value) && !$this->isValidObjectInstance($value)) {
45
            throw new InvalidCollectionItemInstanceException(sprintf('%s must implement %s', $name, $this->classname));
46
        }
47
48 15
        $this->items[$name] = $value;
49
50 15
        return $this;
51 12
    }
52
53
    /**
54 3
     * Verify's if the given object has a valid instance.
55
     *
56
     * @param object $object
57
     *
58
     * @return bool
59
     */
60
    private function isValidObjectInstance($object)
61
    {
62 3
        if (!is_null($this->classname) && !$this->isInstanceOrSubclassOf($object)) {
63
            return false;
64 3
        }
65
66
        return true;
67
    }
68
69
    /**
70
     * Check object instance.
71
     *
72
     * @param object $object
73
     * @return bool
74 9
     */
75
    private function isInstanceOrSubclassOf($object)
76 9
    {
77 3
        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...
78
    }
79
80 6
    /**
81
     * Get all items from collection.
82
     *
83
     * @return array
84
     */
85
    public function all()
86
    {
87
        return $this->items;
88
    }
89
90 6
    /**
91
     * Find collection item by name.
92 6
     *
93
     * @param string $name
94 6
     *
95 3
     * @return mixed|null
96
     */
97
    public function find($name)
98 3
    {
99
        if ($this->has($name)) {
100
            return $this->items[$name];
101
        }
102
    }
103
104
    /**
105
     * Check if the given item exists on collection.
106
     *
107
     * @param string $name
108
     *
109
     * @return bool
110
     */
111
    public function has($name)
112
    {
113
        return array_key_exists($name, $this->items);
114
    }
115
116
    /**
117
     * Find a specific collection item or throw's an exception.
118
     *
119
     * @param string $name
120
     *
121
     * @return mixed
122
     */
123
    public function findOrFail($name)
124
    {
125
        $result = $this->find($name);
126
127
        if (is_null($result)) {
128
            throw new CollectionItemNotFoundException(sprintf('Collection item "%s" not found', $name));
129
        }
130
131
        return $result;
132
    }
133
}
134