MagicMethodElementsFormTrait   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 81.08%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 52
c 3
b 0
f 2
dl 0
loc 149
rs 10
ccs 30
cts 37
cp 0.8108
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getElementNameFromMagicMethodArguments() 0 3 1
A __call() 0 10 2
A getElementOptionsFromMagicMethodArguments() 0 3 2
A isMagicMethodAddElements() 0 3 1
A magicMethodAddElement() 0 8 1
A setElementsTypes() 0 3 1
A getElementsTypes() 0 3 1
A isElementsType() 0 3 1
A getElementIsRequiredFromMagicMethodArguments() 0 3 2
A detectMagicMethodAddElements() 0 13 3
A getElementLabelFromMagicMethodArguments() 0 3 2
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
use Nip\Form\AbstractForm;
6
7
/**
8
 * Class MagicMethodElementsFormTrait
9
 * @package Nip\Form
10
 *
11
 * @method addInput($name, $label = false, $isRequired = false)
12
 * @method addHidden($name, $label = false, $isRequired = false)
13
 * @method addPassword($name, $label = false, $isRequired = false)
14
 * @method addSelect($name, $label = false, $isRequired = false)
15
 * @method addMultiSelect($name, $label = false, $isRequired = false)
16
 * @method addDateinput($name, $label = false, $isRequired = false)
17
 * @method addRadioGroup($name, $label = false, $isRequired = false)
18
 * @method addBsRadioGroup($name, $label = false, $isRequired = false)
19
 * @method addTextarea($name, $label = false, $isRequired = false)
20
 * @method addTextSimpleEditor($name, $label = false, $isRequired = false)
21
 * @method addTextMiniEditor($name, $label = false, $isRequired = false)
22
 * @method addFile($name, $label = false, $isRequired = false)
23
 * @method addNumber($name, $label = false, $isRequired = false)
24
 * @method addMultiElement($name, $label = false, $isRequired = false)
25
 * @method addMoney($name, $label = false, $isRequired = false)
26
 * @method addTel($name, $label = false, $isRequired = false)
27
 * @method addTime($name, $label = false, $isRequired = false)
28
 * @method addTimeselect($name, $label = false, $isRequired = false)
29
 */
30
trait MagicMethodElementsFormTrait
31
{
32
    protected $elementsTypes = [
33
        'input',
34
        'hidden',
35
        'password',
36
        'hash',
37
        'file',
38
        'multiElement',
39
        'dateinput',
40
        'dateselect',
41
        'tel',
42
        'time',
43
        'timeselect',
44
        'textarea',
45
        'texteditor',
46
        'textSimpleEditor',
47
        'textMiniEditor',
48
        'select',
49
        'number',
50
        'money',
51
        'multiSelect',
52 3
        'radio',
53
        'radioGroup',
54 3
        'checkbox',
55 3
        'checkboxGroup',
56 3
        'html',
57
    ];
58
59
    /**
60
     * @param $name
61
     * @param $arguments
62
     * @return AbstractForm|self
63
     */
64
    public function __call($name, $arguments)
65
    {
66
        $addElements = $this->detectMagicMethodAddElements($name, $arguments);
67
        if ($addElements !== false) {
68
            return $addElements;
69 3
        }
70
71 3
        trigger_error('Call to undefined method: [' . $name . ']', E_USER_ERROR);
72 3
73 3
        return $this;
74 3
    }
75 3
76
    /**
77
     * @param $name
78
     * @param $arguments
79
     * @return mixed
80
     */
81
    protected function detectMagicMethodAddElements($name, $arguments)
82
    {
83
        if ($this->isMagicMethodAddElements($name)) {
84
            $type = str_replace('add', '', $name);
85
            $type[0] = strtolower($type[0]);
86
            if ($this->isElementsType($type)) {
87
                return $this->magicMethodAddElement($type, $arguments);
88 3
            } else {
89
                trigger_error('Undefined element type for add operation: [' . $type . ']', E_USER_ERROR);
90 3
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97 3
     * @param $name
98
     * @return bool
99 3
     */
100
    protected function isMagicMethodAddElements($name)
101
    {
102
        return strpos($name, 'add') === 0;
103
    }
104
105 3
    /**
106
     * @param string[] $type
107 3
     * @return boolean
108
     */
109
    public function isElementsType($type)
110
    {
111
        return in_array($type, $this->getElementsTypes());
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getElementsTypes()
118
    {
119
        return $this->elementsTypes;
120
    }
121
122
    /**
123 3
     * @param array $elementsTypes
124
     */
125 3
    public function setElementsTypes($elementsTypes)
126 3
    {
127 3
        $this->elementsTypes = $elementsTypes;
128 3
    }
129
130 3
    /**
131
     * @param string[] $type
132
     * @param $arguments
133
     * @return mixed
134
     */
135
    protected function magicMethodAddElement($type, $arguments)
136
    {
137 3
        $name = $this->getElementNameFromMagicMethodArguments($arguments);
138
        $label = $this->getElementLabelFromMagicMethodArguments($arguments);
139 3
        $isRequired = $this->getElementIsRequiredFromMagicMethodArguments($arguments);
140
        $options = $this->getElementOptionsFromMagicMethodArguments($arguments);
141
142
        return $this->add($name, $label, $type, $isRequired, $options);
0 ignored issues
show
Bug introduced by
The method add() does not exist on Nip\Form\Traits\MagicMethodElementsFormTrait. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

142
        return $this->/** @scrutinizer ignore-call */ add($name, $label, $type, $isRequired, $options);
Loading history...
143
    }
144
145
    /**
146 3
     * @param $arguments
147
     * @return mixed
148 3
     */
149
    protected function getElementNameFromMagicMethodArguments($arguments)
150
    {
151
        return $arguments[0];
152
    }
153
154
    /**
155 3
     * @param $arguments
156
     * @return string|false
157 3
     */
158
    protected function getElementLabelFromMagicMethodArguments($arguments)
159
    {
160
        return isset($arguments[1]) ? $arguments[1] : false;
161
    }
162
163
    /** @noinspection PhpMethodNamingConventionInspection
164 3
     * @param $arguments
165
     * @return string|false
166 3
     */
167
    protected function getElementIsRequiredFromMagicMethodArguments($arguments)
168
    {
169
        return isset($arguments[2]) ? $arguments[2] : false;
170
    }
171
172
    /**
173
     * @param $arguments
174
     * @return string|false
175
     */
176
    protected function getElementOptionsFromMagicMethodArguments($arguments)
177
    {
178
        return isset($arguments[3]) ? $arguments[3] : false;
179
    }
180
}
181