Passed
Push — master ( 369ddf...280e50 )
by Gaetano
10:01
created

AbstractCollection   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 74.07%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 90
ccs 20
cts 27
cp 0.7407
rs 10
c 1
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exchangeArray() 0 9 3
A __construct() 0 9 3
A offsetSet() 0 7 2
A append() 0 7 2
A __set_state() 0 3 1
A isValidElement() 0 3 1
A reset() 0 5 1
A throwInvalid() 0 3 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\API\Collection;
4
5
/**
6
 * Implements a 'typed array' structure
7
 */
8
class AbstractCollection extends \ArrayObject
9
{
10
    protected $allowedClass;
11
12
    /**
13
     * AbstractCollection constructor.
14
     * @param array $input
15
     * @param int $flags
16
     * @param string $iterator_class
17
     */
18 103
    public function __construct($input = array(), $flags = 0, $iterator_class = "ArrayIterator")
19
    {
20 103
        foreach ($input as $value) {
21 99
            if (!$this->isValidElement($value)) {
22 99
                $this->throwInvalid($value);
23
            }
24
        }
25
26 102
        parent::__construct($input, $flags, $iterator_class);
27 102
    }
28
29
    /**
30
     * @param mixed $value
31
     */
32
    public function append($value)
33
    {
34
        if (!$this->isValidElement($value)) {
35
            $this->throwInvalid($value);
36
        }
37
38
        parent::append($value);
39
    }
40
41
    /**
42
     * @param mixed $input
43
     * @return array the old array
44
     */
45 1
    public function exchangeArray($input)
46
    {
47 1
        foreach ($input as $value) {
48 1
            if (!$this->isValidElement($value)) {
49 1
                $this->throwInvalid($value);
50
            }
51
        }
52
53 1
        return parent::exchangeArray($input);
54
    }
55
56
    /**
57
     * @param mixed $index
58
     * @param mixed $value
59
     */
60 19
    public function offsetSet($index, $value)
61
    {
62 19
        if (!$this->isValidElement($value)) {
63 2
            $this->throwInvalid($value);
64
        }
65
66 17
        parent::offsetSet($index, $value);
67 17
    }
68
69 103
    /**
70
     * Work around php 7.4 having removed support for calling `reset($this)`
71 103
     * @todo optimize this (esp. for big collections)
72
     */
73
    public function reset()
74 4
    {
75
        $results = $this->getArrayCopy();
76 4
77
        return reset($results);
78
    }
79
80
    protected function isValidElement($value)
81
    {
82
        return is_a($value, $this->allowedClass);
83
    }
84
85
    protected function throwInvalid($value)
86
    {
87
        throw new \InvalidArgumentException("Can not add element of type '" . (is_object($value) ? get_class($value) : gettype($value)) . "' to Collection of type '" . get_class($this) . "'");
88
    }
89
90
    /**
91
     * Allow the class to be serialized to php using var_export
92
     * @param array $data
93
     * @return static
94
     */
95
    public static function __set_state(array $data)
96
    {
97
        return new static($data);
98
    }
99
}
100