Completed
Push — master ( 0b908e...886262 )
by Kevin
02:30
created

TokenContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 14.86 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 65.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 11
loc 74
ccs 17
cts 26
cp 0.6538
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getChildren() 0 4 1
A hasChild() 0 4 1
A addChild() 0 6 1
A removeChild() 11 11 2
A clean() 0 12 4

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 Psr\Log\LoggerInterface;
7
8
class TokenContainer implements Cleanable, ContainsChildren
9
{
10
    /** @var array[Token] */
11
    private $children;
12
13
    /** @var Configuration */
14
    private $configuration;
15
16
    /**
17
     * Constructor
18
     */
19 21
    public function __construct(Configuration $configuration)
20
    {
21 21
        $this->children = array();
22 21
        $this->configuration = $configuration;
23 21
    }
24
25
    /**
26
     * Required by ContainsChildren interface.
27
     */
28 21
    public function getChildren()
29
    {
30 21
        return $this->children;
31
    }
32
33
    /**
34
     * Required by ContainsChildren interface.
35
     */
36
    public function hasChild(Token $token)
37
    {
38
        return array_search($token, $this->children) !== false;
39
    }
40
41
    /**
42
     * Required by ContainsChildren interface.
43
     */
44 21
    public function addChild(Token $token)
45
    {
46 21
        $this->children[] = $token;
47
48 21
        return $this;
49
    }
50
51
    /**
52
     * Required by ContainsChildren interface.
53
     */
54 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...
55
    {
56
        $key = array_search($token, $this->children);
57
        if ($key !== false) {
58
            unset($this->children[$key]);
59
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Required by Cleanable interface.
68
     */
69 15
    public function clean(LoggerInterface $logger = null)
70
    {
71 15
        if ($this->configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
72
            return;
73
        }
74
75 15
        foreach ($this->children as $child) {
76 15
            if ($child instanceof Cleanable) {
77 7
                $child->clean($logger);
78 7
            }
79 15
        }
80 15
    }
81
}
82