Collection   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 10
c 0
b 0
f 0
ccs 34
cts 40
cp 0.85
wmc 18
lcom 1
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 2
A has() 0 4 1
A set() 0 4 1
A getIterator() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A count() 0 4 1
A select() 0 12 3
A first() 0 8 2
A toArray() 0 4 1
A throwNotFound() 0 4 1
1
<?php declare(strict_types=1);
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Collection;
9
10
class Collection implements CollectionInterface, \Countable
11
{
12
    /**
13
     * @var mixed[]
14
     */
15
    protected $values = [];
16
17
    /**
18
     * @param mixed[] $collection
19
     */
20 65
    public function __construct(array $collection = [])
21
    {
22 65
        $this->values = $collection;
23 65
    }
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @throws \InvalidArgumentException
29
     */
30 47
    public function get(string $name)
31
    {
32 47
        if ($this->has($name)) {
33 42
            return $this->values[$name];
34
        } else {
35 5
            return $this->throwNotFound($name);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 59
    public function has(string $name): bool
43
    {
44 59
        return array_key_exists($name, $this->values);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 58
    public function set(string $name, $object)
51
    {
52 58
        $this->values[$name] = $object;
53 58
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 33
    public function getIterator()
59
    {
60 33
        return new \ArrayIterator($this->values);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 24
    public function offsetExists($offset): bool
67
    {
68 24
        return $this->has($offset);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 31
    public function offsetGet($offset)
75
    {
76 31
        return $this->get($offset);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 44
    public function offsetSet($offset, $value)
83
    {
84 44
        $this->set($offset, $value);
85 44
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function offsetUnset($offset)
91
    {
92 1
        unset($this->values[$offset]);
93 1
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function count(): int
99
    {
100 1
        return count($this->values);
101
    }
102
103
    /**
104
     * @return mixed[]
105
     */
106 3
    public function select(callable $callback): array
107
    {
108 3
        $values = [];
109
110 3
        foreach ($this as $key => $value) {
111 3
            if ($callback($value, $key)) {
112 3
                $values[$key] = $value;
113
            }
114
        }
115
116 3
        return $values;
117
    }
118
119
    /**
120
     * @return mixed
121
     * @throws \InvalidArgumentException
122
     */
123
    public function first()
124
    {
125
        if ($this->count() === 0) {
126
            throw new \InvalidArgumentException("no elements found in collection.");
127
        }
128
129
        return array_values($this->values)[0];
130
    }
131
132
    /**
133
     * @return mixed[]
134
     */
135
    public function toArray(): array
136
    {
137
        return iterator_to_array($this);
138
    }
139
140 1
    protected function throwNotFound(string $name)
141
    {
142 1
        throw new \InvalidArgumentException("`$name` not found in collection.");
143
    }
144
}
145