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

AbstractArray   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 38
ccs 20
cts 20
cp 1
rs 10
wmc 6

4 Methods

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