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 |
||
| 48 | class Assert |
||
| 49 | { |
||
| 50 | /** @var Assert */ |
||
| 51 | protected static $validator; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | protected $name; |
||
| 55 | |||
| 56 | /** @var int|float|bool|string|resource|array|null */ |
||
| 57 | protected $value; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Creates validator instance for variable, first fail check will throw an exception |
||
| 61 | * |
||
| 62 | * @param int|float|string|resource|array|null $value |
||
| 63 | * @param string $name |
||
| 64 | * @return static |
||
| 65 | * @throws InvalidNotObjectException |
||
| 66 | * @throws InvalidStringException |
||
| 67 | */ |
||
| 68 | 133 | public static function assert($value, $name = 'value') |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Return current validation value |
||
| 90 | * |
||
| 91 | * @return int|float|bool|string|resource|array |
||
| 92 | */ |
||
| 93 | 5 | public function get() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param \Closure $callback (Assert $value) |
||
| 100 | * @return $this |
||
| 101 | * @throws InvalidArrayException |
||
| 102 | * @throws InvalidIntException |
||
| 103 | */ |
||
| 104 | 4 | public function forList(\Closure $callback) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param \Closure $callback (Assert $key, Assert $value) |
||
| 128 | * @return $this |
||
| 129 | * @throws InvalidArrayException |
||
| 130 | */ |
||
| 131 | 5 | public function forMap(\Closure $callback) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param int $length |
||
| 159 | * @return $this |
||
| 160 | * @throws InvalidIntException |
||
| 161 | * @throws InvalidStringLengthException |
||
| 162 | * @throws NumberNotPositiveException |
||
| 163 | * @throws InvalidStringException |
||
| 164 | */ |
||
| 165 | 5 | public function length($length) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Soft check if value has length $from <= $length <= to. Runs only after string validation |
||
| 184 | * |
||
| 185 | * @param int $from |
||
| 186 | * @param int $to |
||
| 187 | * @return $this |
||
| 188 | * @throws InvalidIntException |
||
| 189 | * @throws LengthNotBetweenException |
||
| 190 | * @throws NumberNotPositiveException |
||
| 191 | * @throws NumberNotGreaterException |
||
| 192 | * @throws NumberNotLessException |
||
| 193 | * @throws InvalidStringException |
||
| 194 | */ |
||
| 195 | 7 | public function lengthBetween($from, $to) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Soft check if value has length less than $length. Runs only after string validation |
||
| 222 | * |
||
| 223 | * @param int $length |
||
| 224 | * @return $this |
||
| 225 | * @throws InvalidIntException |
||
| 226 | * @throws LengthNotLessException |
||
| 227 | * @throws NumberNotPositiveException |
||
| 228 | * @throws InvalidStringException |
||
| 229 | */ |
||
| 230 | 5 | public function lengthLess($length) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Soft check if value has length less than $length. Runs only after notEmpty and string validations |
||
| 249 | * |
||
| 250 | * @param int $length |
||
| 251 | * @return $this |
||
| 252 | * @throws InvalidIntException |
||
| 253 | * @throws LengthNotGreaterException |
||
| 254 | * @throws NumberNotPositiveException |
||
| 255 | * @throws InvalidStringException |
||
| 256 | */ |
||
| 257 | 5 | public function lengthGreater($length) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Check if value is in array (in_array strict) |
||
| 276 | * |
||
| 277 | * @param array $range |
||
| 278 | * @return $this |
||
| 279 | * @throws InvalidArrayException |
||
| 280 | * @throws InvalidNotEmptyException |
||
| 281 | * @throws ValueNotInArrayException |
||
| 282 | */ |
||
| 283 | 4 | public function inArray($range) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Check if value is array |
||
| 300 | * |
||
| 301 | * @return $this |
||
| 302 | * @throws InvalidArrayException |
||
| 303 | */ |
||
| 304 | 2 | public function isArray() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Check if array key exists |
||
| 315 | * |
||
| 316 | * @param string|int $key |
||
| 317 | * @return $this |
||
| 318 | * @throws ArrayKeyNotExistsException |
||
| 319 | * @throws InvalidArrayException |
||
| 320 | * @throws InvalidIntOrStringException |
||
| 321 | */ |
||
| 322 | 4 | public function hasKey($key) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Check if array elements count is same as $count |
||
| 341 | * |
||
| 342 | * @param int $count |
||
| 343 | * @return $this |
||
| 344 | * @throws InvalidArrayCountException |
||
| 345 | * @throws InvalidArrayException |
||
| 346 | * @throws InvalidIntException |
||
| 347 | * @throws NumberNotGreaterException |
||
| 348 | */ |
||
| 349 | 5 | public function count($count) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Soft check that $from <= value <= $to |
||
| 370 | * |
||
| 371 | * @param float|int $from |
||
| 372 | * @param float|int $to |
||
| 373 | * @return $this |
||
| 374 | * @throws NumberNotBetweenException |
||
| 375 | * @throws InvalidIntOrFloatException |
||
| 376 | * @throws NumberNotLessStrictlyException |
||
| 377 | */ |
||
| 378 | 6 | public function between($from, $to) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Strict check that $from < value < $to |
||
| 399 | * |
||
| 400 | * @param float|int $from |
||
| 401 | * @param float|int $to |
||
| 402 | * @return $this |
||
| 403 | * @throws InvalidIntOrFloatException |
||
| 404 | * @throws NumberNotBetweenStrictlyException |
||
| 405 | * @throws NumberNotLessStrictlyException |
||
| 406 | */ |
||
| 407 | 6 | public function betweenStrict($from, $to) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Check if value is boolean (is_bool) |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | * @throws InvalidBoolException |
||
| 431 | */ |
||
| 432 | 2 | public function bool() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Check if value is digit (ctype_digit) |
||
| 443 | * |
||
| 444 | * @return $this |
||
| 445 | * @throws InvalidDigitException |
||
| 446 | * @throws InvalidStringException |
||
| 447 | */ |
||
| 448 | 3 | public function digit() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Check if value is empty (empty) |
||
| 461 | * |
||
| 462 | * @return $this |
||
| 463 | * @throws InvalidEmptyException |
||
| 464 | */ |
||
| 465 | 2 | public function isEmpty() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Check if value is not empty (empty) |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | * @throws InvalidNotEmptyException |
||
| 479 | */ |
||
| 480 | 2 | public function notEmpty() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Check if value is float (is_float) |
||
| 491 | * |
||
| 492 | * @return $this |
||
| 493 | * @throws InvalidFloatException |
||
| 494 | */ |
||
| 495 | 2 | public function float() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Check if value is integer (is_int) |
||
| 506 | * |
||
| 507 | * @return $this |
||
| 508 | * @throws InvalidIntException |
||
| 509 | */ |
||
| 510 | 7 | public function int() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Soft check that value <= $max |
||
| 521 | * |
||
| 522 | * @param float|int $number |
||
| 523 | * @return $this |
||
| 524 | * @throws InvalidIntOrFloatException |
||
| 525 | * @throws NumberNotLessException |
||
| 526 | */ |
||
| 527 | 4 | public function less($number) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Soft check that value >= $min |
||
| 544 | * |
||
| 545 | * @param float|int $number |
||
| 546 | * @return $this |
||
| 547 | * @throws NumberNotGreaterException |
||
| 548 | * @throws InvalidIntOrFloatException |
||
| 549 | */ |
||
| 550 | 4 | public function greater($number) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Strict check that value < $max |
||
| 567 | * |
||
| 568 | * @param float|int $number |
||
| 569 | * @return $this |
||
| 570 | * @throws InvalidIntOrFloatException |
||
| 571 | * @throws NumberNotLessStrictlyException |
||
| 572 | */ |
||
| 573 | 4 | public function lessStrict($number) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Strict check that value > $min |
||
| 590 | * |
||
| 591 | * @param float|int $number |
||
| 592 | * @return $this |
||
| 593 | * @throws InvalidIntOrFloatException |
||
| 594 | * @throws NumberNotGreaterStrictlyException |
||
| 595 | */ |
||
| 596 | 4 | public function greaterStrict($number) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Check if value match regexp pattern |
||
| 613 | * |
||
| 614 | * @param string $pattern |
||
| 615 | * @return $this |
||
| 616 | * @throws InvalidNotEmptyException |
||
| 617 | * @throws InvalidRegexpPatternException |
||
| 618 | * @throws InvalidStringException |
||
| 619 | * @throws StringNotMatchRegExpException |
||
| 620 | */ |
||
| 621 | 6 | public function match($pattern) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Check if value match glob pattern |
||
| 649 | * |
||
| 650 | * @param string $pattern |
||
| 651 | * @return $this |
||
| 652 | * @throws InvalidNotEmptyException |
||
| 653 | * @throws InvalidStringException |
||
| 654 | * @throws StringNotMatchGlobException |
||
| 655 | */ |
||
| 656 | 5 | public function glob($pattern) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Check if value < 0 |
||
| 675 | * |
||
| 676 | * @return $this |
||
| 677 | * @throws InvalidIntOrFloatException |
||
| 678 | * @throws NumberNotNegativeException |
||
| 679 | */ |
||
| 680 | 3 | public function negative() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Check if value > 0 |
||
| 693 | * |
||
| 694 | * @return $this |
||
| 695 | * @throws InvalidIntOrFloatException |
||
| 696 | * @throws NumberNotPositiveException |
||
| 697 | */ |
||
| 698 | 3 | public function positive() |
|
| 708 | |||
| 709 | /** |
||
| 710 | * Check if value is same as $anotherValue |
||
| 711 | * |
||
| 712 | * @param int|float|bool|string|resource|array|null $anotherValue |
||
| 713 | * @return $this |
||
| 714 | * @throws InvalidNotObjectException |
||
| 715 | * @throws InvalidSameValueException |
||
| 716 | */ |
||
| 717 | 3 | public function isSame($anotherValue) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Check if value is not same as $anotherValue |
||
| 732 | * |
||
| 733 | * @param int|float|bool|string|resource|array|null $anotherValue |
||
| 734 | * @return $this |
||
| 735 | * @throws InvalidNotObjectException |
||
| 736 | * @throws InvalidNotSameValueException |
||
| 737 | */ |
||
| 738 | 3 | public function notSame($anotherValue) |
|
| 750 | |||
| 751 | /** |
||
| 752 | * Check if value is null |
||
| 753 | * |
||
| 754 | * @return $this |
||
| 755 | * @throws InvalidNullException |
||
| 756 | */ |
||
| 757 | 2 | public function isNull() |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Check if value is not null |
||
| 768 | * |
||
| 769 | * @return $this |
||
| 770 | * @throws InvalidNotNullException |
||
| 771 | */ |
||
| 772 | 2 | public function notNull() |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Check if value is numeric (is_numeric) |
||
| 783 | * |
||
| 784 | * @return $this |
||
| 785 | * @throws InvalidIntOrFloatOrStringException |
||
| 786 | * @throws InvalidNumericException |
||
| 787 | */ |
||
| 788 | 3 | public function numeric() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Check if value is resource (is_resource) |
||
| 801 | * |
||
| 802 | * @return $this |
||
| 803 | * @throws InvalidResourceException |
||
| 804 | */ |
||
| 805 | 2 | public function resource() |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Check if value is string (is_string) |
||
| 816 | * |
||
| 817 | * @return $this |
||
| 818 | * @throws InvalidStringException |
||
| 819 | */ |
||
| 820 | 3 | public function string() |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Cast value to bool |
||
| 831 | * |
||
| 832 | * @return $this |
||
| 833 | */ |
||
| 834 | 1 | public function toBool() |
|
| 840 | |||
| 841 | /** |
||
| 842 | * Cast value to float. If it's not numeric - there will be fail cast |
||
| 843 | * |
||
| 844 | * @return $this |
||
| 845 | * @throws InvalidNotArrayException |
||
| 846 | * @throws InvalidNumericException |
||
| 847 | */ |
||
| 848 | 3 | public function toFloat() |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Cast value to int. If it's not numeric - there will be fail cast |
||
| 863 | * |
||
| 864 | * @return $this |
||
| 865 | * @throws InvalidNotArrayException |
||
| 866 | * @throws InvalidNumericException |
||
| 867 | */ |
||
| 868 | 3 | public function toInt() |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Cast value to string. If it's array - there will be fail cast |
||
| 883 | * |
||
| 884 | * @return $this |
||
| 885 | * @throws InvalidNotArrayException |
||
| 886 | */ |
||
| 887 | 2 | public function toString() |
|
| 897 | } |
||
| 898 |