Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 175 | class Assert |
||
|
|
|||
| 176 | { |
||
| 177 | 94 | public static function string($value, $message = '') |
|
| 186 | |||
| 187 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 192 | |||
| 193 | 17 | public static function integer($value, $message = '') |
|
| 202 | |||
| 203 | 16 | public static function integerish($value, $message = '') |
|
| 212 | |||
| 213 | 16 | public static function float($value, $message = '') |
|
| 222 | |||
| 223 | 20 | public static function numeric($value, $message = '') |
|
| 232 | |||
| 233 | 24 | public static function natural($value, $message = '') |
|
| 242 | |||
| 243 | 16 | public static function boolean($value, $message = '') |
|
| 252 | |||
| 253 | 23 | public static function scalar($value, $message = '') |
|
| 262 | |||
| 263 | 23 | public static function object($value, $message = '') |
|
| 272 | |||
| 273 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 290 | |||
| 291 | 20 | public static function isCallable($value, $message = '') |
|
| 300 | |||
| 301 | 20 | public static function isArray($value, $message = '') |
|
| 310 | |||
| 311 | 20 | public static function isTraversable($value, $message = '') |
|
| 328 | |||
| 329 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 338 | |||
| 339 | 24 | View Code Duplication | public static function isCountable($value, $message = '') |
| 348 | |||
| 349 | 688 | View Code Duplication | public static function isIterable($value, $message = '') |
| 358 | |||
| 359 | 16 | public static function isInstanceOf($value, $class, $message = '') |
|
| 369 | |||
| 370 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 380 | |||
| 381 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 395 | |||
| 396 | 23 | public static function isEmpty($value, $message = '') |
|
| 405 | |||
| 406 | 23 | public static function notEmpty($value, $message = '') |
|
| 415 | |||
| 416 | 11 | public static function null($value, $message = '') |
|
| 425 | |||
| 426 | 11 | public static function notNull($value, $message = '') |
|
| 434 | |||
| 435 | 15 | public static function true($value, $message = '') |
|
| 444 | |||
| 445 | 19 | public static function false($value, $message = '') |
|
| 454 | |||
| 455 | 47 | View Code Duplication | public static function ip($value, $message = '') |
| 456 | { |
||
| 457 | 47 | if (false === filter_var($value, FILTER_VALIDATE_IP)) { |
|
| 458 | 19 | static::reportInvalidArgument(sprintf( |
|
| 459 | 19 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
| 460 | 19 | static::valueToString($value) |
|
| 461 | )); |
||
| 462 | } |
||
| 463 | 28 | } |
|
| 464 | |||
| 465 | 47 | View Code Duplication | public static function ipv4($value, $message = '') |
| 466 | { |
||
| 467 | 47 | if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
| 468 | 35 | static::reportInvalidArgument(sprintf( |
|
| 469 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
| 470 | 35 | static::valueToString($value) |
|
| 471 | )); |
||
| 472 | } |
||
| 473 | 12 | } |
|
| 474 | |||
| 475 | 47 | View Code Duplication | public static function ipv6($value, $message = '') |
| 476 | { |
||
| 477 | 47 | if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
| 478 | 31 | static::reportInvalidArgument(sprintf( |
|
| 479 | 31 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
| 480 | 31 | static::valueToString($value) |
|
| 481 | )); |
||
| 482 | } |
||
| 483 | 16 | } |
|
| 484 | |||
| 485 | 33 | public static function eq($value, $value2, $message = '') |
|
| 486 | { |
||
| 487 | 33 | if ($value2 != $value) { |
|
| 488 | 17 | static::reportInvalidArgument(sprintf( |
|
| 489 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 490 | 17 | static::valueToString($value), |
|
| 491 | 17 | static::valueToString($value2) |
|
| 492 | )); |
||
| 493 | } |
||
| 494 | 16 | } |
|
| 495 | |||
| 496 | 28 | public static function notEq($value, $value2, $message = '') |
|
| 497 | { |
||
| 498 | 28 | if ($value2 == $value) { |
|
| 499 | 16 | static::reportInvalidArgument(sprintf( |
|
| 500 | 16 | $message ?: 'Expected a different value than %s.', |
|
| 501 | 16 | static::valueToString($value2) |
|
| 502 | )); |
||
| 503 | } |
||
| 504 | 12 | } |
|
| 505 | |||
| 506 | 16 | public static function same($value, $value2, $message = '') |
|
| 507 | { |
||
| 508 | 16 | if ($value2 !== $value) { |
|
| 509 | 12 | static::reportInvalidArgument(sprintf( |
|
| 510 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 511 | 12 | static::valueToString($value), |
|
| 512 | 12 | static::valueToString($value2) |
|
| 513 | )); |
||
| 514 | } |
||
| 515 | 4 | } |
|
| 516 | |||
| 517 | 16 | public static function notSame($value, $value2, $message = '') |
|
| 518 | { |
||
| 519 | 16 | if ($value2 === $value) { |
|
| 520 | 4 | static::reportInvalidArgument(sprintf( |
|
| 521 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
| 522 | 4 | static::valueToString($value2) |
|
| 523 | )); |
||
| 524 | } |
||
| 525 | 12 | } |
|
| 526 | |||
| 527 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 528 | { |
||
| 529 | 8 | if ($value <= $limit) { |
|
| 530 | 4 | static::reportInvalidArgument(sprintf( |
|
| 531 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 532 | 4 | static::valueToString($value), |
|
| 533 | 4 | static::valueToString($limit) |
|
| 534 | )); |
||
| 535 | } |
||
| 536 | 4 | } |
|
| 537 | |||
| 538 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 548 | |||
| 549 | 8 | public static function lessThan($value, $limit, $message = '') |
|
| 559 | |||
| 560 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 570 | |||
| 571 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 572 | { |
||
| 573 | 16 | if ($value < $min || $value > $max) { |
|
| 574 | 8 | static::reportInvalidArgument(sprintf( |
|
| 575 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 576 | 8 | static::valueToString($value), |
|
| 577 | 8 | static::valueToString($min), |
|
| 578 | 8 | static::valueToString($max) |
|
| 579 | )); |
||
| 580 | } |
||
| 581 | 8 | } |
|
| 582 | |||
| 583 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 584 | { |
||
| 585 | 8 | if (!in_array($value, $values, true)) { |
|
| 586 | 4 | static::reportInvalidArgument(sprintf( |
|
| 587 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
| 588 | 4 | static::valueToString($value), |
|
| 589 | 4 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
|
| 590 | )); |
||
| 591 | } |
||
| 592 | 4 | } |
|
| 593 | |||
| 594 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 595 | { |
||
| 596 | 20 | if (false === strpos($value, $subString)) { |
|
| 597 | 8 | static::reportInvalidArgument(sprintf( |
|
| 598 | 8 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
| 599 | 8 | static::valueToString($value), |
|
| 600 | 8 | static::valueToString($subString) |
|
| 601 | )); |
||
| 602 | } |
||
| 603 | 12 | } |
|
| 604 | |||
| 605 | 20 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 606 | { |
||
| 607 | 20 | if (false !== strpos($value, $subString)) { |
|
| 608 | 12 | static::reportInvalidArgument(sprintf( |
|
| 609 | 12 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
|
| 610 | 12 | static::valueToString($value), |
|
| 611 | 12 | static::valueToString($subString) |
|
| 612 | )); |
||
| 613 | } |
||
| 614 | 8 | } |
|
| 615 | |||
| 616 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 617 | { |
||
| 618 | 40 | if (preg_match('/^\s*$/', $value)) { |
|
| 619 | 24 | static::reportInvalidArgument(sprintf( |
|
| 620 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
| 621 | 24 | static::valueToString($value) |
|
| 622 | )); |
||
| 623 | } |
||
| 624 | 16 | } |
|
| 625 | |||
| 626 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 627 | { |
||
| 628 | 12 | if (0 !== strpos($value, $prefix)) { |
|
| 629 | 8 | static::reportInvalidArgument(sprintf( |
|
| 630 | 8 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
| 631 | 8 | static::valueToString($value), |
|
| 632 | 8 | static::valueToString($prefix) |
|
| 633 | )); |
||
| 634 | } |
||
| 635 | 4 | } |
|
| 636 | |||
| 637 | 12 | public static function startsWithLetter($value, $message = '') |
|
| 638 | { |
||
| 639 | 12 | $valid = isset($value[0]); |
|
| 640 | |||
| 641 | 12 | if ($valid) { |
|
| 642 | 8 | $locale = setlocale(LC_CTYPE, 0); |
|
| 643 | 8 | setlocale(LC_CTYPE, 'C'); |
|
| 644 | 8 | $valid = ctype_alpha($value[0]); |
|
| 645 | 8 | setlocale(LC_CTYPE, $locale); |
|
| 646 | } |
||
| 647 | |||
| 648 | 12 | if (!$valid) { |
|
| 649 | 8 | static::reportInvalidArgument(sprintf( |
|
| 650 | 8 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
| 651 | 8 | static::valueToString($value) |
|
| 652 | )); |
||
| 653 | } |
||
| 654 | 4 | } |
|
| 655 | |||
| 656 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 666 | |||
| 667 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 676 | |||
| 677 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
| 678 | { |
||
| 679 | 12 | if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 680 | 4 | static::reportInvalidArgument(sprintf( |
|
| 681 | 4 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
|
| 682 | 4 | static::valueToString($value), |
|
| 683 | 4 | static::valueToString($pattern), |
|
| 684 | 4 | $matches[0][1] |
|
| 685 | )); |
||
| 686 | } |
||
| 687 | 8 | } |
|
| 688 | |||
| 689 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
| 690 | { |
||
| 691 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
| 692 | 12 | setlocale(LC_CTYPE, 'C'); |
|
| 693 | 12 | $valid = !ctype_alpha($value); |
|
| 694 | 12 | setlocale(LC_CTYPE, $locale); |
|
| 695 | |||
| 696 | 12 | if ($valid) { |
|
| 697 | 8 | static::reportInvalidArgument(sprintf( |
|
| 698 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
| 699 | 8 | static::valueToString($value) |
|
| 700 | )); |
||
| 701 | } |
||
| 702 | 4 | } |
|
| 703 | |||
| 704 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 705 | { |
||
| 706 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
| 707 | 12 | setlocale(LC_CTYPE, 'C'); |
|
| 708 | 12 | $valid = !ctype_digit($value); |
|
| 709 | 12 | setlocale(LC_CTYPE, $locale); |
|
| 710 | |||
| 711 | 12 | if ($valid) { |
|
| 712 | 8 | static::reportInvalidArgument(sprintf( |
|
| 713 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
| 714 | 8 | static::valueToString($value) |
|
| 715 | )); |
||
| 716 | } |
||
| 717 | 4 | } |
|
| 718 | |||
| 719 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 720 | { |
||
| 721 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
| 722 | 12 | setlocale(LC_CTYPE, 'C'); |
|
| 723 | 12 | $valid = !ctype_alnum($value); |
|
| 724 | 12 | setlocale(LC_CTYPE, $locale); |
|
| 725 | |||
| 726 | 12 | if ($valid) { |
|
| 727 | 8 | static::reportInvalidArgument(sprintf( |
|
| 728 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
| 729 | 8 | static::valueToString($value) |
|
| 730 | )); |
||
| 731 | } |
||
| 732 | 4 | } |
|
| 733 | |||
| 734 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 735 | { |
||
| 736 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
| 737 | 16 | setlocale(LC_CTYPE, 'C'); |
|
| 738 | 16 | $valid = !ctype_lower($value); |
|
| 739 | 16 | setlocale(LC_CTYPE, $locale); |
|
| 740 | |||
| 741 | 16 | if ($valid) { |
|
| 742 | 12 | static::reportInvalidArgument(sprintf( |
|
| 743 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
| 744 | 12 | static::valueToString($value) |
|
| 745 | )); |
||
| 746 | } |
||
| 747 | 4 | } |
|
| 748 | |||
| 749 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 750 | { |
||
| 751 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
| 752 | 16 | setlocale(LC_CTYPE, 'C'); |
|
| 753 | 16 | $valid = !ctype_upper($value); |
|
| 754 | 16 | setlocale(LC_CTYPE, $locale); |
|
| 755 | |||
| 756 | 16 | if ($valid) { |
|
| 757 | 12 | static::reportInvalidArgument(sprintf( |
|
| 758 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
| 759 | 12 | static::valueToString($value) |
|
| 760 | )); |
||
| 761 | } |
||
| 762 | 4 | } |
|
| 763 | |||
| 764 | 24 | public static function length($value, $length, $message = '') |
|
| 774 | |||
| 775 | 24 | public static function minLength($value, $min, $message = '') |
|
| 776 | { |
||
| 777 | 24 | if (static::strlen($value) < $min) { |
|
| 778 | 8 | static::reportInvalidArgument(sprintf( |
|
| 779 | 8 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
| 780 | 8 | static::valueToString($value), |
|
| 781 | 8 | $min |
|
| 782 | )); |
||
| 783 | } |
||
| 784 | 16 | } |
|
| 785 | |||
| 786 | 24 | public static function maxLength($value, $max, $message = '') |
|
| 787 | { |
||
| 788 | 24 | if (static::strlen($value) > $max) { |
|
| 789 | 8 | static::reportInvalidArgument(sprintf( |
|
| 790 | 8 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
| 791 | 8 | static::valueToString($value), |
|
| 792 | 8 | $max |
|
| 793 | )); |
||
| 794 | } |
||
| 795 | 16 | } |
|
| 796 | |||
| 797 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 798 | { |
||
| 799 | 40 | $length = static::strlen($value); |
|
| 800 | |||
| 801 | 40 | if ($length < $min || $length > $max) { |
|
| 802 | 16 | static::reportInvalidArgument(sprintf( |
|
| 803 | 16 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
| 804 | 16 | static::valueToString($value), |
|
| 805 | 16 | $min, |
|
| 806 | 16 | $max |
|
| 807 | )); |
||
| 808 | } |
||
| 809 | 24 | } |
|
| 810 | |||
| 811 | 36 | public static function fileExists($value, $message = '') |
|
| 812 | { |
||
| 813 | 36 | static::string($value); |
|
| 814 | |||
| 815 | 36 | if (!file_exists($value)) { |
|
| 816 | 12 | static::reportInvalidArgument(sprintf( |
|
| 817 | 12 | $message ?: 'The file %s does not exist.', |
|
| 818 | 12 | static::valueToString($value) |
|
| 819 | )); |
||
| 820 | } |
||
| 821 | 24 | } |
|
| 822 | |||
| 823 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 824 | { |
||
| 825 | 12 | static::fileExists($value, $message); |
|
| 826 | |||
| 827 | 8 | if (!is_file($value)) { |
|
| 828 | 4 | static::reportInvalidArgument(sprintf( |
|
| 829 | 4 | $message ?: 'The path %s is not a file.', |
|
| 830 | 4 | static::valueToString($value) |
|
| 831 | )); |
||
| 832 | } |
||
| 833 | 4 | } |
|
| 834 | |||
| 835 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 846 | |||
| 847 | public static function readable($value, $message = '') |
||
| 848 | { |
||
| 849 | if (!is_readable($value)) { |
||
| 850 | static::reportInvalidArgument(sprintf( |
||
| 851 | $message ?: 'The path %s is not readable.', |
||
| 852 | static::valueToString($value) |
||
| 853 | )); |
||
| 854 | } |
||
| 855 | } |
||
| 856 | |||
| 857 | public static function writable($value, $message = '') |
||
| 858 | { |
||
| 859 | if (!is_writable($value)) { |
||
| 860 | static::reportInvalidArgument(sprintf( |
||
| 861 | $message ?: 'The path %s is not writable.', |
||
| 862 | static::valueToString($value) |
||
| 863 | )); |
||
| 864 | } |
||
| 865 | } |
||
| 866 | |||
| 867 | 8 | public static function classExists($value, $message = '') |
|
| 868 | { |
||
| 869 | 8 | if (!class_exists($value)) { |
|
| 870 | 4 | static::reportInvalidArgument(sprintf( |
|
| 871 | 4 | $message ?: 'Expected an existing class name. Got: %s', |
|
| 872 | 4 | static::valueToString($value) |
|
| 873 | )); |
||
| 874 | } |
||
| 875 | 4 | } |
|
| 876 | |||
| 877 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 878 | { |
||
| 879 | 8 | if (!is_subclass_of($value, $class)) { |
|
| 880 | 4 | static::reportInvalidArgument(sprintf( |
|
| 881 | 4 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
| 882 | 4 | static::valueToString($value), |
|
| 883 | 4 | static::valueToString($class) |
|
| 884 | )); |
||
| 885 | } |
||
| 886 | 4 | } |
|
| 887 | |||
| 888 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 889 | { |
||
| 890 | 8 | if (!in_array($interface, class_implements($value))) { |
|
| 891 | 4 | static::reportInvalidArgument(sprintf( |
|
| 892 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
| 893 | 4 | static::valueToString($value), |
|
| 894 | 4 | static::valueToString($interface) |
|
| 895 | )); |
||
| 896 | } |
||
| 897 | 4 | } |
|
| 898 | |||
| 899 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 900 | { |
||
| 901 | 12 | if (!property_exists($classOrObject, $property)) { |
|
| 902 | 4 | static::reportInvalidArgument(sprintf( |
|
| 903 | 4 | $message ?: 'Expected the property %s to exist.', |
|
| 904 | 4 | static::valueToString($property) |
|
| 905 | )); |
||
| 906 | } |
||
| 907 | 8 | } |
|
| 908 | |||
| 909 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 910 | { |
||
| 911 | 12 | if (property_exists($classOrObject, $property)) { |
|
| 912 | 8 | static::reportInvalidArgument(sprintf( |
|
| 913 | 8 | $message ?: 'Expected the property %s to not exist.', |
|
| 914 | 8 | static::valueToString($property) |
|
| 915 | )); |
||
| 916 | } |
||
| 917 | 4 | } |
|
| 918 | |||
| 919 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 920 | { |
||
| 921 | 27 | if (!method_exists($classOrObject, $method)) { |
|
| 922 | 19 | static::reportInvalidArgument(sprintf( |
|
| 923 | 19 | $message ?: 'Expected the method %s to exist.', |
|
| 924 | 19 | static::valueToString($method) |
|
| 925 | )); |
||
| 926 | } |
||
| 927 | 8 | } |
|
| 928 | |||
| 929 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 938 | |||
| 939 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 940 | { |
||
| 941 | 12 | if (!(isset($array[$key]) || array_key_exists($key, $array))) { |
|
| 942 | 4 | static::reportInvalidArgument(sprintf( |
|
| 943 | 4 | $message ?: 'Expected the key %s to exist.', |
|
| 944 | 4 | static::valueToString($key) |
|
| 945 | )); |
||
| 946 | } |
||
| 947 | 8 | } |
|
| 948 | |||
| 949 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 950 | { |
||
| 951 | 12 | if (isset($array[$key]) || array_key_exists($key, $array)) { |
|
| 952 | 8 | static::reportInvalidArgument(sprintf( |
|
| 953 | 8 | $message ?: 'Expected the key %s to not exist.', |
|
| 954 | 8 | static::valueToString($key) |
|
| 955 | )); |
||
| 956 | } |
||
| 957 | 4 | } |
|
| 958 | |||
| 959 | 8 | public static function count($array, $number, $message = '') |
|
| 967 | |||
| 968 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 969 | { |
||
| 970 | 12 | if (count($array) < $min) { |
|
| 971 | 4 | static::reportInvalidArgument(sprintf( |
|
| 972 | 4 | $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
|
| 973 | 4 | count($array), |
|
| 974 | 4 | $min |
|
| 975 | )); |
||
| 976 | } |
||
| 977 | 8 | } |
|
| 978 | |||
| 979 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 980 | { |
||
| 981 | 12 | if (count($array) > $max) { |
|
| 982 | 4 | static::reportInvalidArgument(sprintf( |
|
| 983 | 4 | $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
|
| 984 | 4 | count($array), |
|
| 985 | 4 | $max |
|
| 986 | )); |
||
| 987 | } |
||
| 988 | 8 | } |
|
| 989 | |||
| 990 | 12 | public static function countBetween($array, $min, $max, $message = '') |
|
| 991 | { |
||
| 992 | 12 | $count = count($array); |
|
| 993 | |||
| 994 | 12 | if ($count < $min || $count > $max) { |
|
| 995 | 8 | static::reportInvalidArgument(sprintf( |
|
| 996 | 8 | $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', |
|
| 997 | 8 | $count, |
|
| 998 | 8 | $min, |
|
| 999 | 8 | $max |
|
| 1000 | )); |
||
| 1001 | } |
||
| 1002 | 4 | } |
|
| 1003 | |||
| 1004 | 48 | public static function uuid($value, $message = '') |
|
| 1005 | { |
||
| 1006 | 48 | $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
|
| 1021 | |||
| 1022 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 1048 | |||
| 1049 | 1091 | public static function __callStatic($name, $arguments) |
|
| 1077 | |||
| 1078 | 526 | protected static function valueToString($value) |
|
| 1114 | |||
| 1115 | 169 | protected static function typeToString($value) |
|
| 1119 | |||
| 1120 | 124 | protected static function strlen($value) |
|
| 1132 | |||
| 1133 | 710 | protected static function reportInvalidArgument($message) |
|
| 1137 | |||
| 1138 | private function __construct() |
||
| 1141 | } |
||
| 1142 |