AbstractTypedObjectCollection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 32
ccs 0
cts 9
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
getAllowedItemTypes() 0 1 ?
A checkType() 0 9 3
A add() 0 5 1
1
<?php
2
3
namespace Nayjest\Collection\Extended;
4
5
use Nayjest\Collection\CollectionTrait;
6
use Nayjest\Collection\Exceptions\InvalidItemException;
7
8
/**
9
 * Base class for typed collections.
10
 */
11
abstract class AbstractTypedObjectCollection implements ObjectCollectionInterface
12
{
13
    use CollectionTrait {
14
        CollectionTrait::add as protected addWithoutCheck;
15
    }
16
    use ObjectCollectionTrait;
17
18
    /**
19
     * @return string[]
20
     */
21
    abstract protected function getAllowedItemTypes();
22
23
    /**
24
     * @param $item
25
     * @return bool
26
     */
27
    protected function checkType($item)
28
    {
29
        foreach ($this->getAllowedItemTypes() as $className) {
30
            if ($item instanceof $className) {
31
                return true;
32
            }
33
        }
34
        throw new InvalidItemException($item, $this->getAllowedItemTypes());
35
    }
36
37
    public function add($item, $prepend = false)
38
    {
39
        $this->checkType($item);
40
        return $this->addWithoutCheck($item, $prepend);
41
    }
42
}
43