Test Setup Failed
Push — master ( 6d6991...a81a2e )
by Gabriel
02:14
created

HasDecoratorsTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 23.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 20
loc 86
c 0
b 0
f 0
ccs 0
cts 27
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A newDecorator() 0 8 1
A attachDecorator() 0 11 2
A getDecoratorsByPosition() 0 4 1
A getDecorator() 9 14 4
A removeDecorator() 11 16 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 Nip\Form\Elements\Traits;
4
5
use Nip_Form_Decorator_Elements_Abstract;
6
7
/**
8
 * Trait HasDecoratorsTrait
9
 * @package Nip\Form\Elements\Traits
10
 */
11
trait HasDecoratorsTrait
12
{
13
    protected $_decorators;
14
15
    /**
16
     * @param string $type
17
     * @return mixed
18
     */
19
    public function newDecorator($type = '')
20
    {
21
        $name = 'Nip_Form_Decorator_Elements_'.ucfirst($type);
22
        $decorator = new $name();
23
        $decorator->setElement($this);
24
25
        return $decorator;
26
    }
27
28
    /**
29
     * @param Nip_Form_Decorator_Elements_Abstract $decorator
30
     * @param string $position
31
     * @param bool $name
32
     * @return $this
33
     */
34
    public function attachDecorator(
35
        Nip_Form_Decorator_Elements_Abstract $decorator,
36
        $position = 'element',
37
        $name = false
38
    ) {
39
        $decorator->setElement($this);
40
        $name = $name ? $name : $decorator->getName();
41
        $this->_decorators[$position][$name] = $decorator;
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param boolean $position
48
     * @return mixed
49
     */
50
    public function getDecoratorsByPosition($position)
51
    {
52
        return $this->_decorators[$position];
53
    }
54
55
    /**
56
     * @param $name
57
     * @param bool $position
58
     * @return bool
59
     */
60
    public function getDecorator($name, $position = false)
61
    {
62 View Code Duplication
        if ($position) {
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...
63
            return $this->_decorators[$position][$name];
64
        } else {
65
            foreach ($this->_decorators as $position => $decorators) {
66
                if (isset($decorators[$name])) {
67
                    return $decorators[$name];
68
                }
69
            }
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * @param $name
77
     * @param bool $position
78
     * @return $this
79
     */
80
    public function removeDecorator($name, $position = false)
81
    {
82 View Code Duplication
        if ($position) {
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...
83
            unset($this->_decorators[$position][$name]);
84
        } else {
85
            foreach ($this->_decorators as $position => $decorators) {
86
                if (isset($decorators[$name])) {
87
                    unset($decorators[$name]);
88
89
                    return $this;
90
                }
91
            }
92
        }
93
94
        return $this;
95
    }
96
}
97