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

AbstractArray   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 42
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkType() 0 4 3
A checkAllElements() 0 5 1
A exchangeArray() 0 5 1
A __construct() 0 6 1
A offsetSet() 0 7 1
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
}