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 |
||
36 | class Assert |
||
|
|||
37 | { |
||
38 | /** |
||
39 | * @psalm-pure |
||
40 | * @psalm-assert string $value |
||
41 | * |
||
42 | * @param mixed $value |
||
43 | * @param string|callable():string $message |
||
44 | * |
||
45 | * @throws InvalidArgumentException |
||
46 | */ |
||
47 | 241 | public static function string($value, $message = '') |
|
60 | |||
61 | /** |
||
62 | * @psalm-pure |
||
63 | * @psalm-assert non-empty-string $value |
||
64 | * |
||
65 | * @param mixed $value |
||
66 | 16 | * @param string|callable():string $message |
|
67 | * |
||
68 | 16 | * @throws InvalidArgumentException |
|
69 | 12 | */ |
|
70 | 8 | public static function stringNotEmpty($value, $message = '') |
|
75 | |||
76 | /** |
||
77 | * @psalm-pure |
||
78 | * @psalm-assert int $value |
||
79 | * |
||
80 | * @param mixed $value |
||
81 | 17 | * @param string|callable():string $message |
|
82 | * |
||
83 | 17 | * @throws InvalidArgumentException |
|
84 | 13 | */ |
|
85 | 13 | public static function integer($value, $message = '') |
|
98 | |||
99 | /** |
||
100 | 16 | * @psalm-pure |
|
101 | * @psalm-assert numeric $value |
||
102 | 16 | * |
|
103 | 4 | * @param mixed $value |
|
104 | 4 | * @param string|callable():string $message |
|
105 | 4 | * |
|
106 | * @throws InvalidArgumentException |
||
107 | */ |
||
108 | 12 | View Code Duplication | public static function integerish($value, $message = '') |
121 | 16 | ||
122 | 8 | /** |
|
123 | 8 | * @psalm-pure |
|
124 | 8 | * @psalm-assert float $value |
|
125 | * |
||
126 | * @param mixed $value |
||
127 | 8 | * @param string|callable():string $message |
|
128 | * |
||
129 | * @throws InvalidArgumentException |
||
130 | */ |
||
131 | public static function float($value, $message = '') |
||
144 | |||
145 | /** |
||
146 | 16 | * @psalm-pure |
|
147 | * @psalm-assert numeric $value |
||
148 | * |
||
149 | * @param mixed $value |
||
150 | * @param string|callable():string $message |
||
151 | * |
||
152 | * @throws InvalidArgumentException |
||
153 | */ |
||
154 | public static function numeric($value, $message = '') |
||
167 | |||
168 | /** |
||
169 | * @psalm-pure |
||
170 | * @psalm-assert int $value |
||
171 | * |
||
172 | * @param mixed $value |
||
173 | * @param string|callable():string $message |
||
174 | * |
||
175 | * @throws InvalidArgumentException |
||
176 | 16 | */ |
|
177 | public static function natural($value, $message = '') |
||
186 | |||
187 | /** |
||
188 | * @psalm-pure |
||
189 | * @psalm-assert bool $value |
||
190 | * |
||
191 | * @param mixed $value |
||
192 | * @param string|callable():string $message |
||
193 | * |
||
194 | * @throws InvalidArgumentException |
||
195 | 23 | */ |
|
196 | public static function boolean($value, $message = '') |
||
209 | |||
210 | /** |
||
211 | * @psalm-pure |
||
212 | * @psalm-assert scalar $value |
||
213 | * |
||
214 | 23 | * @param mixed $value |
|
215 | * @param string|callable():string $message |
||
216 | 23 | * |
|
217 | 15 | * @throws InvalidArgumentException |
|
218 | 15 | */ |
|
219 | 15 | public static function scalar($value, $message = '') |
|
232 | |||
233 | /** |
||
234 | 16 | * @psalm-pure |
|
235 | * @psalm-assert object $value |
||
236 | 16 | * |
|
237 | 4 | * @param mixed $value |
|
238 | 4 | * @param string|callable():string $message |
|
239 | 4 | * |
|
240 | * @throws InvalidArgumentException |
||
241 | */ |
||
242 | public static function object($value, $message = '') |
||
255 | |||
256 | /** |
||
257 | * @psalm-pure |
||
258 | * @psalm-assert resource $value |
||
259 | * |
||
260 | * @param mixed $value |
||
261 | 20 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
|
262 | * @param string $message |
||
263 | 20 | * |
|
264 | 8 | * @throws InvalidArgumentException |
|
265 | 8 | */ |
|
266 | 8 | public static function resource($value, $type = null, $message = '') |
|
287 | |||
288 | 8 | /** |
|
289 | * @psalm-pure |
||
290 | * @psalm-assert callable $value |
||
291 | * |
||
292 | * @param mixed $value |
||
293 | * @param string|callable():string $message |
||
294 | * |
||
295 | * @throws InvalidArgumentException |
||
296 | */ |
||
297 | public static function isCallable($value, $message = '') |
||
310 | |||
311 | 20 | /** |
|
312 | 8 | * @psalm-pure |
|
313 | 8 | * @psalm-assert array $value |
|
314 | 8 | * |
|
315 | * @param mixed $value |
||
316 | * @param string|callable():string $message |
||
317 | 12 | * |
|
318 | * @throws InvalidArgumentException |
||
319 | */ |
||
320 | View Code Duplication | public static function isArray($value, $message = '') |
|
333 | 8 | ||
334 | /** |
||
335 | * @psalm-pure |
||
336 | 12 | * @psalm-assert iterable $value |
|
337 | * |
||
338 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
339 | * |
||
340 | * @param mixed $value |
||
341 | * @param string|callable():string $message |
||
342 | * |
||
343 | * @throws InvalidArgumentException |
||
344 | */ |
||
345 | public static function isTraversable($value, $message = '') |
||
366 | |||
367 | /** |
||
368 | * @psalm-pure |
||
369 | * @psalm-assert array|ArrayAccess $value |
||
370 | * |
||
371 | 1044 | * @param mixed $value |
|
372 | * @param string|callable():string $message |
||
373 | 1044 | * |
|
374 | 8 | * @throws InvalidArgumentException |
|
375 | 8 | */ |
|
376 | 8 | public static function isArrayAccessible($value, $message = '') |
|
389 | |||
390 | /** |
||
391 | * @psalm-pure |
||
392 | * @psalm-assert countable $value |
||
393 | 19 | * |
|
394 | * @param mixed $value |
||
395 | 19 | * @param string|callable():string $message |
|
396 | 15 | * |
|
397 | 15 | * @throws InvalidArgumentException |
|
398 | 15 | */ |
|
399 | 15 | public static function isCountable($value, $message = '') |
|
417 | |||
418 | 16 | /** |
|
419 | 4 | * @psalm-pure |
|
420 | 4 | * @psalm-assert iterable $value |
|
421 | 4 | * |
|
422 | 4 | * @param mixed $value |
|
423 | * @param string|callable():string $message |
||
424 | * |
||
425 | 12 | * @throws InvalidArgumentException |
|
426 | */ |
||
427 | public static function isIterable($value, $message = '') |
||
440 | 20 | ||
441 | 8 | /** |
|
442 | * @psalm-pure |
||
443 | * @psalm-template ExpectedType of object |
||
444 | * @psalm-param class-string<ExpectedType> $class |
||
445 | 12 | * @psalm-assert ExpectedType $value |
|
446 | 12 | * |
|
447 | 12 | * @param mixed $value |
|
448 | 12 | * @param string|object $class |
|
449 | * @param string $message |
||
450 | * |
||
451 | * @throws InvalidArgumentException |
||
452 | */ |
||
453 | View Code Duplication | public static function isInstanceOf($value, $class, $message = '') |
|
463 | |||
464 | 20 | /** |
|
465 | * @psalm-pure |
||
466 | 20 | * @psalm-template ExpectedType of object |
|
467 | * @psalm-param class-string<ExpectedType> $class |
||
468 | 16 | * @psalm-assert !ExpectedType $value |
|
469 | 12 | * |
|
470 | 12 | * @param mixed $value |
|
471 | 12 | * @param string|object $class |
|
472 | * @param string $message |
||
473 | * |
||
474 | * @throws InvalidArgumentException |
||
475 | 4 | */ |
|
476 | View Code Duplication | public static function notInstanceOf($value, $class, $message = '') |
|
486 | |||
487 | /** |
||
488 | * @psalm-pure |
||
489 | * @psalm-param array<class-string> $classes |
||
490 | 20 | * |
|
491 | * @param mixed $value |
||
492 | 20 | * @param array<object|string> $classes |
|
493 | * @param string $message |
||
494 | 16 | * |
|
495 | 4 | * @throws InvalidArgumentException |
|
496 | 4 | */ |
|
497 | 4 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
511 | |||
512 | /** |
||
513 | 24 | * @psalm-pure |
|
514 | * @psalm-template ExpectedType of object |
||
515 | 24 | * @psalm-param class-string<ExpectedType> $class |
|
516 | 24 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
|
517 | * |
||
518 | 20 | * @param object|string $value |
|
519 | 8 | * @param string $class |
|
520 | * @param string $message |
||
521 | * |
||
522 | * @throws InvalidArgumentException |
||
523 | 12 | */ |
|
524 | 12 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
536 | |||
537 | /** |
||
538 | * @psalm-pure |
||
539 | 23 | * @psalm-template UnexpectedType of object |
|
540 | * @psalm-param class-string<UnexpectedType> $class |
||
541 | 23 | * @psalm-assert !UnexpectedType $value |
|
542 | 8 | * @psalm-assert !class-string<UnexpectedType> $value |
|
543 | 8 | * |
|
544 | 8 | * @param object|string $value |
|
545 | * @param string $class |
||
546 | * @param string $message |
||
547 | 15 | * |
|
548 | * @throws InvalidArgumentException |
||
549 | */ |
||
550 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
|
562 | 23 | ||
563 | 23 | /** |
|
564 | * @psalm-pure |
||
565 | * @psalm-param array<class-string> $classes |
||
566 | 32 | * |
|
567 | * @param object|string $value |
||
568 | * @param string[] $classes |
||
569 | * @param string $message |
||
570 | * |
||
571 | * @throws InvalidArgumentException |
||
572 | */ |
||
573 | public static function isAnyOf($value, array $classes, $message = '') |
||
589 | |||
590 | /** |
||
591 | * @psalm-pure |
||
592 | * @psalm-assert empty $value |
||
593 | * |
||
594 | * @param mixed $value |
||
595 | * @param string|callable():string $message |
||
596 | 11 | * |
|
597 | * @throws InvalidArgumentException |
||
598 | 11 | */ |
|
599 | 3 | public static function isEmpty($value, $message = '') |
|
608 | |||
609 | /** |
||
610 | * @psalm-pure |
||
611 | * @psalm-assert !empty $value |
||
612 | * |
||
613 | * @param mixed $value |
||
614 | 15 | * @param string|callable():string $message |
|
615 | * |
||
616 | 15 | * @throws InvalidArgumentException |
|
617 | 11 | */ |
|
618 | 11 | public static function notEmpty($value, $message = '') |
|
627 | |||
628 | /** |
||
629 | * @psalm-pure |
||
630 | * @psalm-assert null $value |
||
631 | * |
||
632 | * @param mixed $value |
||
633 | 19 | * @param string|callable():string $message |
|
634 | * |
||
635 | 19 | * @throws InvalidArgumentException |
|
636 | 15 | */ |
|
637 | 15 | View Code Duplication | public static function null($value, $message = '') |
646 | |||
647 | /** |
||
648 | * @psalm-pure |
||
649 | * @psalm-assert !null $value |
||
650 | * |
||
651 | * @param mixed $value |
||
652 | 19 | * @param string|callable():string $message |
|
653 | * |
||
654 | 19 | * @throws InvalidArgumentException |
|
655 | 4 | */ |
|
656 | 4 | public static function notNull($value, $message = '') |
|
664 | |||
665 | /** |
||
666 | * @psalm-pure |
||
667 | 51 | * @psalm-assert true $value |
|
668 | * |
||
669 | 51 | * @param mixed $value |
|
670 | 19 | * @param string|callable():string $message |
|
671 | 19 | * |
|
672 | 19 | * @throws InvalidArgumentException |
|
673 | */ |
||
674 | View Code Duplication | public static function true($value, $message = '') |
|
683 | 51 | ||
684 | /** |
||
685 | 51 | * @psalm-pure |
|
686 | 35 | * @psalm-assert false $value |
|
687 | 35 | * |
|
688 | 35 | * @param mixed $value |
|
689 | * @param string|callable():string $message |
||
690 | * |
||
691 | 16 | * @throws InvalidArgumentException |
|
692 | */ |
||
693 | View Code Duplication | public static function false($value, $message = '') |
|
702 | 31 | ||
703 | 31 | /** |
|
704 | 31 | * @psalm-pure |
|
705 | * @psalm-assert !false $value |
||
706 | * |
||
707 | 20 | * @param mixed $value |
|
708 | * @param string|callable():string $message |
||
709 | * |
||
710 | * @throws InvalidArgumentException |
||
711 | */ |
||
712 | public static function notFalse($value, $message = '') |
||
720 | 12 | ||
721 | /** |
||
722 | * @param mixed $value |
||
723 | 8 | * @param string|callable():string $message |
|
724 | * |
||
725 | * @throws InvalidArgumentException |
||
726 | */ |
||
727 | View Code Duplication | public static function ip($value, $message = '') |
|
736 | 12 | ||
737 | /** |
||
738 | 12 | * @param mixed $value |
|
739 | 8 | * @param string|callable():string $message |
|
740 | * |
||
741 | 8 | * @throws InvalidArgumentException |
|
742 | 8 | */ |
|
743 | 8 | View Code Duplication | public static function ipv4($value, $message = '') |
752 | |||
753 | /** |
||
754 | * @param mixed $value |
||
755 | * @param string|callable():string $message |
||
756 | 33 | * |
|
757 | * @throws InvalidArgumentException |
||
758 | 33 | */ |
|
759 | 17 | View Code Duplication | public static function ipv6($value, $message = '') |
768 | |||
769 | /** |
||
770 | * @param mixed $value |
||
771 | * @param string|callable():string $message |
||
772 | * |
||
773 | * @throws InvalidArgumentException |
||
774 | 28 | */ |
|
775 | View Code Duplication | public static function email($value, $message = '') |
|
784 | |||
785 | /** |
||
786 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
787 | * |
||
788 | * @param array $values |
||
789 | * @param string|callable():string $message |
||
790 | * |
||
791 | * @throws InvalidArgumentException |
||
792 | */ |
||
793 | 16 | public static function uniqueValues(array $values, $message = '') |
|
808 | |||
809 | /** |
||
810 | * @param mixed $value |
||
811 | * @param mixed $expect |
||
812 | * @param string|callable():string $message |
||
813 | 16 | * |
|
814 | * @throws InvalidArgumentException |
||
815 | 16 | */ |
|
816 | 4 | public static function eq($value, $expect, $message = '') |
|
826 | |||
827 | /** |
||
828 | * @param mixed $value |
||
829 | * @param mixed $expect |
||
830 | * @param string|callable():string $message |
||
831 | * |
||
832 | 8 | * @throws InvalidArgumentException |
|
833 | */ |
||
834 | 8 | public static function notEq($value, $expect, $message = '') |
|
843 | |||
844 | /** |
||
845 | * @psalm-pure |
||
846 | * |
||
847 | * @param mixed $value |
||
848 | * @param mixed $expect |
||
849 | * @param string|callable():string $message |
||
850 | * |
||
851 | * @throws InvalidArgumentException |
||
852 | 12 | */ |
|
853 | public static function same($value, $expect, $message = '') |
||
863 | |||
864 | /** |
||
865 | * @psalm-pure |
||
866 | * |
||
867 | * @param mixed $value |
||
868 | * @param mixed $expect |
||
869 | * @param string|callable():string $message |
||
870 | * |
||
871 | * @throws InvalidArgumentException |
||
872 | 9 | */ |
|
873 | public static function notSame($value, $expect, $message = '') |
||
882 | |||
883 | /** |
||
884 | * @psalm-pure |
||
885 | * |
||
886 | * @param mixed $value |
||
887 | * @param mixed $limit |
||
888 | * @param string|callable():string $message |
||
889 | * |
||
890 | * @throws InvalidArgumentException |
||
891 | */ |
||
892 | 12 | public static function greaterThan($value, $limit, $message = '') |
|
902 | |||
903 | /** |
||
904 | * @psalm-pure |
||
905 | * |
||
906 | * @param mixed $value |
||
907 | * @param mixed $limit |
||
908 | * @param string|callable():string $message |
||
909 | * |
||
910 | * @throws InvalidArgumentException |
||
911 | */ |
||
912 | public static function greaterThanEq($value, $limit, $message = '') |
||
922 | 8 | ||
923 | /** |
||
924 | * @psalm-pure |
||
925 | 8 | * |
|
926 | * @param mixed $value |
||
927 | * @param mixed $limit |
||
928 | * @param string|callable():string $message |
||
929 | * |
||
930 | * @throws InvalidArgumentException |
||
931 | */ |
||
932 | public static function lessThan($value, $limit, $message = '') |
||
942 | |||
943 | /** |
||
944 | * @psalm-pure |
||
945 | * |
||
946 | * @param mixed $value |
||
947 | * @param mixed $limit |
||
948 | * @param string|callable():string $message |
||
949 | * |
||
950 | * @throws InvalidArgumentException |
||
951 | */ |
||
952 | public static function lessThanEq($value, $limit, $message = '') |
||
962 | |||
963 | 8 | /** |
|
964 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
965 | * |
||
966 | * @psalm-pure |
||
967 | * |
||
968 | * @param mixed $value |
||
969 | * @param mixed $min |
||
970 | * @param mixed $max |
||
971 | * @param string|callable():string $message |
||
972 | * |
||
973 | * @throws InvalidArgumentException |
||
974 | 80 | */ |
|
975 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
|
986 | |||
987 | /** |
||
988 | * A more human-readable alias of Assert::inArray(). |
||
989 | * |
||
990 | * @psalm-pure |
||
991 | * |
||
992 | * @param mixed $value |
||
993 | * @param array $values |
||
994 | 80 | * @param string|callable():string $message |
|
995 | * |
||
996 | 80 | * @throws InvalidArgumentException |
|
997 | 48 | */ |
|
998 | 48 | public static function oneOf($value, array $values, $message = '') |
|
1002 | |||
1003 | 32 | /** |
|
1004 | * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion. |
||
1005 | * |
||
1006 | * @psalm-pure |
||
1007 | * |
||
1008 | * @param mixed $value |
||
1009 | * @param array $values |
||
1010 | * @param string|callable():string $message |
||
1011 | * |
||
1012 | * @throws InvalidArgumentException |
||
1013 | 40 | */ |
|
1014 | public static function inArray($value, array $values, $message = '') |
||
1024 | |||
1025 | /** |
||
1026 | * @psalm-pure |
||
1027 | * |
||
1028 | * @param string $value |
||
1029 | * @param string $subString |
||
1030 | * @param string|callable():string $message |
||
1031 | * |
||
1032 | 48 | * @throws InvalidArgumentException |
|
1033 | */ |
||
1034 | 48 | View Code Duplication | public static function contains($value, $subString, $message = '') |
1044 | |||
1045 | /** |
||
1046 | * @psalm-pure |
||
1047 | * |
||
1048 | * @param string $value |
||
1049 | * @param string $subString |
||
1050 | * @param string|callable():string $message |
||
1051 | * |
||
1052 | 48 | * @throws InvalidArgumentException |
|
1053 | */ |
||
1054 | 48 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1064 | |||
1065 | /** |
||
1066 | * @psalm-pure |
||
1067 | * |
||
1068 | * @param string $value |
||
1069 | * @param string|callable():string $message |
||
1070 | * |
||
1071 | 35 | * @throws InvalidArgumentException |
|
1072 | */ |
||
1073 | 35 | public static function notWhitespaceOnly($value, $message = '') |
|
1082 | |||
1083 | /** |
||
1084 | 24 | * @psalm-pure |
|
1085 | 12 | * |
|
1086 | 12 | * @param string $value |
|
1087 | 12 | * @param string $prefix |
|
1088 | * @param string|callable():string $message |
||
1089 | * |
||
1090 | 12 | * @throws InvalidArgumentException |
|
1091 | */ |
||
1092 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
|
1102 | |||
1103 | 48 | /** |
|
1104 | 32 | * @psalm-pure |
|
1105 | 32 | * |
|
1106 | 32 | * @param string $value |
|
1107 | 32 | * @param string $prefix |
|
1108 | * @param string|callable():string $message |
||
1109 | * |
||
1110 | 16 | * @throws InvalidArgumentException |
|
1111 | */ |
||
1112 | View Code Duplication | public static function notStartsWith($value, $prefix, $message = '') |
|
1122 | |||
1123 | 48 | /** |
|
1124 | 16 | * @psalm-pure |
|
1125 | 16 | * |
|
1126 | 16 | * @param mixed $value |
|
1127 | 16 | * @param string|callable():string $message |
|
1128 | * |
||
1129 | * @throws InvalidArgumentException |
||
1130 | 32 | */ |
|
1131 | public static function startsWithLetter($value, $message = '') |
||
1151 | |||
1152 | /** |
||
1153 | * @psalm-pure |
||
1154 | * |
||
1155 | * @param string $value |
||
1156 | * @param string $suffix |
||
1157 | * @param string|callable():string $message |
||
1158 | * |
||
1159 | * @throws InvalidArgumentException |
||
1160 | 12 | */ |
|
1161 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
|
1171 | |||
1172 | /** |
||
1173 | * @psalm-pure |
||
1174 | * |
||
1175 | * @param string $value |
||
1176 | * @param string $suffix |
||
1177 | * @param string|callable():string $message |
||
1178 | * |
||
1179 | * @throws InvalidArgumentException |
||
1180 | 28 | */ |
|
1181 | View Code Duplication | public static function notEndsWith($value, $suffix, $message = '') |
|
1191 | |||
1192 | /** |
||
1193 | * @psalm-pure |
||
1194 | * |
||
1195 | * @param string $value |
||
1196 | * @param string $pattern |
||
1197 | * @param string|callable():string $message |
||
1198 | * |
||
1199 | * @throws InvalidArgumentException |
||
1200 | 20 | */ |
|
1201 | public static function regex($value, $pattern, $message = '') |
||
1210 | 8 | ||
1211 | 8 | /** |
|
1212 | 8 | * @psalm-pure |
|
1213 | * |
||
1214 | * @param string $value |
||
1215 | 4 | * @param string $pattern |
|
1216 | * @param string|callable():string $message |
||
1217 | * |
||
1218 | * @throws InvalidArgumentException |
||
1219 | */ |
||
1220 | public static function notRegex($value, $pattern, $message = '') |
||
1231 | |||
1232 | 12 | /** |
|
1233 | 8 | * @psalm-pure |
|
1234 | 8 | * |
|
1235 | 8 | * @param mixed $value |
|
1236 | * @param string|callable():string $message |
||
1237 | * |
||
1238 | 4 | * @throws InvalidArgumentException |
|
1239 | */ |
||
1240 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
|
1251 | 12 | ||
1252 | 12 | /** |
|
1253 | 12 | * @psalm-pure |
|
1254 | * |
||
1255 | 12 | * @param mixed $value |
|
1256 | 8 | * @param string|callable():string $message |
|
1257 | 8 | * |
|
1258 | 8 | * @throws InvalidArgumentException |
|
1259 | */ |
||
1260 | View Code Duplication | public static function alpha($value, $message = '') |
|
1276 | 16 | ||
1277 | 16 | /** |
|
1278 | * @psalm-pure |
||
1279 | 16 | * |
|
1280 | 12 | * @param string $value |
|
1281 | 12 | * @param string|callable():string $message |
|
1282 | 12 | * |
|
1283 | * @throws InvalidArgumentException |
||
1284 | */ |
||
1285 | 4 | View Code Duplication | public static function digits($value, $message = '') |
1299 | 16 | ||
1300 | 16 | /** |
|
1301 | 16 | * @psalm-pure |
|
1302 | * |
||
1303 | 16 | * @param string $value |
|
1304 | 12 | * @param string|callable():string $message |
|
1305 | 12 | * |
|
1306 | 12 | * @throws InvalidArgumentException |
|
1307 | */ |
||
1308 | View Code Duplication | public static function alnum($value, $message = '') |
|
1322 | 36 | ||
1323 | 24 | /** |
|
1324 | 24 | * @psalm-pure |
|
1325 | 24 | * @psalm-assert lowercase-string $value |
|
1326 | 24 | * |
|
1327 | * @param string $value |
||
1328 | * @param string|callable():string $message |
||
1329 | 12 | * |
|
1330 | * @throws InvalidArgumentException |
||
1331 | */ |
||
1332 | View Code Duplication | public static function lower($value, $message = '') |
|
1346 | 12 | ||
1347 | 12 | /** |
|
1348 | 12 | * @psalm-pure |
|
1349 | * @psalm-assert !lowercase-string $value |
||
1350 | * |
||
1351 | 24 | * @param string $value |
|
1352 | * @param string|callable():string $message |
||
1353 | * |
||
1354 | * @throws InvalidArgumentException |
||
1355 | */ |
||
1356 | View Code Duplication | public static function upper($value, $message = '') |
|
1370 | 12 | ||
1371 | /** |
||
1372 | * @psalm-pure |
||
1373 | 24 | * |
|
1374 | * @param string $value |
||
1375 | * @param int $length |
||
1376 | * @param string|callable():string $message |
||
1377 | * |
||
1378 | * @throws InvalidArgumentException |
||
1379 | */ |
||
1380 | View Code Duplication | public static function length($value, $length, $message = '') |
|
1390 | |||
1391 | 60 | /** |
|
1392 | 24 | * Inclusive min. |
|
1393 | 24 | * |
|
1394 | 24 | * @psalm-pure |
|
1395 | 24 | * |
|
1396 | 24 | * @param string $value |
|
1397 | * @param int|float $min |
||
1398 | * @param string $message |
||
1399 | 36 | * |
|
1400 | * @throws InvalidArgumentException |
||
1401 | */ |
||
1402 | View Code Duplication | public static function minLength($value, $min, $message = '') |
|
1412 | |||
1413 | 36 | /** |
|
1414 | 12 | * Inclusive max. |
|
1415 | 12 | * |
|
1416 | 12 | * @psalm-pure |
|
1417 | * |
||
1418 | * @param string $value |
||
1419 | 24 | * @param int|float $max |
|
1420 | * @param string $message |
||
1421 | * |
||
1422 | * @throws InvalidArgumentException |
||
1423 | */ |
||
1424 | View Code Duplication | public static function maxLength($value, $max, $message = '') |
|
1434 | 4 | ||
1435 | /** |
||
1436 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1437 | 4 | * |
|
1438 | * @psalm-pure |
||
1439 | * |
||
1440 | * @param string $value |
||
1441 | * @param int|float $min |
||
1442 | * @param int|float $max |
||
1443 | * @param string $message |
||
1444 | * |
||
1445 | 12 | * @throws InvalidArgumentException |
|
1446 | */ |
||
1447 | 12 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
1460 | |||
1461 | /** |
||
1462 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1463 | * |
||
1464 | * @param mixed $value |
||
1465 | * @param string|callable():string $message |
||
1466 | * |
||
1467 | * @throws InvalidArgumentException |
||
1468 | */ |
||
1469 | View Code Duplication | public static function fileExists($value, $message = '') |
|
1480 | |||
1481 | /** |
||
1482 | * @param mixed $value |
||
1483 | * @param string|callable():string $message |
||
1484 | * |
||
1485 | * @throws InvalidArgumentException |
||
1486 | */ |
||
1487 | View Code Duplication | public static function file($value, $message = '') |
|
1498 | |||
1499 | 8 | /** |
|
1500 | 4 | * @param mixed $value |
|
1501 | 4 | * @param string|callable():string $message |
|
1502 | 4 | * |
|
1503 | * @throws InvalidArgumentException |
||
1504 | */ |
||
1505 | 4 | View Code Duplication | public static function directory($value, $message = '') |
1516 | |||
1517 | /** |
||
1518 | * @param string $value |
||
1519 | 8 | * @param string|callable():string $message |
|
1520 | * |
||
1521 | 8 | * @throws InvalidArgumentException |
|
1522 | 4 | */ |
|
1523 | 4 | public static function readable($value, $message = '') |
|
1532 | |||
1533 | /** |
||
1534 | * @param string $value |
||
1535 | * @param string|callable():string $message |
||
1536 | * |
||
1537 | * @throws InvalidArgumentException |
||
1538 | 8 | */ |
|
1539 | public static function writable($value, $message = '') |
||
1548 | |||
1549 | /** |
||
1550 | * @psalm-assert class-string $value |
||
1551 | * |
||
1552 | * @param mixed $value |
||
1553 | * @param string|callable():string $message |
||
1554 | * |
||
1555 | * @throws InvalidArgumentException |
||
1556 | */ |
||
1557 | public static function classExists($value, $message = '') |
||
1566 | 4 | ||
1567 | /** |
||
1568 | * @psalm-pure |
||
1569 | 4 | * @psalm-template ExpectedType of object |
|
1570 | * @psalm-param class-string<ExpectedType> $class |
||
1571 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
1572 | * |
||
1573 | * @param mixed $value |
||
1574 | * @param string|object $class |
||
1575 | * @param string $message |
||
1576 | * |
||
1577 | * @throws InvalidArgumentException |
||
1578 | */ |
||
1579 | public static function subclassOf($value, $class, $message = '') |
||
1589 | 8 | ||
1590 | /** |
||
1591 | * @psalm-assert class-string $value |
||
1592 | * |
||
1593 | * @param mixed $value |
||
1594 | * @param string|callable():string $message |
||
1595 | * |
||
1596 | * @throws InvalidArgumentException |
||
1597 | */ |
||
1598 | public static function interfaceExists($value, $message = '') |
||
1607 | |||
1608 | /** |
||
1609 | 4 | * @psalm-pure |
|
1610 | * @psalm-template ExpectedType of object |
||
1611 | * @psalm-param class-string<ExpectedType> $interface |
||
1612 | * @psalm-assert class-string<ExpectedType> $value |
||
1613 | * |
||
1614 | * @param mixed $value |
||
1615 | * @param mixed $interface |
||
1616 | * @param string|callable():string $message |
||
1617 | * |
||
1618 | * @throws InvalidArgumentException |
||
1619 | */ |
||
1620 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
|
1630 | |||
1631 | /** |
||
1632 | * @psalm-pure |
||
1633 | * @psalm-param class-string|object $classOrObject |
||
1634 | * |
||
1635 | * @param string|object $classOrObject |
||
1636 | * @param mixed $property |
||
1637 | * @param string $message |
||
1638 | * |
||
1639 | * @throws InvalidArgumentException |
||
1640 | */ |
||
1641 | 27 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1650 | |||
1651 | /** |
||
1652 | * @psalm-pure |
||
1653 | * @psalm-param class-string|object $classOrObject |
||
1654 | * |
||
1655 | * @param string|object $classOrObject |
||
1656 | * @param mixed $property |
||
1657 | * @param string $message |
||
1658 | * |
||
1659 | * @throws InvalidArgumentException |
||
1660 | 12 | */ |
|
1661 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
|
1670 | |||
1671 | /** |
||
1672 | * @psalm-pure |
||
1673 | * @psalm-param class-string|object $classOrObject |
||
1674 | * |
||
1675 | * @param string|object $classOrObject |
||
1676 | * @param mixed $method |
||
1677 | * @param string $message |
||
1678 | * |
||
1679 | 12 | * @throws InvalidArgumentException |
|
1680 | */ |
||
1681 | 12 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1690 | |||
1691 | /** |
||
1692 | * @psalm-pure |
||
1693 | * @psalm-param class-string|object $classOrObject |
||
1694 | * |
||
1695 | * @param string|object $classOrObject |
||
1696 | * @param mixed $method |
||
1697 | * @param string $message |
||
1698 | * |
||
1699 | * @throws InvalidArgumentException |
||
1700 | 28 | */ |
|
1701 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
|
1710 | |||
1711 | /** |
||
1712 | * @psalm-pure |
||
1713 | * |
||
1714 | * @param array $array |
||
1715 | * @param string|int $key |
||
1716 | * @param string $message |
||
1717 | * |
||
1718 | * @throws InvalidArgumentException |
||
1719 | 8 | */ |
|
1720 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
|
1729 | |||
1730 | 4 | /** |
|
1731 | * @psalm-pure |
||
1732 | * |
||
1733 | * @param array $array |
||
1734 | * @param string|int $key |
||
1735 | * @param string $message |
||
1736 | * |
||
1737 | * @throws InvalidArgumentException |
||
1738 | */ |
||
1739 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
|
1748 | |||
1749 | /** |
||
1750 | 8 | * Checks if a value is a valid array key (int or string). |
|
1751 | * |
||
1752 | * @psalm-pure |
||
1753 | * @psalm-assert array-key $value |
||
1754 | * |
||
1755 | * @param mixed $value |
||
1756 | * @param string|callable():string $message |
||
1757 | * |
||
1758 | * @throws InvalidArgumentException |
||
1759 | */ |
||
1760 | public static function validArrayKey($value, $message = '') |
||
1773 | |||
1774 | /** |
||
1775 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1776 | * |
||
1777 | * @param Countable|array $array |
||
1778 | * @param int $number |
||
1779 | * @param string $message |
||
1780 | * |
||
1781 | * @throws InvalidArgumentException |
||
1782 | 20 | */ |
|
1783 | public static function count($array, $number, $message = '') |
||
1795 | |||
1796 | /** |
||
1797 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1798 | * |
||
1799 | * @param Countable|array $array |
||
1800 | * @param int|float $min |
||
1801 | * @param string $message |
||
1802 | * |
||
1803 | * @throws InvalidArgumentException |
||
1804 | */ |
||
1805 | 80 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1815 | |||
1816 | /** |
||
1817 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1818 | * |
||
1819 | * @param Countable|array $array |
||
1820 | * @param int|float $max |
||
1821 | * @param string $message |
||
1822 | * |
||
1823 | 40 | * @throws InvalidArgumentException |
|
1824 | */ |
||
1825 | 40 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1835 | |||
1836 | /** |
||
1837 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1838 | * |
||
1839 | * @param Countable|array $array |
||
1840 | 32 | * @param int|float $min |
|
1841 | * @param int|float $max |
||
1842 | * @param string $message |
||
1843 | 32 | * |
|
1844 | 32 | * @throws InvalidArgumentException |
|
1845 | */ |
||
1846 | 16 | public static function countBetween($array, $min, $max, $message = '') |
|
1859 | |||
1860 | /** |
||
1861 | * @psalm-pure |
||
1862 | * @psalm-assert list $array |
||
1863 | * |
||
1864 | 16 | * @param mixed $array |
|
1865 | * @param string|callable():string $message |
||
1866 | 16 | * |
|
1867 | 8 | * @throws InvalidArgumentException |
|
1868 | 4 | */ |
|
1869 | View Code Duplication | public static function isList($array, $message = '') |
|
1877 | |||
1878 | 56 | /** |
|
1879 | * @psalm-pure |
||
1880 | 56 | * @psalm-assert non-empty-list $array |
|
1881 | * |
||
1882 | * @param mixed $array |
||
1883 | * @param string|callable():string $message |
||
1884 | 56 | * |
|
1885 | 4 | * @throws InvalidArgumentException |
|
1886 | */ |
||
1887 | public static function isNonEmptyList($array, $message = '') |
||
1892 | |||
1893 | /** |
||
1894 | 32 | * @psalm-pure |
|
1895 | * @psalm-template T |
||
1896 | * @psalm-param mixed|array<T> $array |
||
1897 | * @psalm-assert array<string, T> $array |
||
1898 | * |
||
1899 | * @param mixed $array |
||
1900 | * @param string|callable():string $message |
||
1901 | * |
||
1902 | * @throws InvalidArgumentException |
||
1903 | */ |
||
1904 | View Code Duplication | public static function isMap($array, $message = '') |
|
1915 | 20 | ||
1916 | 20 | /** |
|
1917 | * @psalm-pure |
||
1918 | 4 | * @psalm-template T |
|
1919 | 4 | * @psalm-param mixed|array<T> $array |
|
1920 | 4 | * @psalm-assert array<string, T> $array |
|
1921 | 4 | * @psalm-assert !empty $array |
|
1922 | * |
||
1923 | * @param mixed $array |
||
1924 | * @param string|callable():string $message |
||
1925 | 8 | * |
|
1926 | 8 | * @throws InvalidArgumentException |
|
1927 | 8 | */ |
|
1928 | 8 | public static function isNonEmptyMap($array, $message = '') |
|
1933 | |||
1934 | /** |
||
1935 | 1642 | * @psalm-pure |
|
1936 | * |
||
1937 | 1642 | * @param string $value |
|
1938 | 607 | * @param string|callable():string $message |
|
1939 | 501 | * |
|
1940 | 501 | * @throws InvalidArgumentException |
|
1941 | */ |
||
1942 | public static function uuid($value, $message = '') |
||
1959 | |||
1960 | /** |
||
1961 | 1 | * @psalm-param class-string<Throwable> $class |
|
1962 | * |
||
1963 | * @param Closure $expression |
||
1964 | * @param string $class |
||
1965 | * @param string $message |
||
1966 | * |
||
1967 | * @throws InvalidArgumentException |
||
1968 | */ |
||
1969 | 751 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
1995 | |||
1996 | 1 | /** |
|
1997 | * @throws BadMethodCallException |
||
1998 | */ |
||
1999 | 676 | public static function __callStatic($name, $arguments) |
|
2027 | |||
2028 | /** |
||
2029 | * @param mixed $value |
||
2030 | 168 | * |
|
2031 | * @return string |
||
2032 | */ |
||
2033 | protected static function valueToString($value) |
||
2073 | |||
2074 | /** |
||
2075 | * @param mixed $value |
||
2076 | * |
||
2077 | * @return string |
||
2078 | */ |
||
2079 | protected static function typeToString($value) |
||
2083 | |||
2084 | protected static function strlen($value) |
||
2096 | |||
2097 | /** |
||
2098 | * @param string|callable():string $message |
||
2099 | * |
||
2100 | * @throws InvalidArgumentException |
||
2101 | * |
||
2102 | * @psalm-pure this method is not supposed to perform side-effects |
||
2103 | */ |
||
2104 | protected static function reportInvalidArgument($message) |
||
2112 | |||
2113 | private function __construct() |
||
2116 | } |
||
2117 |