Passed
Push — master ( 4ee1ca...f0c776 )
by Maxim
02:45 queued 10s
created

ArrayStrictList::afterElementsSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @author Anton Lytkin <[email protected]>
4
 */
5
6
namespace WS\Utils\Collections;
7
8
use InvalidArgumentException;
9
10
class ArrayStrictList extends ArrayList
11
{
12 10
    protected function afterElementsSet(): void
13
    {
14 10
        foreach ($this->elements as $element) {
15 10
            $this->afterElementAdd($element);
16
        }
17 2
        parent::afterElementsSet();
18 2
    }
19
20 10
    protected function afterElementAdd($element): void
21
    {
22 10
        $firstElement = $this->elements[0];
23 10
        if (null === $firstElement && !count($this->elements)) {
24
            return;
25
        }
26 10
        if (is_object($firstElement)) {
27 2
            if (!is_object($element) || (get_class($firstElement) !== get_class($element))) {
28 2
                throw new InvalidArgumentException('Collection must contain elements with identical types');
29
            }
30 2
            return;
31
        }
32 8
        if (is_object($element) || (gettype($firstElement) !== gettype($element))) {
33 7
            throw new InvalidArgumentException('Collection must contain elements with identical types');
34
        }
35 8
        parent::afterElementAdd($element);
36 8
    }
37
}
38