Complex classes like Assert 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Assert, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Assert |
||
50 | { |
||
51 | /** @var Assert */ |
||
52 | protected static $validator; |
||
53 | |||
54 | /** @var string */ |
||
55 | protected static $exceptionClass; |
||
56 | |||
57 | /** @var string */ |
||
58 | protected $name; |
||
59 | |||
60 | /** @var int|float|bool|string|resource|array|null */ |
||
61 | protected $value; |
||
62 | |||
63 | /** |
||
64 | * Creates validator instance for variable, first fail check will throw an exception |
||
65 | * |
||
66 | * @param int|float|string|resource|array|null $value |
||
67 | * @param string $name |
||
68 | 133 | * @param null|string $exceptionClass |
|
69 | * @return static |
||
70 | 133 | * @throws InvalidNotObjectException |
|
71 | 1 | * @throws InvalidStringException |
|
72 | */ |
||
73 | public static function assert($value, $name = 'value', $exceptionClass = null) |
||
97 | |||
98 | /** |
||
99 | * Return current validation value |
||
100 | * |
||
101 | * @return int|float|bool|string|resource|array |
||
102 | */ |
||
103 | public function get() |
||
107 | 1 | ||
108 | /** |
||
109 | * @param \Closure $callback (Assert $value) |
||
110 | 3 | * @return $this |
|
111 | 1 | * @throws InvalidArrayException |
|
112 | * @throws InvalidIntException |
||
113 | */ |
||
114 | 2 | public function forList(\Closure $callback) |
|
135 | |||
136 | /** |
||
137 | 4 | * @param \Closure $callback (Assert $key, Assert $value) |
|
138 | 1 | * @return $this |
|
139 | * @throws InvalidArrayException |
||
140 | */ |
||
141 | 3 | public function forMap(\Closure $callback) |
|
166 | |||
167 | 5 | /** |
|
168 | 1 | * @param int $length |
|
169 | 4 | * @return $this |
|
170 | 1 | * @throws InvalidIntException |
|
171 | * @throws InvalidStringLengthException |
||
172 | * @throws NumberNotPositiveException |
||
173 | 3 | * @throws InvalidStringException |
|
174 | 1 | * @throws \Exception|\Throwable |
|
175 | 2 | */ |
|
176 | 1 | public function length($length) |
|
192 | |||
193 | /** |
||
194 | * Soft check if value has length $from <= $length <= to. Runs only after string validation |
||
195 | 7 | * |
|
196 | * @param int $from |
||
197 | 7 | * @param int $to |
|
198 | 1 | * @return $this |
|
199 | 6 | * @throws InvalidIntException |
|
200 | 1 | * @throws LengthNotBetweenException |
|
201 | 5 | * @throws NumberNotPositiveException |
|
202 | 1 | * @throws NumberNotGreaterException |
|
203 | 4 | * @throws NumberNotLessException |
|
204 | 1 | * @throws InvalidStringException |
|
205 | * @throws \Exception|\Throwable |
||
206 | */ |
||
207 | 3 | public function lengthBetween($from, $to) |
|
231 | |||
232 | 5 | /** |
|
233 | 1 | * Soft check if value has length less than $length. Runs only after string validation |
|
234 | 4 | * |
|
235 | 1 | * @param int $length |
|
236 | * @return $this |
||
237 | * @throws InvalidIntException |
||
238 | 3 | * @throws LengthNotLessException |
|
239 | 1 | * @throws NumberNotPositiveException |
|
240 | 2 | * @throws InvalidStringException |
|
241 | 1 | * @throws \Exception|\Throwable |
|
242 | */ |
||
243 | public function lengthLess($length) |
||
259 | 5 | ||
260 | 1 | /** |
|
261 | 4 | * Soft check if value has length less than $length. Runs only after notEmpty and string validations |
|
262 | 1 | * |
|
263 | * @param int $length |
||
264 | * @return $this |
||
265 | 3 | * @throws InvalidIntException |
|
266 | 1 | * @throws LengthNotGreaterException |
|
267 | 2 | * @throws NumberNotPositiveException |
|
268 | 1 | * @throws InvalidStringException |
|
269 | * @throws \Exception|\Throwable |
||
270 | */ |
||
271 | 1 | public function lengthGreater($length) |
|
287 | 3 | ||
288 | 1 | /** |
|
289 | * Check if value is in array (in_array strict) |
||
290 | * |
||
291 | 2 | * @param array $range |
|
292 | 1 | * @return $this |
|
293 | * @throws InvalidArrayException |
||
294 | * @throws InvalidNotEmptyException |
||
295 | 1 | * @throws ValueNotInArrayException |
|
296 | * @throws \Exception|\Throwable |
||
297 | */ |
||
298 | public function inArray($range) |
||
312 | |||
313 | /** |
||
314 | * Check if value is array |
||
315 | * |
||
316 | * @return $this |
||
317 | * @throws InvalidArrayException |
||
318 | * @throws \Exception|\Throwable |
||
319 | */ |
||
320 | public function isArray() |
||
328 | 3 | ||
329 | 1 | /** |
|
330 | * Check if array key exists |
||
331 | * |
||
332 | 2 | * @param string|int $key |
|
333 | 1 | * @return $this |
|
334 | * @throws ArrayKeyNotExistsException |
||
335 | * @throws InvalidArrayException |
||
336 | 1 | * @throws InvalidIntOrStringException |
|
337 | * @throws \Exception|\Throwable |
||
338 | */ |
||
339 | public function hasKey($key) |
||
355 | |||
356 | /** |
||
357 | 3 | * Check if array elements count is same as $count |
|
358 | 1 | * |
|
359 | * @param int $count |
||
360 | * @return $this |
||
361 | 2 | * @throws InvalidArrayCountException |
|
362 | 1 | * @throws InvalidArrayException |
|
363 | * @throws InvalidIntException |
||
364 | * @throws NumberNotGreaterException |
||
365 | 1 | * @throws \Exception|\Throwable |
|
366 | */ |
||
367 | public function count($count) |
||
385 | 1 | ||
386 | /** |
||
387 | * Soft check that $from <= value <= $to |
||
388 | 3 | * |
|
389 | 1 | * @param float|int $from |
|
390 | 2 | * @param float|int $to |
|
391 | 1 | * @return $this |
|
392 | * @throws NumberNotBetweenException |
||
393 | * @throws InvalidIntOrFloatException |
||
394 | 1 | * @throws NumberNotLessStrictlyException |
|
395 | * @throws \Exception|\Throwable |
||
396 | */ |
||
397 | public function between($from, $to) |
||
415 | |||
416 | /** |
||
417 | 3 | * Strict check that $from < value < $to |
|
418 | 1 | * |
|
419 | 2 | * @param float|int $from |
|
420 | 1 | * @param float|int $to |
|
421 | * @return $this |
||
422 | * @throws InvalidIntOrFloatException |
||
423 | 1 | * @throws NumberNotBetweenStrictlyException |
|
424 | * @throws NumberNotLessStrictlyException |
||
425 | * @throws \Exception|\Throwable |
||
426 | */ |
||
427 | public function betweenStrict($from, $to) |
||
445 | |||
446 | /** |
||
447 | * Check if value is boolean (is_bool) |
||
448 | 3 | * |
|
449 | * @return $this |
||
450 | 3 | * @throws InvalidBoolException |
|
451 | 1 | * @throws \Exception|\Throwable |
|
452 | 2 | */ |
|
453 | 1 | public function bool() |
|
461 | |||
462 | /** |
||
463 | * Check if value is digit (ctype_digit) |
||
464 | * |
||
465 | 2 | * @return $this |
|
466 | * @throws InvalidDigitException |
||
467 | 2 | * @throws InvalidStringException |
|
468 | 1 | * @throws \Exception|\Throwable |
|
469 | */ |
||
470 | public function digit() |
||
480 | 2 | ||
481 | /** |
||
482 | 2 | * Check if value is empty (empty) |
|
483 | 1 | * |
|
484 | * @return $this |
||
485 | * @throws InvalidEmptyException |
||
486 | 1 | * @throws \Exception|\Throwable |
|
487 | */ |
||
488 | public function isEmpty() |
||
496 | |||
497 | 2 | /** |
|
498 | 1 | * Check if value is not empty (empty) |
|
499 | * |
||
500 | * @return $this |
||
501 | 1 | * @throws InvalidNotEmptyException |
|
502 | * @throws \Exception|\Throwable |
||
503 | */ |
||
504 | public function notEmpty() |
||
512 | 7 | ||
513 | 4 | /** |
|
514 | * Check if value is float (is_float) |
||
515 | * |
||
516 | 3 | * @return $this |
|
517 | * @throws InvalidFloatException |
||
518 | * @throws \Exception|\Throwable |
||
519 | */ |
||
520 | public function float() |
||
528 | |||
529 | 4 | /** |
|
530 | 1 | * Check if value is integer (is_int) |
|
531 | * |
||
532 | * @return $this |
||
533 | 3 | * @throws InvalidIntException |
|
534 | 1 | * @throws \Exception|\Throwable |
|
535 | 2 | */ |
|
536 | 1 | public function int() |
|
544 | |||
545 | /** |
||
546 | * Soft check that value <= $max |
||
547 | * |
||
548 | * @param float|int $number |
||
549 | * @return $this |
||
550 | 4 | * @throws InvalidIntOrFloatException |
|
551 | * @throws NumberNotLessException |
||
552 | 4 | * @throws \Exception|\Throwable |
|
553 | 1 | */ |
|
554 | public function less($number) |
||
568 | |||
569 | /** |
||
570 | * Soft check that value >= $min |
||
571 | * |
||
572 | * @param float|int $number |
||
573 | 4 | * @return $this |
|
574 | * @throws NumberNotGreaterException |
||
575 | 4 | * @throws InvalidIntOrFloatException |
|
576 | 1 | * @throws \Exception|\Throwable |
|
577 | */ |
||
578 | public function greater($number) |
||
592 | |||
593 | /** |
||
594 | * Strict check that value < $max |
||
595 | * |
||
596 | 4 | * @param float|int $number |
|
597 | * @return $this |
||
598 | 4 | * @throws InvalidIntOrFloatException |
|
599 | 1 | * @throws NumberNotLessStrictlyException |
|
600 | * @throws \Exception|\Throwable |
||
601 | */ |
||
602 | 3 | public function lessStrict($number) |
|
616 | |||
617 | /** |
||
618 | * Strict check that value > $min |
||
619 | * |
||
620 | * @param float|int $number |
||
621 | 6 | * @return $this |
|
622 | * @throws InvalidIntOrFloatException |
||
623 | 6 | * @throws NumberNotGreaterStrictlyException |
|
624 | 1 | * @throws \Exception|\Throwable |
|
625 | 5 | */ |
|
626 | 1 | public function greaterStrict($number) |
|
640 | 2 | ||
641 | 1 | /** |
|
642 | * Check if value match regexp pattern |
||
643 | * |
||
644 | 1 | * @param string $pattern |
|
645 | * @return $this |
||
646 | * @throws InvalidNotEmptyException |
||
647 | * @throws InvalidRegexpPatternException |
||
648 | * @throws InvalidStringException |
||
649 | * @throws StringNotMatchRegExpException |
||
650 | * @throws \Exception|\Throwable |
||
651 | */ |
||
652 | public function match($pattern) |
||
677 | |||
678 | /** |
||
679 | * Check if value match glob pattern |
||
680 | 3 | * |
|
681 | * @param string $pattern |
||
682 | 3 | * @return $this |
|
683 | 1 | * @throws InvalidNotEmptyException |
|
684 | 2 | * @throws InvalidStringException |
|
685 | 1 | * @throws StringNotMatchGlobException |
|
686 | * @throws \Exception|\Throwable |
||
687 | */ |
||
688 | 1 | public function glob($pattern) |
|
704 | |||
705 | /** |
||
706 | 1 | * Check if value < 0 |
|
707 | * |
||
708 | * @return $this |
||
709 | * @throws InvalidIntOrFloatException |
||
710 | * @throws NumberNotNegativeException |
||
711 | * @throws \Exception|\Throwable |
||
712 | */ |
||
713 | public function negative() |
||
723 | 2 | ||
724 | 1 | /** |
|
725 | * Check if value > 0 |
||
726 | * |
||
727 | 1 | * @return $this |
|
728 | * @throws InvalidIntOrFloatException |
||
729 | * @throws NumberNotPositiveException |
||
730 | * @throws \Exception|\Throwable |
||
731 | */ |
||
732 | public function positive() |
||
742 | |||
743 | /** |
||
744 | 2 | * Check if value is same as $anotherValue |
|
745 | 1 | * |
|
746 | * @param int|float|bool|string|resource|array|null $anotherValue |
||
747 | * @return $this |
||
748 | 1 | * @throws InvalidNotObjectException |
|
749 | * @throws InvalidSameValueException |
||
750 | * @throws \Exception|\Throwable |
||
751 | */ |
||
752 | public function isSame($anotherValue) |
||
764 | |||
765 | /** |
||
766 | * Check if value is not same as $anotherValue |
||
767 | * |
||
768 | * @param int|float|bool|string|resource|array|null $anotherValue |
||
769 | * @return $this |
||
770 | * @throws InvalidNotObjectException |
||
771 | * @throws InvalidNotSameValueException |
||
772 | 2 | * @throws \Exception|\Throwable |
|
773 | */ |
||
774 | 2 | public function notSame($anotherValue) |
|
786 | |||
787 | /** |
||
788 | 3 | * Check if value is null |
|
789 | * |
||
790 | 3 | * @return $this |
|
791 | 1 | * @throws InvalidNullException |
|
792 | 2 | * @throws \Exception|\Throwable |
|
793 | 1 | */ |
|
794 | public function isNull() |
||
802 | |||
803 | /** |
||
804 | * Check if value is not null |
||
805 | 2 | * |
|
806 | * @return $this |
||
807 | 2 | * @throws InvalidNotNullException |
|
808 | 1 | * @throws \Exception|\Throwable |
|
809 | */ |
||
810 | public function notNull() |
||
818 | |||
819 | /** |
||
820 | 3 | * Check if value is numeric (is_numeric) |
|
821 | * |
||
822 | 3 | * @return $this |
|
823 | 1 | * @throws InvalidIntOrFloatOrStringException |
|
824 | * @throws InvalidNumericException |
||
825 | * @throws \Exception|\Throwable |
||
826 | 2 | */ |
|
827 | public function numeric() |
||
837 | |||
838 | 1 | /** |
|
839 | * Check if value is resource (is_resource) |
||
840 | * |
||
841 | * @return $this |
||
842 | * @throws InvalidResourceException |
||
843 | * @throws \Exception|\Throwable |
||
844 | */ |
||
845 | public function resource() |
||
853 | 1 | ||
854 | /** |
||
855 | * Check if value is string (is_string) |
||
856 | 1 | * |
|
857 | * @return $this |
||
858 | 1 | * @throws InvalidStringException |
|
859 | * @throws \Exception|\Throwable |
||
860 | */ |
||
861 | public function string() |
||
869 | |||
870 | 3 | /** |
|
871 | 1 | * Cast value to bool |
|
872 | 2 | * |
|
873 | 1 | * @return $this |
|
874 | */ |
||
875 | public function toBool() |
||
881 | |||
882 | /** |
||
883 | * Cast value to float. If it's not numeric - there will be fail cast |
||
884 | * |
||
885 | * @return $this |
||
886 | * @throws InvalidNotArrayException |
||
887 | 2 | * @throws InvalidNumericException |
|
888 | */ |
||
889 | 2 | public function toFloat() |
|
901 | |||
902 | /** |
||
903 | * Cast value to int. If it's not numeric - there will be fail cast |
||
904 | * |
||
905 | * @return $this |
||
906 | * @throws InvalidNotArrayException |
||
907 | * @throws InvalidNumericException |
||
908 | */ |
||
909 | public function toInt() |
||
921 | |||
922 | /** |
||
923 | * Cast value to string. If it's array - there will be fail cast |
||
924 | * |
||
925 | * @return $this |
||
926 | * @throws InvalidNotArrayException |
||
927 | */ |
||
928 | public function toString() |
||
938 | |||
939 | /** |
||
940 | * Process fail validation |
||
941 | * |
||
942 | * @param \InvalidArgumentException $originalException |
||
943 | * |
||
944 | * @return \InvalidArgumentException|\Exception|\Throwable |
||
945 | */ |
||
946 | protected function buildException(\InvalidArgumentException $originalException) |
||
957 | |||
958 | /** |
||
959 | * Return class of exception, which will be thrown on fail test |
||
960 | * |
||
961 | * @return string |
||
962 | * @deprecated Method will be removed |
||
963 | */ |
||
964 | public function getExceptionClass() |
||
968 | |||
969 | /** |
||
970 | * Update default exception class |
||
971 | * |
||
972 | * @param string $exceptionClass |
||
973 | * |
||
974 | * @return $this |
||
975 | * @throws InvalidStringException|ArgumentException |
||
976 | * @deprecated Method will be removed |
||
977 | */ |
||
978 | public function setExceptionClass($exceptionClass) |
||
989 | |||
990 | /** |
||
991 | * Soft check that value >= $min |
||
992 | * |
||
993 | * @param float|int $number |
||
994 | * @return $this |
||
995 | * @throws NumberNotGreaterException |
||
996 | * @throws InvalidIntOrFloatException |
||
997 | * @throws \Exception|\Throwable |
||
998 | * @deprecated Method will be removed |
||
999 | */ |
||
1000 | public function more($number) |
||
1004 | |||
1005 | /** |
||
1006 | * Strict check that value > $min |
||
1007 | * |
||
1008 | * @param float|int $number |
||
1009 | * @return $this |
||
1010 | * @throws InvalidIntOrFloatException |
||
1011 | * @throws NumberNotGreaterStrictlyException |
||
1012 | * @throws \Exception|\Throwable |
||
1013 | * @deprecated Method will be removed |
||
1014 | */ |
||
1015 | public function moreStrict($number) |
||
1019 | |||
1020 | /** |
||
1021 | * Soft check if value has length less than $length. Runs only after notEmpty and string validations |
||
1022 | * |
||
1023 | * @param int $length |
||
1024 | * @return $this |
||
1025 | * @throws InvalidIntException |
||
1026 | * @throws LengthNotGreaterException |
||
1027 | * @throws NumberNotPositiveException |
||
1028 | * @throws InvalidStringException |
||
1029 | * @throws \Exception|\Throwable |
||
1030 | * @deprecated Method will be removed |
||
1031 | */ |
||
1032 | public function lengthMore($length) |
||
1036 | } |
||
1037 |