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

ArrayStrictList::afterElementAdd()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.048

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
ccs 10
cts 11
cp 0.9091
rs 8.4444
cc 8
nc 5
nop 1
crap 8.048
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