DefinitionList   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 36
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A getTagged() 0 10 4
A hasTag() 0 9 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Definition;
5
6
use Habemus\Definition\Tag\Taggable;
7
use Habemus\Utility\Lists\KeyValueList;
8
use IteratorAggregate;
9
10
/**
11
 * @method Definition get($id):
12
 */
13
class DefinitionList implements IteratorAggregate
14
{
15
    use KeyValueList {
16
        set as private;
17
    }
18
19
    public function add(Definition $definition)
20
    {
21
        $this->set($definition->getIdentity(), $definition);
22
    }
23
24
    public function hasTag(string $tag): bool
25
    {
26
        foreach ($this->elements as $definition) {
27
            if ($definition instanceof Taggable && $definition->hasTag($tag)) {
28
                return true;
29
            }
30
        }
31
32
        return false;
33
    }
34
35
    /**
36
     * @param string $tag
37
     * @return Definition[]|Taggable[]
38
     */
39
    public function getTagged(string $tag): array
40
    {
41
        $tagged = [];
42
        foreach ($this->elements as $definition) {
43
            if ($definition instanceof Taggable && $definition->hasTag($tag)) {
44
                $tagged[] = $definition;
45
            }
46
        }
47
48
        return $tagged;
49
    }
50
}
51