Total Complexity | 76 |
Total Lines | 706 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Validation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Validation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Validation |
||
24 | { |
||
25 | /** |
||
26 | * Validation::isOptional |
||
27 | * |
||
28 | * @return bool |
||
29 | */ |
||
30 | public static function isOptional() |
||
31 | { |
||
32 | return true; |
||
33 | } |
||
34 | |||
35 | // ------------------------------------------------------------------------ |
||
36 | |||
37 | /** |
||
38 | * Validation::isRequired |
||
39 | * |
||
40 | * @param $string |
||
41 | * |
||
42 | * @return bool |
||
43 | */ |
||
44 | public static function isRequired($string = null) |
||
45 | { |
||
46 | if (empty($string) OR strlen($string) == 0) { |
||
47 | return false; |
||
48 | } |
||
49 | |||
50 | return true; |
||
51 | } |
||
52 | |||
53 | // ------------------------------------------------------------------------ |
||
54 | |||
55 | /** |
||
56 | * Validation::isMatch |
||
57 | * |
||
58 | * @param $string |
||
59 | * @param $match |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public static function isMatch($string, $match) |
||
64 | { |
||
65 | if ($string === $match) { |
||
66 | return true; |
||
67 | } |
||
68 | |||
69 | return false; |
||
70 | } |
||
71 | |||
72 | // ------------------------------------------------------------------------ |
||
73 | |||
74 | /** |
||
75 | * Validation::isRegexMatch |
||
76 | * |
||
77 | * Performs a Regular Expression match test. |
||
78 | * |
||
79 | * @param string |
||
80 | * @param string regex |
||
|
|||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | public static function isRegexMatch($string, $regex) |
||
85 | { |
||
86 | return (bool)preg_match($regex, $string); |
||
87 | } |
||
88 | |||
89 | // ------------------------------------------------------------------------ |
||
90 | |||
91 | /** |
||
92 | * Validation::isFloat |
||
93 | * |
||
94 | * @param $string |
||
95 | * |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public static function isFloat($string) |
||
99 | { |
||
100 | return filter_var($string, FILTER_VALIDATE_FLOAT); |
||
101 | } |
||
102 | |||
103 | // ------------------------------------------------------------------------ |
||
104 | |||
105 | /** |
||
106 | * Validation::isMaxLength |
||
107 | * |
||
108 | * Max Length |
||
109 | * |
||
110 | * @param string |
||
111 | * @param string |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | public static function isMaxLength($string, $length) |
||
116 | { |
||
117 | if ( ! is_numeric($length)) { |
||
118 | return false; |
||
119 | } |
||
120 | |||
121 | return ($length >= mb_strlen($string)); |
||
122 | } |
||
123 | |||
124 | // -------------------------------------------------------------------- |
||
125 | |||
126 | /** |
||
127 | * Validation::isExactLength |
||
128 | * |
||
129 | * Exact Length |
||
130 | * |
||
131 | * @param string |
||
132 | * @param string |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | public static function isExactLength($string, $length) |
||
137 | { |
||
138 | if ( ! is_numeric($length)) { |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | return (mb_strlen($string) === (int)$length); |
||
143 | } |
||
144 | |||
145 | // ------------------------------------------------------------------------ |
||
146 | |||
147 | /** |
||
148 | * Validation::isDimension |
||
149 | * |
||
150 | * @param $string |
||
151 | * @param string $format |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | public static function isDimension($string, $format = 'W x H x L') |
||
156 | { |
||
157 | $string = strtolower($string); |
||
158 | $string = preg_replace('/\s+/', '', $string); |
||
159 | $x_string = explode('x', $string); |
||
160 | |||
161 | $format = strtolower($format); |
||
162 | $format = preg_replace('/\s+/', '', $format); |
||
163 | $x_format = explode('x', $format); |
||
164 | |||
165 | if (count($x_string) == count($x_format)) { |
||
166 | return true; |
||
167 | } |
||
168 | |||
169 | return false; |
||
170 | } |
||
171 | |||
172 | // ------------------------------------------------------------------------ |
||
173 | |||
174 | /** |
||
175 | * Validation::isIpv4 |
||
176 | * |
||
177 | * @param $string |
||
178 | * |
||
179 | * @return mixed |
||
180 | */ |
||
181 | public static function isIpv4($string) |
||
182 | { |
||
183 | return filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
||
184 | } |
||
185 | |||
186 | // ------------------------------------------------------------------------ |
||
187 | |||
188 | /** |
||
189 | * Validation::isIpv6 |
||
190 | * |
||
191 | * @param $string |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public static function isIpv6($string) |
||
196 | { |
||
197 | return filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); |
||
198 | } |
||
199 | |||
200 | // ------------------------------------------------------------------------ |
||
201 | |||
202 | /** |
||
203 | * Validation::isUrl |
||
204 | * |
||
205 | * @param $string |
||
206 | * |
||
207 | * @return bool|mixed |
||
208 | */ |
||
209 | public static function isUrl($string) |
||
210 | { |
||
211 | if (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $string, $matches)) { |
||
212 | if (empty($matches[ 2 ])) { |
||
213 | return false; |
||
214 | } elseif ( ! in_array($matches[ 1 ], ['http', 'https'], true)) { |
||
215 | return false; |
||
216 | } |
||
217 | |||
218 | $string = $matches[ 2 ]; |
||
219 | } |
||
220 | |||
221 | $string = 'http://' . $string; |
||
222 | |||
223 | return filter_var($string, FILTER_VALIDATE_URL); |
||
224 | } |
||
225 | |||
226 | // ------------------------------------------------------------------------ |
||
227 | |||
228 | /** |
||
229 | * Validation::isEmail |
||
230 | * |
||
231 | * @param $string |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public static function isEmail($string) |
||
236 | { |
||
237 | return (bool)filter_var($string, FILTER_VALIDATE_EMAIL); |
||
238 | } |
||
239 | |||
240 | // ------------------------------------------------------------------------ |
||
241 | |||
242 | /** |
||
243 | * Validation::isDomain |
||
244 | * |
||
245 | * @param $string |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | public static function isDomain($string) |
||
250 | { |
||
251 | return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $string) //valid chars check |
||
252 | && preg_match("/^.{1,253}$/", $string) //overall length check |
||
253 | && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $string)); //length of each label |
||
254 | } |
||
255 | |||
256 | // ------------------------------------------------------------------------ |
||
257 | |||
258 | /** |
||
259 | * Validation::isBool |
||
260 | * |
||
261 | * @param $string |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | public static function isBool($string) |
||
266 | { |
||
267 | return (bool)filter_var($string, FILTER_VALIDATE_BOOLEAN); |
||
268 | } |
||
269 | |||
270 | // -------------------------------------------------------------------- |
||
271 | |||
272 | /** |
||
273 | * Validation::isAlpha |
||
274 | * |
||
275 | * @param string $string |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public static function isAlpha($string) |
||
280 | { |
||
281 | return (bool)ctype_alpha($string); |
||
282 | } |
||
283 | |||
284 | // ------------------------------------------------------------------------ |
||
285 | |||
286 | /** |
||
287 | * Validation::isAlphaSpaces |
||
288 | * |
||
289 | * @param string $string |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | public static function isAlphaSpaces($string) |
||
294 | { |
||
295 | return (bool)preg_match('/^[A-Z ]+$/i', $string); |
||
296 | } |
||
297 | |||
298 | // -------------------------------------------------------------------- |
||
299 | |||
300 | /** |
||
301 | * Alpha-numeric |
||
302 | * |
||
303 | * @param string $string |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | public static function isAlphaNumeric($string) |
||
308 | { |
||
309 | return (bool)ctype_alnum((string)$string); |
||
310 | } |
||
311 | |||
312 | // -------------------------------------------------------------------- |
||
313 | |||
314 | /** |
||
315 | * Validation::isAlphaNumericSpaces |
||
316 | * |
||
317 | * Alpha-numeric w/ spaces |
||
318 | * |
||
319 | * @param string $string |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public static function isAlphaNumericSpaces($string) |
||
324 | { |
||
325 | return (bool)preg_match('/^[A-Z0-9 ]+$/i', $string); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Validation::isAlphaDash |
||
330 | * |
||
331 | * Alpha-numeric with underscores and dashes |
||
332 | * |
||
333 | * @param string |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | public static function isAlphaDash($string) |
||
338 | { |
||
339 | return (bool)preg_match('/^[a-z0-9-]+$/i', $string); |
||
340 | } |
||
341 | |||
342 | // ------------------------------------------------------------------------ |
||
343 | |||
344 | /** |
||
345 | * Validation::isAlphaUnderscore |
||
346 | * |
||
347 | * Alpha-numeric with underscores and dashes |
||
348 | * |
||
349 | * @param string |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | public static function isAlphaUnderscore($string) |
||
354 | { |
||
355 | return (bool)preg_match('/^[a-z0-9_]+$/i', $string); |
||
356 | } |
||
357 | |||
358 | // ------------------------------------------------------------------------ |
||
359 | |||
360 | /** |
||
361 | * Validation::isAlphaUnderscoreDash |
||
362 | * |
||
363 | * Alpha-numeric with underscores and dashes |
||
364 | * |
||
365 | * @param string |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | public static function isAlphaUnderscoreDash($string) |
||
370 | { |
||
371 | return (bool)preg_match('/^[a-z0-9_-]+$/i', $string); |
||
372 | } |
||
373 | |||
374 | // ------------------------------------------------------------------------ |
||
375 | |||
376 | /** |
||
377 | * Validation::isNumeric |
||
378 | * |
||
379 | * @param string |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | public static function isNumeric($str) |
||
384 | { |
||
385 | return (bool)preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); |
||
386 | |||
387 | } |
||
388 | |||
389 | // ------------------------------------------------------------------------ |
||
390 | |||
391 | /** |
||
392 | * Validation::isInteger |
||
393 | * |
||
394 | * @param string |
||
395 | * |
||
396 | * @return bool |
||
397 | */ |
||
398 | public static function isInteger($str) |
||
399 | { |
||
400 | return (bool)preg_match('/^[\-+]?[0-9]+$/', $str); |
||
401 | } |
||
402 | |||
403 | // ------------------------------------------------------------------------ |
||
404 | |||
405 | /** |
||
406 | * Validation::isDecimal |
||
407 | * |
||
408 | * Decimal number |
||
409 | * |
||
410 | * @param string |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public static function isDecimal($string) |
||
415 | { |
||
416 | return (bool)preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $string); |
||
417 | } |
||
418 | |||
419 | // ------------------------------------------------------------------------ |
||
420 | |||
421 | /** |
||
422 | * Validation::isGreater |
||
423 | * |
||
424 | * Greater than |
||
425 | * |
||
426 | * @param string |
||
427 | * @param int |
||
428 | * |
||
429 | * @return bool |
||
430 | */ |
||
431 | public static function isGreater($string, $min) |
||
432 | { |
||
433 | return is_numeric($string) ? ($string > $min) : false; |
||
434 | } |
||
435 | |||
436 | // ------------------------------------------------------------------------ |
||
437 | |||
438 | /** |
||
439 | * Validation::isGreaterEqual |
||
440 | * |
||
441 | * Equal to or Greater than |
||
442 | * |
||
443 | * @param string |
||
444 | * @param int |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | public static function isGreaterEqual($string, $min) |
||
449 | { |
||
450 | return (bool)is_numeric($string) ? ($string >= $min) : false; |
||
451 | } |
||
452 | |||
453 | // -------------------------------------------------------------------- |
||
454 | |||
455 | /** |
||
456 | * Validation::isLess |
||
457 | * |
||
458 | * Less than |
||
459 | * |
||
460 | * @param string |
||
461 | * @param int |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | public static function isLess($string, $max) |
||
466 | { |
||
467 | return (bool)is_numeric($string) ? ($string < $max) : false; |
||
468 | } |
||
469 | |||
470 | // -------------------------------------------------------------------- |
||
471 | |||
472 | /** |
||
473 | * Validation::isLessEqual |
||
474 | * |
||
475 | * Equal to or Less than |
||
476 | * |
||
477 | * @param string |
||
478 | * @param int |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | public static function isLessEqual($string, $max) |
||
483 | { |
||
484 | return (bool)is_numeric($string) ? ($string <= $max) : false; |
||
485 | } |
||
486 | |||
487 | // -------------------------------------------------------------------- |
||
488 | |||
489 | /** |
||
490 | * Validation::isListed |
||
491 | * |
||
492 | * Value should be within an array of values |
||
493 | * |
||
494 | * @param string |
||
495 | * @param string |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | public static function isListed($string, $list) |
||
507 | } |
||
508 | |||
509 | // -------------------------------------------------------------------- |
||
510 | |||
511 | /** |
||
512 | * Validation::isNatural |
||
513 | * |
||
514 | * Is a Natural number (0,1,2,3, etc.) |
||
515 | * |
||
516 | * @param string |
||
517 | * |
||
518 | * @return bool |
||
519 | */ |
||
520 | public static function isNatural($string) |
||
521 | { |
||
522 | return (bool)ctype_digit((string)$string); |
||
523 | } |
||
524 | |||
525 | // -------------------------------------------------------------------- |
||
526 | |||
527 | /** |
||
528 | * Validation::isNaturalNoZero |
||
529 | * |
||
530 | * Is a Natural number, but not a zero (1,2,3, etc.) |
||
531 | * |
||
532 | * @param string |
||
533 | * |
||
534 | * @return bool |
||
535 | */ |
||
536 | public static function isNaturalNoZero($string) |
||
537 | { |
||
538 | return (bool)($string != 0 && ctype_digit((string)$string)); |
||
539 | } |
||
540 | |||
541 | // -------------------------------------------------------------------- |
||
542 | |||
543 | /** |
||
544 | * Validation::isMd5 |
||
545 | * |
||
546 | * @param $string |
||
547 | * |
||
548 | * @return int |
||
549 | */ |
||
550 | public static function isMd5($string) |
||
551 | { |
||
552 | return preg_match('/^[a-f0-9]{32}$/i', $string); |
||
553 | } |
||
554 | |||
555 | // -------------------------------------------------------------------- |
||
556 | |||
557 | /** |
||
558 | * Validation::isMsisdn |
||
559 | * |
||
560 | * @param $string |
||
561 | * @param string $leading |
||
562 | * |
||
563 | * @return bool |
||
564 | */ |
||
565 | public static function isMsisdn($string, $leading = '62') |
||
566 | { |
||
567 | return (bool)preg_match('/^(' . $leading . '[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $string); |
||
568 | } |
||
569 | |||
570 | // -------------------------------------------------------------------- |
||
571 | |||
572 | /** |
||
573 | * Validation::isDate |
||
574 | * |
||
575 | * @param $string |
||
576 | * @param string $format |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | public static function isDate($string, $format = 'Y-m-d') |
||
581 | { |
||
582 | $dateTime = \DateTime::createFromFormat($format, $string); |
||
583 | |||
584 | return (bool)$dateTime !== false && ! array_sum($dateTime->getLastErrors()); |
||
585 | } |
||
586 | |||
587 | // -------------------------------------------------------------------- |
||
588 | |||
589 | /** |
||
590 | * Validation::isPassword |
||
591 | * |
||
592 | * @param string $string |
||
593 | * @param int $length |
||
594 | * @param string $format |
||
595 | * |
||
596 | * @return bool |
||
597 | */ |
||
598 | public static function isPassword($string, $length = 8, $format = 'uppercase, lowercase, number, special') |
||
642 | } |
||
643 | |||
644 | // -------------------------------------------------------------------- |
||
645 | |||
646 | /** |
||
647 | * Validation::isMinLength |
||
648 | * |
||
649 | * Minimum Length |
||
650 | * |
||
651 | * @param string |
||
652 | * @param string |
||
653 | * |
||
654 | * @return bool |
||
655 | */ |
||
656 | public static function isMinLength($string, $length) |
||
667 | } |
||
668 | |||
669 | // -------------------------------------------------------------------- |
||
670 | |||
671 | /** |
||
672 | * Validation::isAscii |
||
673 | * |
||
674 | * Chceks if a string is an v4 or v6 ip address. |
||
675 | * |
||
676 | * @param string $string String to check |
||
677 | * @param string $which ipv4 | ipv6 |
||
678 | * |
||
679 | * @return bool |
||
680 | */ |
||
681 | public static function isValidIp($string, $which = null) |
||
682 | { |
||
683 | switch (strtolower($which)) { |
||
684 | case 'ipv4': |
||
685 | $which = FILTER_FLAG_IPV4; |
||
686 | break; |
||
687 | case 'ipv6': |
||
688 | $which = FILTER_FLAG_IPV6; |
||
689 | break; |
||
690 | default: |
||
691 | $which = null; |
||
692 | break; |
||
693 | } |
||
694 | |||
695 | return (bool)filter_var($string, FILTER_VALIDATE_IP, $which); |
||
696 | } |
||
697 | |||
698 | // -------------------------------------------------------------------- |
||
699 | |||
700 | /** |
||
701 | * Validation::isAscii |
||
702 | * |
||
703 | * Tests if a string is standard 7-bit ASCII or not. |
||
704 | * |
||
705 | * @param string $string String to check |
||
706 | * |
||
707 | * @return bool |
||
708 | */ |
||
709 | public static function isAscii($string) |
||
710 | { |
||
711 | return (bool)(preg_match('/[^\x00-\x7F]/S', $string) === 0); |
||
712 | } |
||
713 | |||
714 | // -------------------------------------------------------------------- |
||
715 | |||
716 | /** |
||
717 | * Validation::isBase64 |
||
718 | * |
||
719 | * Tests a string for characters outside of the Base64 alphabet |
||
720 | * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 |
||
721 | * |
||
722 | * @param string |
||
723 | * |
||
724 | * @return bool |
||
725 | */ |
||
726 | public function isBase64($string) |
||
729 | } |
||
730 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths