Conditions | 19 |
Paths | 64 |
Total Lines | 78 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
78 | private function parseOptions(array $options): array |
||
79 | { |
||
80 | $_options = []; |
||
81 | |||
82 | $options = array_change_key_case(array_filter($options, function ($var) { |
||
83 | return !is_null($var); |
||
84 | })); |
||
85 | |||
86 | if (!empty($options['attr']['type'])) { |
||
87 | $options['type'] = $options['attr']['type']; |
||
88 | unset($options['attr']['type']); |
||
89 | } |
||
90 | |||
91 | foreach ($options as $option => $value) { |
||
92 | switch ($option) { |
||
93 | case 'default': |
||
94 | $_options['data'] = $value; |
||
95 | break; |
||
96 | case 'constraints': |
||
97 | $attributes = \json_decode($value, true); |
||
98 | |||
99 | if ((\json_last_error() !== JSON_ERROR_NONE)) { |
||
100 | $attributes = [$value]; |
||
101 | } |
||
102 | |||
103 | foreach ($attributes as $attribute => $_value) { |
||
104 | $attribute = \strtolower($attribute); |
||
105 | |||
106 | if ($attribute == 'required' || $_value == 'required') { |
||
107 | $_options['required'] = true; |
||
108 | } elseif (in_array($attribute, ['length', 'mincheck', 'maxcheck', 'check', 'equalto'])) { |
||
109 | $_options['attr']['data-parsley-' . $attribute] = $_value; |
||
110 | } elseif ($attribute == 'range') { |
||
111 | $_options['type'] = $attribute; |
||
112 | list($min, $max) = explode(',', $_value); |
||
113 | $_options['attr'] = compact('min', 'max'); |
||
114 | } elseif ($attribute == 'type') { |
||
115 | $_options['type'] = $_value; |
||
116 | } else { |
||
117 | $_options['attr'][$attribute] = $_value; |
||
118 | } |
||
119 | } |
||
120 | break; |
||
121 | case 'empty_data': |
||
122 | $_options[$option] = $value; |
||
123 | |||
124 | if (empty($_options['attr'])) { |
||
125 | $_options['attr'] = []; |
||
126 | } |
||
127 | |||
128 | $_options['attr']['placeholder'] = $value; |
||
129 | |||
130 | break; |
||
131 | default: |
||
132 | if (is_array($value) && !empty($_options[$option])) { |
||
133 | $_options[$option] = array_merge_recursive($_options[$option], $value); |
||
134 | } else { |
||
135 | $_options[$option] = $value; |
||
136 | } |
||
137 | break; |
||
138 | } |
||
139 | |||
140 | unset($options[$option]); |
||
141 | } |
||
142 | |||
143 | if (!empty($_options['type'])) { |
||
144 | $_type = strtolower($_options['type']); |
||
145 | |||
146 | switch ($_type) { |
||
147 | case 'digits': |
||
148 | case 'alphanum': |
||
149 | $_options['attr']['data-parsley-type'] = $_type; |
||
150 | $_options['type'] = 'text'; |
||
151 | break; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return $_options; |
||
156 | } |
||
158 |