Completed
Push — master ( a17ead...35004c )
by Kevin
02:36
created

TokenContainer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 35.96 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 3
Metric Value
wmc 16
c 6
b 1
f 3
lcom 1
cbo 4
dl 41
loc 114
ccs 46
cts 46
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getChildren() 0 4 1
A hasChild() 0 4 1
A appendChild() 0 6 1
A prependChild() 0 6 1
A removeChild() 11 11 2
A clean() 0 8 1
C remove() 30 30 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Exceptions\ValidationException;
7
use Groundskeeper\Tokens\Elements\Element;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Groundskeeper\Tokens\Element.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 51
    public function __construct(Configuration $configuration)
22
    {
23 51
        $this->children = array();
24 51
        $this->configuration = $configuration;
25 51
    }
26
27
    /**
28
     * Required by ContainsChildren interface.
29
     */
30 51
    public function getChildren()
31
    {
32 51
        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, true) !== false;
41
    }
42
43
    /**
44
     * Required by ContainsChildren interface.
45
     */
46 50
    public function appendChild(Token $token)
47
    {
48 50
        $this->children[] = $token;
49
50 50
        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 5 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 5
        $key = array_search($token, $this->children, true);
69 5
        if ($key !== false) {
70 5
            unset($this->children[$key]);
71
72 5
            return true;
73
        }
74
75 1
        return false;
76
    }
77
78
    /**
79
     * Required by Cleanable interface.
80
     */
81 43
    public function clean(LoggerInterface $logger)
82
    {
83 43
        return AbstractToken::cleanChildTokens(
84 43
            $this->configuration,
85 43
            $this->children,
86
            $logger
87 43
        );
88
    }
89
90
    /**
91
     * Required by the Removable interface.
92
     */
93 42 View Code Duplication
    public function remove(LoggerInterface $logger)
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 42
        $hasRemovableElements = $this->configuration->get('element-blacklist') != '';
96 42
        $hasRemovableTypes = $this->configuration->get('type-blacklist') != '';
97 42
        foreach ($this->children as $child) {
98
            // Check types.
99 42
            if ($hasRemovableTypes &&
100 42
                !$this->configuration->isAllowedType($child->getType())) {
101 2
                $logger->debug('Removing ' . $child);
102 2
                $this->removeChild($child);
103 2
104
                continue;
105 2
            }
106 2
107
            // Check elements.
108
            if ($hasRemovableElements &&
109
                $child->getType() == Token::ELEMENT &&
110 42
                !$this->configuration->isAllowedElement($child->getName())) {
111 42
                $logger->debug('Removing ' . $child);
112 42
                $this->removeChild($child);
113 2
114 2
                continue;
115 2
            }
116
117 2
            // Check children.
118 2
            if ($child instanceof Removable) {
119
                $child->remove($logger);
120
            }
121
        }
122 40
    }
123
}
124