Completed
Push — master ( 4e457c...e6ff17 )
by Gabriel
02:37
created

Collection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 95
ccs 35
cts 45
cp 0.7778
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __clone() 0 6 2
A reset() 0 4 1
A replace() 0 4 1
A add() 0 4 1
A first() 0 4 2
A jsonSerialize() 0 6 1
A count() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 5 2
A rewind() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
3
namespace Waredesk;
4
5
use Iterator;
6
use Countable;
7
use JsonSerializable;
8
use ArrayAccess;
9
10
abstract class Collection implements Iterator, Countable, ArrayAccess, JsonSerializable
11
{
12
    protected $items;
13
    protected $key;
14
15 6
    public function __construct(array $items = [])
16
    {
17 6
        $this->items = $items;
18 6
    }
19
20 1
    public function __clone()
21
    {
22 1
        foreach ($this->items as $key => $item) {
23 1
            $this->items[$key] = clone $item;
24
        }
25 1
    }
26
27 3
    public function reset()
28
    {
29 3
        $this->items = [];
30 3
    }
31
32
    public function replace(array $items = [])
33
    {
34
        $this->items = $items;
35
    }
36
37 6
    public function add($item)
38
    {
39 6
        $this->items[] = $item;
40 6
    }
41
42 4
    public function first()
43
    {
44 4
        return isset($this->items[0]) ? $this->items[0] : null;
45
    }
46
47
    public function jsonSerialize()
48
    {
49 3
        return array_map(function (JsonSerializable $item) {
50 3
            return $item->jsonSerialize();
51 3
        }, $this->items);
52
    }
53
54 3
    public function count()
55
    {
56 3
        return count($this->items);
57
    }
58
59 1
    public function current()
60
    {
61 1
        return current($this->items);
62
    }
63
64 1
    public function next()
65
    {
66 1
        return next($this->items);
67
    }
68
69 1
    public function key()
70
    {
71 1
        return key($this->items);
72
    }
73
74 5
    public function valid()
75
    {
76 5
        $key = key($this->items);
77 5
        return ($key !== null && $key !== false);
78
    }
79
80 5
    public function rewind()
81
    {
82 5
        reset($this->items);
83 5
    }
84
85
    public function offsetExists($offset): bool
86
    {
87
        return array_key_exists($offset, $this->items);
88
    }
89
90
    public function offsetGet($offset)
91
    {
92
        return $this->items[$offset];
93
    }
94
95
    public function offsetSet($offset, $value)
96
    {
97
        $this->items[$offset] = $value;
98
    }
99
100 1
    public function offsetUnset($offset)
101
    {
102 1
        unset($this->items[$offset]);
103 1
    }
104
}
105