Passed
Push — master ( a22205...112e67 )
by Gabriel
04:04 queued 13s
created

getElementLabelFromMagicMethodArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
crap 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 addDateinput($name, $label = false, $isRequired = false)
16
 * @method addRadioGroup($name, $label = false, $isRequired = false)
17
 * @method addBsRadioGroup($name, $label = false, $isRequired = false)
18
 * @method addTextarea($name, $label = false, $isRequired = false)
19
 * @method addTextSimpleEditor($name, $label = false, $isRequired = false)
20
 * @method addFile($name, $label = false, $isRequired = false)
21
 * @method addMultiElement($name, $label = false, $isRequired = false)
22
 */
23
trait MagicMethodElementsFormTrait
24
{
25
    protected $elementsTypes = [
26
        'input',
27
        'hidden',
28
        'password',
29
        'hash',
30
        'file',
31
        'multiElement',
32
        'dateinput',
33
        'dateselect',
34
        'timeselect',
35
        'textarea',
36
        'texteditor',
37
        'textSimpleEditor',
38
        'textMiniEditor',
39
        'select',
40
        'radio',
41
        'radioGroup',
42
        'checkbox',
43
        'checkboxGroup',
44
        'html',
45
    ];
46
47
    /**
48
     * @param $name
49
     * @param $arguments
50
     * @return AbstractForm|self
51
     */
52 3
    public function __call($name, $arguments)
53
    {
54 3
        $addElements = $this->detectMagicMethodAddElements($name, $arguments);
55 3
        if ($addElements !== false) {
56 3
            return $addElements;
57
        }
58
59
        trigger_error('Call to undefined method: [' . $name . ']', E_USER_ERROR);
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param $name
66
     * @param $arguments
67
     * @return mixed
68
     */
69 3
    protected function detectMagicMethodAddElements($name, $arguments)
70
    {
71 3
        if ($this->isMagicMethodAddElements($name)) {
72 3
            $type = str_replace('add', '', $name);
73 3
            $type[0] = strtolower($type[0]);
74 3
            if ($this->isElementsType($type)) {
75 3
                return $this->magicMethodAddElement($type, $arguments);
76
            } else {
77
                trigger_error('Undefined element type for add operation: [' . $type . ']', E_USER_ERROR);
78
            }
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * @param $name
86
     * @return bool
87
     */
88 3
    protected function isMagicMethodAddElements($name)
89
    {
90 3
        return strpos($name, 'add') === 0;
91
    }
92
93
    /**
94
     * @param string[] $type
95
     * @return boolean
96
     */
97 3
    public function isElementsType($type)
98
    {
99 3
        return in_array($type, $this->getElementsTypes());
100
    }
101
102
    /**
103
     * @return array
104
     */
105 3
    public function getElementsTypes()
106
    {
107 3
        return $this->elementsTypes;
108
    }
109
110
    /**
111
     * @param array $elementsTypes
112
     */
113
    public function setElementsTypes($elementsTypes)
114
    {
115
        $this->elementsTypes = $elementsTypes;
116
    }
117
118
    /**
119
     * @param string[] $type
120
     * @param $arguments
121
     * @return mixed
122
     */
123 3
    protected function magicMethodAddElement($type, $arguments)
124
    {
125 3
        $name = $this->getElementNameFromMagicMethodArguments($arguments);
126 3
        $label = $this->getElementLabelFromMagicMethodArguments($arguments);
127 3
        $isRequired = $this->getElementIsRequiredFromMagicMethodArguments($arguments);
128 3
        $options = $this->getElementOptionsFromMagicMethodArguments($arguments);
129
130 3
        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

130
        return $this->/** @scrutinizer ignore-call */ add($name, $label, $type, $isRequired, $options);
Loading history...
131
    }
132
133
    /**
134
     * @param $arguments
135
     * @return mixed
136
     */
137 3
    protected function getElementNameFromMagicMethodArguments($arguments)
138
    {
139 3
        return $arguments[0];
140
    }
141
142
    /**
143
     * @param $arguments
144
     * @return string|false
145
     */
146 3
    protected function getElementLabelFromMagicMethodArguments($arguments)
147
    {
148 3
        return isset($arguments[1]) ? $arguments[1] : false;
149
    }
150
151
    /** @noinspection PhpMethodNamingConventionInspection
152
     * @param $arguments
153
     * @return string|false
154
     */
155 3
    protected function getElementIsRequiredFromMagicMethodArguments($arguments)
156
    {
157 3
        return isset($arguments[2]) ? $arguments[2] : false;
158
    }
159
160
    /**
161
     * @param $arguments
162
     * @return string|false
163
     */
164 3
    protected function getElementOptionsFromMagicMethodArguments($arguments)
165
    {
166 3
        return isset($arguments[3]) ? $arguments[3] : false;
167
    }
168
}
169