Completed
Push — master ( 62c01b...6e9edd )
by Kevin
02:26
created

TokenContainer::remove()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 36
Code Lines 21

Duplication

Lines 36
Ratio 100 %

Code Coverage

Tests 26
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 36
loc 36
ccs 26
cts 26
cp 1
rs 4.8196
cc 10
eloc 21
nc 7
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
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 35
    public function __construct(Configuration $configuration)
22
    {
23 35
        $this->children = array();
24 35
        $this->configuration = $configuration;
25 35
    }
26
27
    /**
28
     * Required by ContainsChildren interface.
29
     */
30 35
    public function getChildren()
31
    {
32 35
        return $this->children;
33
    }
34
35
    /**
36
     * Required by ContainsChildren interface.
37
     */
38
    public function hasChild(Token $token)
39
    {
40
        return array_search($token, $this->children) !== false;
41
    }
42
43
    /**
44
     * Required by ContainsChildren interface.
45
     */
46 35
    public function addChild(Token $token)
47
    {
48 35
        $this->children[] = $token;
49
50 35
        return $this;
51
    }
52
53
    /**
54
     * Required by ContainsChildren interface.
55
     */
56 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...
57
    {
58
        $key = array_search($token, $this->children);
59
        if ($key !== false) {
60
            unset($this->children[$key]);
61
62
            return true;
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * Required by Cleanable interface.
70
     */
71 29
    public function clean(LoggerInterface $logger = null)
72
    {
73 29
        if ($this->configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
74
            return true;
75
        }
76
77 29 View Code Duplication
        foreach ($this->children as $child) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78 23
            if ($child instanceof Cleanable) {
79 17
                $isClean = $child->clean($logger);
80 17
                if (!$isClean) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
81
                    /// @todo
82
                }
83 17
            }
84 29
        }
85
86 29
        return true;
87
    }
88
89
    /**
90
     * Required by the Removable interface.
91
     */
92 29 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...
93
    {
94 29
        $hasRemovableTypes = $this->configuration->get('type-blacklist') !==
95 29
            Configuration::TYPE_BLACKLIST_NONE;
96 29
        $hasRemovableElements = $this->configuration->get('element-blacklist') !==
97 29
            Configuration::ELEMENT_BLACKLIST_NONE;
98 29
        foreach ($this->children as $key => $child) {
99
            // Check types.
100 29
            if ($hasRemovableTypes &&
101 29
                !$this->configuration->isAllowedType($child->getType())) {
102 5
                unset($this->children[$key]);
103 5
                if ($logger !== null) {
104 5
                    $logger->debug('Removing token of type: ' . $child->getType());
105 5
                }
106
107 5
                continue;
108
            }
109
110
            // Check elements.
111 24
            if ($hasRemovableElements &&
112 24
                $child instanceof Element &&
113 24
                !$this->configuration->isAllowedElement($child->getName())) {
114 1
                unset($this->children[$key]);
115 1
                if ($logger !== null) {
116 1
                    $logger->debug('Removing element of type: ' . $child->getName());
117 1
                }
118
119 1
                continue;
120
            }
121
122
            // Check children.
123 23
            if ($child instanceof Removable) {
124 15
                $child->remove($logger);
125 15
            }
126 29
        }
127 29
    }
128
}
129