TypedCollection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A offsetSet() 0 5 1
A add() 0 5 1
A validateType() 0 7 3
isValidType() 0 1 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Collection;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
9
abstract class TypedCollection extends ArrayCollection
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function __construct(array $elements = [])
15
    {
16
        foreach ($elements as $value) {
17
            $this->validateType($value);
18
        }
19
20
        parent::__construct($elements);
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function offsetSet($offset, $value)
27
    {
28
        $this->validateType($value);
29
        parent::offsetSet($offset, $value);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function add($value)
36
    {
37
        $this->validateType($value);
38
        parent::add($value);
39
    }
40
41
    /**
42
     * @param mixed $value
43
     *
44
     * @throws InvalidArgumentException
45
     */
46
    protected function validateType($value)
47
    {
48
        if (!$this->isValidType($value)) {
49
            $type = is_object($value) ? get_class($value) : gettype($value);
50
            throw new \InvalidArgumentException('Unsupported type: ' . $type);
51
        }
52
    }
53
54
    /**
55
     * @param mixed $value
56
     *
57
     * @return bool
58
     */
59
    abstract public function isValidType($value);
60
}
61