Completed
Push — master ( 6e9edd...3f79e5 )
by Kevin
02:25
created

TokenContainer::appendChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Exceptions\ValidationException;
7
use Groundskeeper\Tokens\Elements\Element;
8
use Psr\Log\LoggerInterface;
9
10
final class TokenContainer implements Cleanable, ContainsChildren, Removable
11
{
12
    /** @var array[Token] */
13
    private $children;
14
15
    /** @var Configuration */
16
    private $configuration;
17
18
    /**
19
     * Constructor
20
     */
21 54
    public function __construct(Configuration $configuration)
22
    {
23 54
        $this->children = array();
24 54
        $this->configuration = $configuration;
25 54
    }
26
27
    /**
28
     * Required by ContainsChildren interface.
29
     */
30 53
    public function getChildren()
31
    {
32 53
        return $this->children;
33
    }
34
35
    /**
36
     * Required by ContainsChildren interface.
37
     */
38 1
    public function hasChild(Token $token)
39
    {
40 1
        return array_search($token, $this->children) !== false;
41
    }
42
43
    /**
44
     * Required by ContainsChildren interface.
45
     */
46 53
    public function appendChild(Token $token)
47
    {
48 53
        $this->children[] = $token;
49
50 53
        return $this;
51
    }
52
53
    /**
54
     * Required by ContainsChildren interface.
55
     */
56 1
    public function prependChild(Token $token)
57
    {
58 1
        array_unshift($this->children, $token);
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Required by ContainsChildren interface.
65
     */
66 1 View Code Duplication
    public function removeChild(Token $token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 1
        $key = array_search($token, $this->children);
69 1
        if ($key !== false) {
70 1
            unset($this->children[$key]);
71
72 1
            return true;
73
        }
74
75 1
        return false;
76
    }
77
78
    /**
79
     * Required by Cleanable interface.
80
     */
81 46
    public function clean(LoggerInterface $logger = null)
82
    {
83 46
        return AbstractToken::cleanChildTokens(
84 46
            $this->configuration,
85 46
            $this->children,
86
            $logger
87 46
        );
88
    }
89
90
    /**
91
     * Required by the Removable interface.
92
     */
93 44 View Code Duplication
    public function remove(LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95 43
        $hasRemovableTypes = $this->configuration->get('type-blacklist') !==
96 43
            Configuration::TYPE_BLACKLIST_NONE;
97 43
        $hasRemovableElements = $this->configuration->get('element-blacklist') !==
98 43
            Configuration::ELEMENT_BLACKLIST_NONE;
99 43
        foreach ($this->children as $key => $child) {
100
            // Check types.
101 43
            if ($hasRemovableTypes &&
102 44
                !$this->configuration->isAllowedType($child->getType())) {
103 5
                unset($this->children[$key]);
104 5
                if ($logger !== null) {
105 5
                    $logger->debug('Removing token of type: ' . $child->getType());
106 5
                }
107
108 5
                continue;
109
            }
110
111
            // Check elements.
112 38
            if ($hasRemovableElements &&
113 38
                $child instanceof Element &&
114 38
                !$this->configuration->isAllowedElement($child->getName())) {
115 1
                unset($this->children[$key]);
116 1
                if ($logger !== null) {
117 1
                    $logger->debug('Removing element of type: ' . $child->getName());
118 1
                }
119
120 1
                continue;
121
            }
122
123
            // Check children.
124 37
            if ($child instanceof Removable) {
125 29
                $child->remove($logger);
126 29
            }
127 43
        }
128 43
    }
129
}
130