SpecificClassCollection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 127
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 3 1
A __construct() 0 4 1
A valid() 0 3 1
A clear() 0 4 1
A count() 0 3 1
A current() 0 3 1
A rewind() 0 3 1
A key() 0 3 1
A getElements() 0 3 1
A add() 0 6 1
A checkElementIsValid() 0 9 3
1
<?php
2
3
namespace SpecificClassCollection;
4
5
use Countable;
6
use Iterator;
7
8
abstract class SpecificClassCollection implements Countable, Iterator
9
{
10
    private $collection;
11
    private $current;
12
13
    /**
14
     * SpecificClassCollection constructor.
15
     */
16 10
    public function __construct()
17
    {
18 10
        $this->collection = [];
19 10
        $this->current = 0;
20 10
    }
21
22
    /**
23
     * Get the number of elements inside
24
     * the collection
25
     *
26
     * @return int
27
     */
28 2
    public function count()
29
    {
30 2
        return count($this->collection);
31
    }
32
33
    /**
34
     * Add an element to the collection
35
     *
36
     * @param mixed $element
37
     * @return bool
38
     * @throws InvalidClassException
39
     */
40 8
    public function add($element)
41
    {
42 8
        $this->checkElementIsValid($element);
43 7
        $this->collection[] = $element;
44
45 7
        return true;
46
    }
47
48
    /**
49
     * Check if an element can be added to
50
     * the collection
51
     *
52
     * @param mixed $element
53
     * @return bool
54
     * @throws InvalidClassException
55
     */
56 8
    private function checkElementIsValid($element)
57
    {
58 8
        $elementClass = get_class($element);
59
60 8
        if ($this->getValidClassName() !== $elementClass  && ! is_subclass_of($element, $this->getValidClassName())) {
61 1
            throw new InvalidClassException($this->getValidClassName());
62
        }
63
64 7
        return true;
65
    }
66
67
    /**
68
     * Get the FQDN of the class we
69
     * want to store in the collection
70
     *
71
     * @return string
72
     */
73
    abstract protected function getValidClassName();
74
75
    /**
76
     * Empty the collection
77
     */
78 1
    public function clear()
79
    {
80 1
        $this->collection = [];
81 1
        $this->rewind();
82 1
    }
83
84
    /**
85
     * Return the array of elements
86
     *
87
     * @return array
88
     */
89 1
    public function getElements()
90
    {
91 1
        return $this->collection;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     * @return mixed
97
     */
98 2
    public function current()
99
    {
100 2
        return $this->collection[$this->current];
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106 3
    public function next()
107
    {
108 3
        ++$this->current;
109 3
    }
110
111
    /**
112
     * @inheritdoc
113
     * @return int
114
     */
115 3
    public function key()
116
    {
117 3
        return $this->current;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     * @return bool
123
     */
124 3
    public function valid()
125
    {
126 3
        return isset($this->collection[$this->current]);
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132 3
    public function rewind()
133
    {
134 3
        $this->current = 0;
135 3
    }
136
}
137