Passed
Push — master ( f14939...a6afbd )
by Doru
02:35
created

AbstractArray::checkAllElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Diacdg\PHPArray;
5
6
abstract class AbstractArray extends \ArrayObject
7
{
8
    protected $valueType;
9
10 16
    public function __construct(string $valueType, array $array = [], int $flags = 0, string $iteratorClass = "ArrayIterator") {
11 16
        $this->valueType = $valueType;
12
13 16
        $this->checkAllElements($array);
14
15 16
        parent::__construct($array, $flags, $iteratorClass);
16 16
    }
17
18 12
    public function offsetSet($offset, $value)
19
    {
20 12
        $this->checkType($value);
21
22 10
        $this->checkOffset($offset);
23
24 8
        parent::offsetSet($offset, $value);
25 8
    }
26
27 1
    public function exchangeArray($array)
28
    {
29 1
        $this->checkAllElements($array);
30
31 1
        parent::exchangeArray($array);
32 1
    }
33
34 16
    protected function checkType($value): void
35
    {
36 16
        if (gettype($value) !== strtolower($this->valueType) && !$value instanceof $this->valueType) {
37 2
            throw new \InvalidArgumentException('Value must be of type: ' . $this->valueType . " but value of type: " . gettype($value) . " given.");
38
        }
39 14
    }
40
41
    abstract protected function checkOffset($offset): void;
42
43 16
    private function checkAllElements(array $array): void
44
    {
45 16
        array_walk($array, function ($value, $offset) {
46 4
            $this->checkOffset($offset);
47 4
            $this->checkType($value);
48 16
        });
49
    }
50
}