Passed
Push — master ( d53f0c...96b162 )
by Doru
02:10
created

AbstractArray::exchangeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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