Conditions | 32 |
Paths | 270 |
Total Lines | 139 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
130 | public function validate() |
||
131 | { |
||
132 | $this->valid = null; |
||
133 | |||
134 | $this->setValid(true); |
||
135 | $elements = $this->form->getChildren(); |
||
136 | |||
137 | foreach ($elements as $label => $element) |
||
138 | { |
||
139 | if (!$element->isFormControl()) |
||
140 | continue; |
||
141 | |||
142 | $attribs = $element->getAttributes(); |
||
143 | |||
144 | $all_attribs = []; |
||
145 | |||
146 | foreach ($attribs as $attr) |
||
147 | { |
||
148 | $all_attribs[$attr->getName()] = $attr->getValue(); |
||
149 | } |
||
150 | |||
151 | $required = array_key_exists('required', $all_attribs) ? $all_attribs["required"] : false; |
||
152 | |||
153 | foreach ($attribs as $attr) |
||
154 | { |
||
155 | $name = $attr->getName(); |
||
156 | $value = $attr->getValue(); |
||
157 | |||
158 | $attrib = $element->getAttribute("value"); |
||
159 | $form_value = (!is_null($attrib)) ? $attrib->getValue() : null; |
||
160 | |||
161 | $validator = null; |
||
162 | |||
163 | switch ($name) |
||
164 | { |
||
165 | case 'required': |
||
166 | |||
167 | $validator = new NotEmpty(); |
||
168 | break; |
||
169 | |||
170 | case 'minlength': |
||
171 | |||
172 | $validator = new StringLength(['min' => $value]); |
||
173 | break; |
||
174 | |||
175 | case 'maxlength': |
||
176 | |||
177 | $validator = new StringLength(['max' => $value]); |
||
178 | break; |
||
179 | |||
180 | case 'type': |
||
181 | |||
182 | switch ($value) |
||
183 | { |
||
184 | case 'number': |
||
185 | |||
186 | $validator = new Digits(); |
||
187 | break; |
||
188 | |||
189 | case 'email': |
||
190 | |||
191 | $validator = new EmailAddress(); |
||
192 | break; |
||
193 | |||
194 | case 'date': |
||
195 | |||
196 | $validator = new Date(); |
||
197 | break; |
||
198 | |||
199 | case 'url': |
||
200 | |||
201 | $validator = new Uri(); |
||
202 | break; |
||
203 | } |
||
204 | |||
205 | break; |
||
206 | |||
207 | case 'min': |
||
208 | |||
209 | if (array_key_exists('type', $all_attribs) && in_array($all_attribs['type'], ['number', 'range'])) |
||
210 | $validator = new GreaterThan(['min' => $value, 'inclusive' => true]); |
||
211 | else |
||
212 | throw new \LogicException("The input type must be 'range' or 'number'"); |
||
213 | |||
214 | break; |
||
215 | |||
216 | case 'max': |
||
217 | |||
218 | if (array_key_exists('type', $all_attribs) && in_array($all_attribs['type'], ['number', 'range'])) |
||
219 | $validator = new LessThan(['max' => $value, 'inclusive' => true]); |
||
220 | else |
||
221 | throw new \LogicException("The input type must be 'range' or 'number'"); |
||
222 | |||
223 | break; |
||
224 | |||
225 | case 'step': |
||
226 | |||
227 | $baseValue = (array_key_exists('min', $all_attribs)) ? $all_attribs['min'] : 0; |
||
228 | |||
229 | if (array_key_exists('type', $all_attribs) && in_array($all_attribs['type'], ['range'])) |
||
230 | $validator = new Step(['baseValue' => $baseValue, 'step' => $value]); |
||
231 | else |
||
232 | throw new \LogicException("The input type must be 'range'"); |
||
233 | |||
234 | break; |
||
235 | |||
236 | case 'data-validators': |
||
237 | |||
238 | if (!is_array($value)) |
||
239 | throw new \InvalidArgumentException("Invalid type given. Array expected in 'data-validators' attribute."); |
||
240 | |||
241 | foreach ($value as $class => $params) |
||
242 | { |
||
243 | $className = "\Zend\Validator\\" . $class; |
||
244 | |||
245 | if (!class_exists($className)) |
||
246 | { |
||
247 | $className = "\Zend\I18n\Validator\\" . $class; |
||
248 | |||
249 | if (!class_exists($className)) |
||
250 | throw new \RuntimeException("The class '$userInputClass' or '$className' does not exists"); |
||
251 | } |
||
252 | |||
253 | $validator = new $className($params); |
||
254 | |||
255 | $validator->setTranslator($this->translator); |
||
256 | $this->_validate($validator, $form_value, $label, $required); |
||
257 | } |
||
258 | |||
259 | break; |
||
260 | } |
||
261 | |||
262 | if (in_array( |
||
263 | $name, |
||
264 | ['required', 'digits', 'minlength', 'maxlength', 'type', 'min', 'max', 'date', 'step'] |
||
265 | ) && !is_null($validator)) |
||
266 | { |
||
267 | $validator->setTranslator($this->translator); |
||
268 | $this->_validate($validator, $form_value, $label, $required); |
||
269 | } |
||
345 | } |