Conditions | 39 |
Paths | > 20000 |
Total Lines | 122 |
Code Lines | 95 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 2 |
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 |
||
184 | { |
||
185 | $type = isset($my_model['_type']) ? $my_model['_type'] : 'string'; |
||
186 | $input_type = isset($my_model['_input_type']) ? $my_model['_input_type'] : 'string'; |
||
187 | $format = isset($my_model['_input_format']) ? $my_model['_input_format'] : ""; |
||
188 | $min_length = isset($my_model['_min_length']) ? $my_model['_min_length'] : null; |
||
189 | $max_length = isset($my_model['_max_length']) ? $my_model['_max_length'] : null; |
||
190 | $in_options = isset($my_model['_in_options']) ? $my_model['_in_options'] : null; |
||
191 | |||
192 | if (ModelUtils::getType($value) != $type) { |
||
193 | return false; |
||
194 | } |
||
195 | if ($input_type !== null) { |
||
196 | switch ($input_type) { |
||
197 | case 'mail': |
||
198 | $filter_check = filter_var($value, FILTER_VALIDATE_EMAIL); |
||
199 | if ($filter_check === false) { |
||
200 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
201 | "validation: INVALID_EMAIL_ADDRESS "); |
||
202 | } |
||
203 | break; |
||
204 | case 'bool': |
||
205 | $filter_check = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
||
206 | if ($filter_check === false) { |
||
207 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
208 | "validation: INVALID_BOOLEAN_VALUE "); |
||
209 | } |
||
210 | break; |
||
211 | case 'url': |
||
212 | $filter_check = filter_var($value, FILTER_VALIDATE_URL); |
||
213 | if ($filter_check === false) { |
||
214 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
215 | "validation: INVALID_URL "); |
||
216 | } |
||
217 | break; |
||
218 | case 'ip': |
||
219 | $filter_check = filter_var($value, FILTER_VALIDATE_IP); |
||
220 | if ($filter_check === false) { |
||
221 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
222 | "validation: INVALID_IP_ADDRESS "); |
||
223 | } |
||
224 | break; |
||
225 | case 'mac_address': |
||
226 | $filter_check = filter_var($value, FILTER_VALIDATE_MAC); |
||
227 | if ($filter_check === false) { |
||
228 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
229 | "validation: INVALID_MAC_ADDRESS "); |
||
230 | } |
||
231 | break; |
||
232 | case 'date': |
||
233 | $regex = "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/"; |
||
234 | $options = array("options"=>array("regexp"=> $regex)); |
||
235 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, $options); |
||
236 | if ($filter_check === false) { |
||
237 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
238 | "validation: INVALID_FORMAT "); |
||
239 | } |
||
240 | break; |
||
241 | case 'time': |
||
242 | $regex = "/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])$/"; |
||
243 | $options = array("options"=>array("regexp"=> $regex)); |
||
244 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, $options); |
||
245 | if ($filter_check === false) { |
||
246 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
247 | "validation: INVALID_FORMAT "); |
||
248 | } |
||
249 | break; |
||
250 | case 'datetime': |
||
251 | $date_part = "[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])"; |
||
252 | $time_part = "([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])"; |
||
253 | $regex = "/^".$date_part." ".$time_part."$/"; |
||
254 | $options = array("options"=>array("regexp"=> $regex)); |
||
255 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, $options); |
||
256 | if ($filter_check === false) { |
||
257 | $exception_message="Error for value '".$value."' for '".$key."' |
||
258 | couldn't pass the validation: INVALID_FORMAT"; |
||
259 | throw new \Exception($exception_message); |
||
260 | } |
||
261 | break; |
||
262 | case 'regex': |
||
263 | $regex = "/^".$format."$/"; |
||
264 | $options = array("options"=>array("regexp"=> $regex)); |
||
265 | $filter_check = filter_var($value, FILTER_VALIDATE_REGEXP, $options); |
||
266 | if ($filter_check === false) { |
||
267 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
268 | "validation: INVALID_FORMAT "); |
||
269 | } |
||
270 | break; |
||
271 | } |
||
272 | } |
||
273 | switch ($type) { |
||
274 | case 'integer': |
||
275 | case 'float': |
||
276 | if ($min_length !== null) { |
||
277 | if ($value<$min_length) { |
||
278 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
279 | "validation: Must be bigger than ".$min_length." "); |
||
280 | } |
||
281 | } |
||
282 | if ($max_length !== null) { |
||
283 | if ($value>$max_length) { |
||
284 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
285 | "validation: Must be smallerr than ".$max_length." "); |
||
286 | } |
||
287 | } |
||
288 | break; |
||
289 | default: |
||
290 | if ($max_length !== null) { |
||
291 | if (strlen($value)>$max_length) { |
||
292 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the " . |
||
293 | "validation: It's length must be smaller than ".$max_length." "); |
||
294 | } |
||
295 | } |
||
296 | if ($min_length !== null) { |
||
297 | if (strlen($value)<$min_length) { |
||
298 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the ". |
||
299 | "validation: It's length must be longer than ".$min_length." "); |
||
300 | } |
||
301 | } |
||
302 | break; |
||
303 | } |
||
304 | if ($in_options !== null) { |
||
305 | if (!in_array($value, $in_options)) { |
||
306 | throw new \Exception("Error for value '".$value."' for '".$key."' couldn't pass the validation: ". |
||
404 |