HasDecoratorsTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 93
ccs 18
cts 28
cp 0.6429
rs 10
c 2
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addDecorator() 0 5 1
A attachDecorator() 0 10 2
A newDecorator() 0 3 1
A getDecorator() 0 13 4
A removeDecorator() 0 15 4
A getDecoratorsByPosition() 0 3 2
1
<?php
2
3
namespace Nip\Form\Elements\Traits;
4
5
use Nip\Form\Decorator\DecoratorFactory;
6
use Nip\Form\Decorator\Elements\AbstractDecorator;
7
8
/**
9
 * Trait HasDecoratorsTrait
10
 * @package Nip\Form\Elements\Traits
11
 */
12
trait HasDecoratorsTrait
13
{
14
    protected $decorators;
15
16
    /**
17
     * @param string $type
18
     * @return mixed
19
     */
20 1
    public function newDecorator($type = '')
21
    {
22 1
        return DecoratorFactory::createForElement($this, $type);
23
    }
24
25
    /**
26
     * @param string $type
27
     * @param string $position
28
     * @param bool|string $name
29
     * @return mixed
30
     */
31 1
    public function addDecorator($type = '', $position = 'element', $name = false)
32
    {
33 1
        $decorator = $this->newDecorator($type);
34 1
        $this->attachDecorator($decorator, $position, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of Nip\Form\Elements\Traits...rait::attachDecorator() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $this->attachDecorator($decorator, $position, /** @scrutinizer ignore-type */ $name);
Loading history...
35 1
        return $decorator;
36
    }
37
38
    /**
39
     * @param AbstractDecorator $decorator
40
     * @param string $position
41
     * @param bool $name
42
     * @return $this
43
     */
44 1
    public function attachDecorator(
45
        AbstractDecorator $decorator,
46
        $position = 'element',
47
        $name = false
48
    ) {
49 1
        $decorator->setElement($this);
50 1
        $name = $name ? $name : $decorator->getName();
51 1
        $this->decorators[$position][$name] = $decorator;
52
53 1
        return $this;
54
    }
55
56
    /**
57
     * @param boolean $position
58
     * @return mixed
59
     */
60 8
    public function getDecoratorsByPosition($position)
61
    {
62 8
        return isset($this->decorators[$position]) ? $this->decorators[$position] : [];
63
    }
64
65
    /**
66
     * @param $name
67
     * @param bool $position
68
     * @return bool
69
     */
70 1
    public function getDecorator($name, $position = false)
71
    {
72 1
        if ($position) {
73
            return $this->decorators[$position][$name];
74
        } else {
75 1
            foreach ($this->decorators as $position => $decorators) {
76 1
                if (isset($decorators[$name])) {
77 1
                    return $decorators[$name];
78
                }
79
            }
80
        }
81
82
        return false;
83
    }
84
85
    /**
86
     * @param $name
87
     * @param bool $position
88
     * @return $this
89
     */
90
    public function removeDecorator($name, $position = false)
91
    {
92
        if ($position) {
93
            unset($this->decorators[$position][$name]);
94
        } else {
95
            foreach ($this->decorators as $position => $decorators) {
96
                if (isset($decorators[$name])) {
97
                    unset($decorators[$name]);
98
99
                    return $this;
100
                }
101
            }
102
        }
103
104
        return $this;
105
    }
106
}
107