Completed
Push — master ( 181562...3cf559 )
by Pablo
02:49
created

ObjectCollection   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 122
ccs 49
cts 53
cp 0.9245
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A init() 0 4 1
setClassName() 0 1 ?
A getCollection() 0 4 1
A first() 0 4 1
A last() 0 4 1
A key() 0 4 1
A next() 0 4 1
A current() 0 4 1
A remove() 0 11 3
A removeElement() 0 12 2
A contains() 0 4 1
A get() 0 4 1
A getKeys() 0 4 1
A getValues() 0 4 1
A count() 0 4 1
A set() 0 4 1
A add() 0 4 1
A isEmpty() 0 4 1
A clear() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpValueObjects\Collection;
6
7
use PhpValueObjects\Collection\Exception\InvalidCollectionObjectException;
8
9
abstract class ObjectCollection
10
{
11
    protected $objects;
12
13 54
    public function __construct(array $objects)
14
    {
15 54
        foreach ($objects as $object) {
16 54
            if (false === is_a($object, $this->setClassName())) {
17 54
                throw new InvalidCollectionObjectException($object, $this->setClassName());
18
            }
19
        }
20 54
        $this->objects = $objects;
21 54
    }
22
23
    public static function init(): self
24
    {
25
        return new static([]);
26
    }
27
28
    abstract protected function setClassName(): string;
29
30 12
    public function getCollection(): array
31
    {
32 12
        return $this->objects;
33
    }
34
35 12
    public function first()
36
    {
37 12
        return reset($this->objects);
38
    }
39
40 9
    public function last()
41
    {
42 9
        return end($this->objects);
43
    }
44
45 3
    public function key()
46
    {
47 3
        return key($this->getCollection());
48
    }
49
50 6
    public function next()
51
    {
52 6
        return next($this->objects);
53
    }
54
55 3
    public function current()
56
    {
57 3
        return current($this->objects);
58
    }
59
60 3
    public function remove($key)
61
    {
62 3
        if (!isset($this->objects[$key]) && !array_key_exists($key, $this->objects)) {
63
            return null;
64
        }
65
66 3
        $removed = $this->objects[$key];
67 3
        unset($this->objects[$key]);
68
69 3
        return $removed;
70
    }
71
72 3
    public function removeElement($element): bool
73
    {
74 3
        $key = array_search($element, $this->objects, true);
75
76 3
        if ($key === false) {
77
            return false;
78
        }
79
80 3
        unset($this->objects[$key]);
81
82 3
        return true;
83
    }
84
85 3
    public function contains($element): bool
86
    {
87 3
        return in_array($element, $this->objects, true);
88
    }
89
90 6
    public function get($key)
91
    {
92 6
        return $this->objects[$key] ?? null;
93
    }
94
95 3
    public function getKeys()
96
    {
97 3
        return array_keys($this->objects);
98
    }
99
100 3
    public function getValues()
101
    {
102 3
        return array_values($this->objects);
103
    }
104
105 3
    public function count(): int
106
    {
107 3
        return count($this->objects);
108
    }
109
110 3
    public function set($key, $value): void
111
    {
112 3
        $this->objects[$key] = $value;
113 3
    }
114
115 3
    public function add($element): void
116
    {
117 3
        $this->objects[] = $element;
118 3
    }
119
120 3
    public function isEmpty(): bool
121
    {
122 3
        return empty($this->objects);
123
    }
124
125 3
    public function clear(): void
126
    {
127 3
        $this->objects = [];
128 3
    }
129
130
}
131