AbstractTypedArray   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 1
cbo 0
dl 0
loc 52
ccs 21
cts 21
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A offsetSet() 0 6 1
A verify() 0 6 2
A verifyValue() 0 6 2
1
<?php
2
3
namespace TypedArray;
4
5
abstract class AbstractTypedArray extends \ArrayObject
6
{
7
    /**
8
     * @var callable
9
     */
10
    private $test;
11
12
    /**
13
     * @var string
14
     */
15
    private $type;
16
17
    /**
18
     * @param callable $test The validation test to perform on new array entries
19
     * @param string $type The name of the intended type
20
     * @param array|null The source array
21
     */
22 135
    public function __construct(callable $test, $type, array $source = null)
23
    {
24 135
        $this->test = $test;
25 135
        $this->type = $type;
26
27 135
        if (null !== $source) {
28 66
            $this->verify($source);
29
30 33
            parent::__construct($source);
31 33
        } else {
32 102
            parent::__construct();
33
        }
34 102
    }
35
36 99
    public function offsetSet($index, $newval)
37
    {
38 99
        $this->verifyValue($newval);
39
40 66
        return parent::offsetSet($index, $newval);
41
    }
42
43 66
    private function verify(array $source)
44
    {
45 66
        foreach ($source as $value) {
46 66
            $this->verifyValue($value);
47 33
        }
48 33
    }
49
50 132
    private function verifyValue($value)
51
    {
52 132
        if (!call_user_func($this->test, $value)) {
53 66
            throw new \InvalidArgumentException('Expected ' . $this->type . ', got ' . gettype($value));
54
        }
55 66
    }
56
}
57