Completed
Push — master ( 9e4d7b...c3f58e )
by Gabriel
03:57 queued 11s
created

HasElementsTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 26.42%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 25
eloc 51
dl 0
loc 137
ccs 14
cts 53
cp 0.2642
rs 10
c 1
b 0
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
B findElements() 0 20 8
A getElement() 0 7 2
A hasElement() 0 3 1
A addElement() 0 8 1
B setElementOrder() 0 22 7
A getElementByLabel() 0 7 2
A removeElement() 0 10 2
A getElements() 0 8 2
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
use Nip\Form\Elements\AbstractElement;
6
7
/**
8
 * Trait HasElementsTrait
9
 * @package Nip\Form\Traits
10
 */
11
trait HasElementsTrait
12
{
13
    protected $_elements = [];
14
    protected $_elementsLabel;
15
    protected $_elementsOrder = [];
16
17
18
    /**
19
     * @param AbstractElement $element
20
     * @return $this
21
     */
22 4
    public function addElement(AbstractElement $element)
23
    {
24 4
        $name = $element->getUniqueId();
25 4
        $this->_elements[$name] = $element;
26 4
        $this->_elementsLabel[$element->getLabel()] = $name;
27 4
        $this->_elementsOrder[] = $name;
28
29 4
        return $this;
30
    }
31
32
    /**
33
     * @param $name
34
     * @return $this
35
     */
36
    public function removeElement($name)
37
    {
38
        unset($this->_elements[$name]);
39
40
        $key = array_search($name, $this->_elementsOrder);
41
        if ($key) {
42
            unset($this->_elementsOrder[$key]);
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param $name
50
     * @return AbstractElement
51
     */
52 3
    public function getElement($name)
53
    {
54 3
        if (array_key_exists($name, $this->_elements)) {
55 3
            return $this->_elements[$name];
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * @return AbstractElement[]
63
     */
64 1
    public function getElements()
65
    {
66 1
        $return = [];
67 1
        foreach ($this->_elementsOrder as $current) {
68 1
            $return[$current] = $this->_elements[$current];
69
        }
70
71 1
        return $return;
72
    }
73
74
    /**
75
     * @param $name
76
     * @return bool
77
     */
78
    public function hasElement($name)
79
    {
80
        return array_key_exists($name, $this->_elements);
81
    }
82
83
    /**
84
     * @param $label
85
     * @return AbstractElement
86
     */
87
    public function getElementByLabel($label)
88
    {
89
        if (array_key_exists($label, $this->_elementsLabel)) {
90
            return $this->_elements[$this->_elementsLabel[$label]];
91
        }
92
93
        return null;
94
    }
95
    /**
96
     * @param $element
97
     * @param $neighbour
98
     * @param string $type
99
     * @return $this
100
     */
101
    public function setElementOrder($element, $neighbour, $type = 'bellow')
102
    {
103
        if (in_array($element, $this->_elementsOrder) && in_array($neighbour, $this->_elementsOrder)) {
104
            $newOrder = [];
105
            foreach ($this->_elementsOrder as $current) {
106
                if ($current == $element) {
107
                } elseif ($current == $neighbour) {
108
                    if ($type == 'above') {
109
                        $newOrder[] = $element;
110
                        $newOrder[] = $neighbour;
111
                    } else {
112
                        $newOrder[] = $neighbour;
113
                        $newOrder[] = $element;
114
                    }
115
                } else {
116
                    $newOrder[] = $current;
117
                }
118
            }
119
            $this->_elementsOrder = $newOrder;
120
        }
121
122
        return $this;
123
    }
124
    /**
125
     * @param bool $params
126
     * @return array
127
     */
128
    public function findElements($params = false)
129
    {
130
        $elements = [];
131
        foreach ($this->getElements() as $element) {
132
            if (isset($params['type'])) {
133
                if ($element->getType() != $params['type']) {
134
                    continue;
135
                }
136
            }
137
            if (isset($params['attribs']) && is_array($params['attribs'])) {
138
                foreach ($params['attribs'] as $name => $value) {
139
                    if ($element->getAttrib($name) != $value) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $element->getAttrib($name) targeting Nip\Form\Elements\AbstractElement::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
140
                        continue(2);
141
                    }
142
                }
143
            }
144
            $elements[$element->getUniqueId()] = $element;
145
        }
146
147
        return $elements;
148
    }
149
}
150