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 |
||
191 | class Assert |
||
|
|||
192 | { |
||
193 | 122 | public static function string($value, $message = '') |
|
202 | |||
203 | 16 | public static function stringNotEmpty($value, $message = '') |
|
208 | |||
209 | 17 | public static function integer($value, $message = '') |
|
218 | |||
219 | 16 | public static function integerish($value, $message = '') |
|
228 | |||
229 | 16 | public static function float($value, $message = '') |
|
238 | |||
239 | 20 | public static function numeric($value, $message = '') |
|
248 | |||
249 | 24 | public static function natural($value, $message = '') |
|
250 | { |
||
251 | 24 | if (!is_int($value) || $value < 0) { |
|
252 | 16 | static::reportInvalidArgument(sprintf( |
|
253 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
254 | 16 | static::valueToString($value) |
|
255 | )); |
||
256 | } |
||
257 | 8 | } |
|
258 | |||
259 | 16 | public static function boolean($value, $message = '') |
|
268 | |||
269 | 23 | public static function scalar($value, $message = '') |
|
278 | |||
279 | 23 | public static function object($value, $message = '') |
|
288 | |||
289 | 16 | public static function resource($value, $type = null, $message = '') |
|
306 | |||
307 | 20 | public static function isCallable($value, $message = '') |
|
316 | |||
317 | 20 | public static function isArray($value, $message = '') |
|
326 | |||
327 | /** |
||
328 | * @deprecated |
||
329 | */ |
||
330 | 20 | public static function isTraversable($value, $message = '') |
|
331 | { |
||
332 | 20 | @trigger_error( |
|
333 | 20 | sprintf( |
|
334 | 20 | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', |
|
335 | 20 | __METHOD__ |
|
336 | ), |
||
337 | 20 | E_USER_DEPRECATED |
|
338 | ); |
||
339 | |||
340 | 20 | if (!is_array($value) && !($value instanceof Traversable)) { |
|
341 | 8 | static::reportInvalidArgument(sprintf( |
|
342 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
343 | 8 | static::typeToString($value) |
|
344 | )); |
||
345 | } |
||
346 | 12 | } |
|
347 | |||
348 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
357 | |||
358 | 24 | View Code Duplication | public static function isCountable($value, $message = '') |
367 | |||
368 | 870 | View Code Duplication | public static function isIterable($value, $message = '') |
377 | |||
378 | 16 | public static function isInstanceOf($value, $class, $message = '') |
|
388 | |||
389 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
399 | |||
400 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
414 | |||
415 | 23 | public static function isEmpty($value, $message = '') |
|
424 | |||
425 | 23 | public static function notEmpty($value, $message = '') |
|
434 | |||
435 | 11 | public static function null($value, $message = '') |
|
436 | { |
||
437 | 11 | if (null !== $value) { |
|
438 | 8 | static::reportInvalidArgument(sprintf( |
|
439 | 8 | $message ?: 'Expected null. Got: %s', |
|
440 | 8 | static::valueToString($value) |
|
441 | )); |
||
442 | } |
||
443 | 3 | } |
|
444 | |||
445 | 11 | public static function notNull($value, $message = '') |
|
453 | |||
454 | 15 | public static function true($value, $message = '') |
|
455 | { |
||
456 | 15 | if (true !== $value) { |
|
457 | 11 | static::reportInvalidArgument(sprintf( |
|
458 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
459 | 11 | static::valueToString($value) |
|
460 | )); |
||
461 | } |
||
462 | 4 | } |
|
463 | |||
464 | 19 | public static function false($value, $message = '') |
|
465 | { |
||
466 | 19 | if (false !== $value) { |
|
467 | 15 | static::reportInvalidArgument(sprintf( |
|
468 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
469 | 15 | static::valueToString($value) |
|
470 | )); |
||
471 | } |
||
472 | 4 | } |
|
473 | |||
474 | 47 | View Code Duplication | public static function ip($value, $message = '') |
483 | |||
484 | 47 | View Code Duplication | public static function ipv4($value, $message = '') |
493 | |||
494 | 47 | View Code Duplication | public static function ipv6($value, $message = '') |
503 | |||
504 | 12 | public static function uniqueValues(array $values, $message = '') |
|
519 | |||
520 | 33 | public static function eq($value, $expect, $message = '') |
|
521 | { |
||
522 | 33 | if ($expect != $value) { |
|
523 | 17 | static::reportInvalidArgument(sprintf( |
|
524 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
525 | 17 | static::valueToString($value), |
|
526 | 17 | static::valueToString($expect) |
|
527 | )); |
||
528 | } |
||
529 | 16 | } |
|
530 | |||
531 | 28 | public static function notEq($value, $expect, $message = '') |
|
540 | |||
541 | 16 | public static function same($value, $expect, $message = '') |
|
542 | { |
||
543 | 16 | if ($expect !== $value) { |
|
544 | 12 | static::reportInvalidArgument(sprintf( |
|
545 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
546 | 12 | static::valueToString($value), |
|
547 | 12 | static::valueToString($expect) |
|
548 | )); |
||
549 | } |
||
550 | 4 | } |
|
551 | |||
552 | 16 | public static function notSame($value, $expect, $message = '') |
|
561 | |||
562 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
563 | { |
||
564 | 8 | if ($value <= $limit) { |
|
565 | 4 | static::reportInvalidArgument(sprintf( |
|
566 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
567 | 4 | static::valueToString($value), |
|
568 | 4 | static::valueToString($limit) |
|
569 | )); |
||
570 | } |
||
571 | 4 | } |
|
572 | |||
573 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
574 | { |
||
575 | 12 | if ($value < $limit) { |
|
576 | 4 | static::reportInvalidArgument(sprintf( |
|
577 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
578 | 4 | static::valueToString($value), |
|
579 | 4 | static::valueToString($limit) |
|
580 | )); |
||
581 | } |
||
582 | 8 | } |
|
583 | |||
584 | 8 | public static function lessThan($value, $limit, $message = '') |
|
585 | { |
||
586 | 8 | if ($value >= $limit) { |
|
587 | 4 | static::reportInvalidArgument(sprintf( |
|
588 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
589 | 4 | static::valueToString($value), |
|
590 | 4 | static::valueToString($limit) |
|
591 | )); |
||
592 | } |
||
593 | 4 | } |
|
594 | |||
595 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
596 | { |
||
597 | 12 | if ($value > $limit) { |
|
598 | 4 | static::reportInvalidArgument(sprintf( |
|
599 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
600 | 4 | static::valueToString($value), |
|
601 | 4 | static::valueToString($limit) |
|
602 | )); |
||
603 | } |
||
604 | 8 | } |
|
605 | |||
606 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
617 | |||
618 | 8 | public static function oneOf($value, array $values, $message = '') |
|
628 | |||
629 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
639 | |||
640 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
650 | |||
651 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
652 | { |
||
653 | 40 | if (preg_match('/^\s*$/', $value)) { |
|
654 | 24 | static::reportInvalidArgument(sprintf( |
|
655 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
656 | 24 | static::valueToString($value) |
|
657 | )); |
||
658 | } |
||
659 | 16 | } |
|
660 | |||
661 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
671 | |||
672 | 24 | public static function startsWithLetter($value, $message = '') |
|
690 | |||
691 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
701 | |||
702 | 12 | public static function regex($value, $pattern, $message = '') |
|
703 | { |
||
704 | 12 | if (!preg_match($pattern, $value)) { |
|
705 | 8 | static::reportInvalidArgument(sprintf( |
|
706 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
707 | 8 | static::valueToString($value) |
|
708 | )); |
||
709 | } |
||
710 | 4 | } |
|
711 | |||
712 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
723 | |||
724 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
725 | { |
||
726 | 28 | static::string($value); |
|
727 | |||
728 | 28 | if (!preg_match('/^\p{L}+$/u', $value)) { |
|
729 | 16 | static::reportInvalidArgument(sprintf( |
|
730 | 16 | $message ?: 'Expected a value to contain only Unicode letters. Got: %s', |
|
731 | 16 | static::valueToString($value) |
|
732 | )); |
||
733 | } |
||
734 | 12 | } |
|
735 | |||
736 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
737 | { |
||
738 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
739 | 12 | setlocale(LC_CTYPE, 'C'); |
|
740 | 12 | $valid = !ctype_alpha($value); |
|
741 | 12 | setlocale(LC_CTYPE, $locale); |
|
742 | |||
743 | 12 | if ($valid) { |
|
744 | 8 | static::reportInvalidArgument(sprintf( |
|
745 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
746 | 8 | static::valueToString($value) |
|
747 | )); |
||
748 | } |
||
749 | 4 | } |
|
750 | |||
751 | 12 | View Code Duplication | public static function digits($value, $message = '') |
752 | { |
||
753 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
754 | 12 | setlocale(LC_CTYPE, 'C'); |
|
755 | 12 | $valid = !ctype_digit($value); |
|
756 | 12 | setlocale(LC_CTYPE, $locale); |
|
757 | |||
758 | 12 | if ($valid) { |
|
759 | 8 | static::reportInvalidArgument(sprintf( |
|
760 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
761 | 8 | static::valueToString($value) |
|
762 | )); |
||
763 | } |
||
764 | 4 | } |
|
765 | |||
766 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
767 | { |
||
768 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
769 | 12 | setlocale(LC_CTYPE, 'C'); |
|
770 | 12 | $valid = !ctype_alnum($value); |
|
771 | 12 | setlocale(LC_CTYPE, $locale); |
|
772 | |||
773 | 12 | if ($valid) { |
|
774 | 8 | static::reportInvalidArgument(sprintf( |
|
775 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
776 | 8 | static::valueToString($value) |
|
777 | )); |
||
778 | } |
||
779 | 4 | } |
|
780 | |||
781 | 16 | View Code Duplication | public static function lower($value, $message = '') |
782 | { |
||
783 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
784 | 16 | setlocale(LC_CTYPE, 'C'); |
|
785 | 16 | $valid = !ctype_lower($value); |
|
786 | 16 | setlocale(LC_CTYPE, $locale); |
|
787 | |||
788 | 16 | if ($valid) { |
|
789 | 12 | static::reportInvalidArgument(sprintf( |
|
790 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
791 | 12 | static::valueToString($value) |
|
792 | )); |
||
793 | } |
||
794 | 4 | } |
|
795 | |||
796 | 16 | View Code Duplication | public static function upper($value, $message = '') |
797 | { |
||
798 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
799 | 16 | setlocale(LC_CTYPE, 'C'); |
|
800 | 16 | $valid = !ctype_upper($value); |
|
801 | 16 | setlocale(LC_CTYPE, $locale); |
|
802 | |||
803 | 16 | if ($valid) { |
|
804 | 12 | static::reportInvalidArgument(sprintf( |
|
805 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
806 | 12 | static::valueToString($value) |
|
807 | )); |
||
808 | } |
||
809 | 4 | } |
|
810 | |||
811 | 36 | public static function length($value, $length, $message = '') |
|
812 | { |
||
813 | 36 | if ($length !== static::strlen($value)) { |
|
814 | 24 | static::reportInvalidArgument(sprintf( |
|
815 | 24 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
816 | 24 | static::valueToString($value), |
|
817 | $length |
||
818 | )); |
||
819 | } |
||
820 | 12 | } |
|
821 | |||
822 | 36 | public static function minLength($value, $min, $message = '') |
|
823 | { |
||
824 | 36 | if (static::strlen($value) < $min) { |
|
825 | 12 | static::reportInvalidArgument(sprintf( |
|
826 | 12 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
827 | 12 | static::valueToString($value), |
|
828 | $min |
||
829 | )); |
||
830 | } |
||
831 | 24 | } |
|
832 | |||
833 | 36 | public static function maxLength($value, $max, $message = '') |
|
834 | { |
||
835 | 36 | if (static::strlen($value) > $max) { |
|
836 | 12 | static::reportInvalidArgument(sprintf( |
|
837 | 12 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
838 | 12 | static::valueToString($value), |
|
839 | $max |
||
840 | )); |
||
841 | } |
||
842 | 24 | } |
|
843 | |||
844 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
845 | { |
||
846 | 60 | $length = static::strlen($value); |
|
847 | |||
848 | 60 | if ($length < $min || $length > $max) { |
|
849 | 24 | static::reportInvalidArgument(sprintf( |
|
850 | 24 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
851 | 24 | static::valueToString($value), |
|
852 | $min, |
||
853 | $max |
||
854 | )); |
||
855 | } |
||
856 | 36 | } |
|
857 | |||
858 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
869 | |||
870 | 12 | View Code Duplication | public static function file($value, $message = '') |
871 | { |
||
872 | 12 | static::fileExists($value, $message); |
|
873 | |||
874 | 8 | if (!is_file($value)) { |
|
875 | 4 | static::reportInvalidArgument(sprintf( |
|
876 | 4 | $message ?: 'The path %s is not a file.', |
|
877 | 4 | static::valueToString($value) |
|
878 | )); |
||
879 | } |
||
880 | 4 | } |
|
881 | |||
882 | 12 | View Code Duplication | public static function directory($value, $message = '') |
883 | { |
||
884 | 12 | static::fileExists($value, $message); |
|
885 | |||
886 | 8 | if (!is_dir($value)) { |
|
887 | 4 | static::reportInvalidArgument(sprintf( |
|
888 | 4 | $message ?: 'The path %s is no directory.', |
|
889 | 4 | static::valueToString($value) |
|
890 | )); |
||
891 | } |
||
892 | 4 | } |
|
893 | |||
894 | public static function readable($value, $message = '') |
||
895 | { |
||
896 | if (!is_readable($value)) { |
||
897 | static::reportInvalidArgument(sprintf( |
||
898 | $message ?: 'The path %s is not readable.', |
||
899 | static::valueToString($value) |
||
900 | )); |
||
901 | } |
||
902 | } |
||
903 | |||
904 | public static function writable($value, $message = '') |
||
905 | { |
||
906 | if (!is_writable($value)) { |
||
907 | static::reportInvalidArgument(sprintf( |
||
908 | $message ?: 'The path %s is not writable.', |
||
909 | static::valueToString($value) |
||
910 | )); |
||
911 | } |
||
912 | } |
||
913 | |||
914 | 8 | public static function classExists($value, $message = '') |
|
915 | { |
||
916 | 8 | if (!class_exists($value)) { |
|
917 | 4 | static::reportInvalidArgument(sprintf( |
|
918 | 4 | $message ?: 'Expected an existing class name. Got: %s', |
|
919 | 4 | static::valueToString($value) |
|
920 | )); |
||
921 | } |
||
922 | 4 | } |
|
923 | |||
924 | 8 | public static function subclassOf($value, $class, $message = '') |
|
925 | { |
||
926 | 8 | if (!is_subclass_of($value, $class)) { |
|
927 | 4 | static::reportInvalidArgument(sprintf( |
|
928 | 4 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
929 | 4 | static::valueToString($value), |
|
930 | 4 | static::valueToString($class) |
|
931 | )); |
||
932 | } |
||
933 | 4 | } |
|
934 | |||
935 | 8 | public static function interfaceExists($value, $message = '') |
|
936 | { |
||
937 | 8 | if (!interface_exists($value)) { |
|
938 | 4 | static::reportInvalidArgument(sprintf( |
|
939 | 4 | $message ?: 'Expected an existing interface name. got %s', |
|
940 | 4 | static::valueToString($value) |
|
941 | )); |
||
942 | } |
||
943 | 4 | } |
|
944 | |||
945 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
946 | { |
||
947 | 8 | if (!in_array($interface, class_implements($value))) { |
|
948 | 4 | static::reportInvalidArgument(sprintf( |
|
949 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
950 | 4 | static::valueToString($value), |
|
951 | 4 | static::valueToString($interface) |
|
952 | )); |
||
953 | } |
||
954 | 4 | } |
|
955 | |||
956 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
957 | { |
||
958 | 12 | if (!property_exists($classOrObject, $property)) { |
|
959 | 4 | static::reportInvalidArgument(sprintf( |
|
960 | 4 | $message ?: 'Expected the property %s to exist.', |
|
961 | 4 | static::valueToString($property) |
|
962 | )); |
||
963 | } |
||
964 | 8 | } |
|
965 | |||
966 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
967 | { |
||
968 | 12 | if (property_exists($classOrObject, $property)) { |
|
969 | 8 | static::reportInvalidArgument(sprintf( |
|
970 | 8 | $message ?: 'Expected the property %s to not exist.', |
|
971 | 8 | static::valueToString($property) |
|
972 | )); |
||
973 | } |
||
974 | 4 | } |
|
975 | |||
976 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
977 | { |
||
978 | 27 | if (!method_exists($classOrObject, $method)) { |
|
979 | 19 | static::reportInvalidArgument(sprintf( |
|
980 | 19 | $message ?: 'Expected the method %s to exist.', |
|
981 | 19 | static::valueToString($method) |
|
982 | )); |
||
983 | } |
||
984 | 8 | } |
|
985 | |||
986 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
987 | { |
||
988 | 27 | if (method_exists($classOrObject, $method)) { |
|
989 | 8 | static::reportInvalidArgument(sprintf( |
|
990 | 8 | $message ?: 'Expected the method %s to not exist.', |
|
991 | 8 | static::valueToString($method) |
|
992 | )); |
||
993 | } |
||
994 | 19 | } |
|
995 | |||
996 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
997 | { |
||
998 | 12 | if (!(isset($array[$key]) || array_key_exists($key, $array))) { |
|
999 | 4 | static::reportInvalidArgument(sprintf( |
|
1000 | 4 | $message ?: 'Expected the key %s to exist.', |
|
1001 | 4 | static::valueToString($key) |
|
1002 | )); |
||
1003 | } |
||
1004 | 8 | } |
|
1005 | |||
1006 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1007 | { |
||
1008 | 12 | if (isset($array[$key]) || array_key_exists($key, $array)) { |
|
1009 | 8 | static::reportInvalidArgument(sprintf( |
|
1010 | 8 | $message ?: 'Expected the key %s to not exist.', |
|
1011 | 8 | static::valueToString($key) |
|
1012 | )); |
||
1013 | } |
||
1014 | 4 | } |
|
1015 | |||
1016 | 8 | public static function count($array, $number, $message = '') |
|
1017 | { |
||
1018 | 8 | static::eq( |
|
1019 | 8 | count($array), |
|
1020 | $number, |
||
1021 | 8 | $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
|
1022 | ); |
||
1023 | 4 | } |
|
1024 | |||
1025 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1026 | { |
||
1027 | 12 | if (count($array) < $min) { |
|
1028 | 4 | static::reportInvalidArgument(sprintf( |
|
1029 | 4 | $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
|
1030 | 4 | count($array), |
|
1031 | $min |
||
1032 | )); |
||
1033 | } |
||
1034 | 8 | } |
|
1035 | |||
1036 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1037 | { |
||
1038 | 12 | if (count($array) > $max) { |
|
1039 | 4 | static::reportInvalidArgument(sprintf( |
|
1040 | 4 | $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
|
1041 | 4 | count($array), |
|
1042 | $max |
||
1043 | )); |
||
1044 | } |
||
1045 | 8 | } |
|
1046 | |||
1047 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
1048 | { |
||
1049 | 20 | $count = count($array); |
|
1050 | |||
1051 | 20 | if ($count < $min || $count > $max) { |
|
1052 | 8 | static::reportInvalidArgument(sprintf( |
|
1053 | 8 | $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', |
|
1054 | $count, |
||
1055 | $min, |
||
1056 | $max |
||
1057 | )); |
||
1058 | } |
||
1059 | 12 | } |
|
1060 | |||
1061 | 24 | public static function isList($array, $message = '') |
|
1069 | |||
1070 | 16 | public static function isMap($array, $message = '') |
|
1071 | { |
||
1072 | if ( |
||
1073 | 16 | !is_array($array) || |
|
1074 | 16 | !$array || |
|
1075 | array_keys($array) !== array_filter(array_keys($array), function ($key) { |
||
1076 | 12 | return is_string($key); |
|
1077 | 16 | }) |
|
1078 | ) { |
||
1079 | 12 | static::reportInvalidArgument( |
|
1080 | 12 | $message ?: 'Expected map - associative array with string keys.' |
|
1081 | ); |
||
1082 | } |
||
1083 | 4 | } |
|
1084 | |||
1085 | 56 | public static function uuid($value, $message = '') |
|
1102 | |||
1103 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
1104 | { |
||
1105 | 24 | static::string($class); |
|
1106 | |||
1107 | 24 | $actual = 'none'; |
|
1108 | |||
1109 | try { |
||
1110 | 24 | $expression(); |
|
1111 | 24 | } catch (Exception $e) { |
|
1112 | 20 | $actual = get_class($e); |
|
1113 | 20 | if ($e instanceof $class) { |
|
1114 | 20 | return; |
|
1115 | } |
||
1116 | 4 | } catch (Throwable $e) { |
|
1117 | 4 | $actual = get_class($e); |
|
1118 | 4 | if ($e instanceof $class) { |
|
1119 | 4 | return; |
|
1120 | } |
||
1121 | } |
||
1122 | |||
1123 | 8 | static::reportInvalidArgument($message ?: sprintf( |
|
1124 | 8 | 'Expected to throw "%s", got "%s"', |
|
1125 | $class, |
||
1126 | $actual |
||
1127 | )); |
||
1128 | } |
||
1129 | |||
1130 | 1370 | public static function __callStatic($name, $arguments) |
|
1158 | |||
1159 | 682 | protected static function valueToString($value) |
|
1195 | |||
1196 | 169 | protected static function typeToString($value) |
|
1200 | |||
1201 | 168 | protected static function strlen($value) |
|
1213 | |||
1214 | 906 | protected static function reportInvalidArgument($message) |
|
1218 | |||
1219 | private function __construct() |
||
1222 | } |
||
1223 |