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 |
||
209 | class Assert |
||
|
|||
210 | { |
||
211 | /** |
||
212 | * @psalm-pure |
||
213 | * @psalm-assert string $value |
||
214 | * |
||
215 | * @param mixed $value |
||
216 | * @param string $message |
||
217 | * |
||
218 | * @throws InvalidArgumentException |
||
219 | */ |
||
220 | 241 | public static function string($value, $message = '') |
|
229 | |||
230 | /** |
||
231 | * @psalm-pure |
||
232 | * @psalm-assert string $value |
||
233 | * @psalm-assert !empty $value |
||
234 | * |
||
235 | * @param mixed $value |
||
236 | * @param string $message |
||
237 | * |
||
238 | * @throws InvalidArgumentException |
||
239 | */ |
||
240 | 16 | public static function stringNotEmpty($value, $message = '') |
|
245 | |||
246 | /** |
||
247 | * @psalm-pure |
||
248 | * @psalm-assert int $value |
||
249 | * |
||
250 | * @param mixed $value |
||
251 | * @param string $message |
||
252 | * |
||
253 | * @throws InvalidArgumentException |
||
254 | */ |
||
255 | 17 | public static function integer($value, $message = '') |
|
264 | |||
265 | /** |
||
266 | * @psalm-pure |
||
267 | * @psalm-assert numeric $value |
||
268 | * |
||
269 | * @param mixed $value |
||
270 | * @param string $message |
||
271 | * |
||
272 | * @throws InvalidArgumentException |
||
273 | */ |
||
274 | 16 | public static function integerish($value, $message = '') |
|
283 | |||
284 | /** |
||
285 | * @psalm-pure |
||
286 | * @psalm-assert float $value |
||
287 | * |
||
288 | * @param mixed $value |
||
289 | * @param string $message |
||
290 | * |
||
291 | * @throws InvalidArgumentException |
||
292 | */ |
||
293 | 16 | public static function float($value, $message = '') |
|
302 | |||
303 | /** |
||
304 | * @psalm-pure |
||
305 | * @psalm-assert numeric $value |
||
306 | * |
||
307 | * @param mixed $value |
||
308 | * @param string $message |
||
309 | * |
||
310 | * @throws InvalidArgumentException |
||
311 | */ |
||
312 | 20 | public static function numeric($value, $message = '') |
|
321 | |||
322 | /** |
||
323 | * @psalm-pure |
||
324 | * @psalm-assert int $value |
||
325 | * |
||
326 | * @param mixed $value |
||
327 | * @param string $message |
||
328 | * |
||
329 | * @throws InvalidArgumentException |
||
330 | */ |
||
331 | 24 | public static function natural($value, $message = '') |
|
340 | |||
341 | /** |
||
342 | * @psalm-pure |
||
343 | * @psalm-assert bool $value |
||
344 | * |
||
345 | * @param mixed $value |
||
346 | * @param string $message |
||
347 | * |
||
348 | * @throws InvalidArgumentException |
||
349 | */ |
||
350 | 16 | public static function boolean($value, $message = '') |
|
359 | |||
360 | /** |
||
361 | * @psalm-pure |
||
362 | * @psalm-assert scalar $value |
||
363 | * |
||
364 | * @param mixed $value |
||
365 | * @param string $message |
||
366 | * |
||
367 | * @throws InvalidArgumentException |
||
368 | */ |
||
369 | 23 | public static function scalar($value, $message = '') |
|
378 | |||
379 | /** |
||
380 | * @psalm-pure |
||
381 | * @psalm-assert object $value |
||
382 | * |
||
383 | * @param mixed $value |
||
384 | * @param string $message |
||
385 | * |
||
386 | * @throws InvalidArgumentException |
||
387 | */ |
||
388 | 23 | public static function object($value, $message = '') |
|
397 | |||
398 | /** |
||
399 | * @psalm-pure |
||
400 | * @psalm-assert resource $value |
||
401 | * |
||
402 | * @param mixed $value |
||
403 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
404 | * @param string $message |
||
405 | * |
||
406 | * @throws InvalidArgumentException |
||
407 | */ |
||
408 | 16 | public static function resource($value, $type = null, $message = '') |
|
425 | |||
426 | /** |
||
427 | * @psalm-pure |
||
428 | * @psalm-assert callable $value |
||
429 | * |
||
430 | * @param mixed $value |
||
431 | * @param string $message |
||
432 | * |
||
433 | * @throws InvalidArgumentException |
||
434 | */ |
||
435 | 20 | public static function isCallable($value, $message = '') |
|
444 | |||
445 | /** |
||
446 | * @psalm-pure |
||
447 | * @psalm-assert array $value |
||
448 | * |
||
449 | * @param mixed $value |
||
450 | * @param string $message |
||
451 | * |
||
452 | * @throws InvalidArgumentException |
||
453 | */ |
||
454 | 20 | public static function isArray($value, $message = '') |
|
463 | |||
464 | /** |
||
465 | * @psalm-pure |
||
466 | * @psalm-assert iterable $value |
||
467 | * |
||
468 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
469 | * |
||
470 | * @param mixed $value |
||
471 | * @param string $message |
||
472 | * |
||
473 | * @throws InvalidArgumentException |
||
474 | */ |
||
475 | 20 | public static function isTraversable($value, $message = '') |
|
492 | |||
493 | /** |
||
494 | * @psalm-pure |
||
495 | * @psalm-assert array|ArrayAccess $value |
||
496 | * |
||
497 | * @param mixed $value |
||
498 | * @param string $message |
||
499 | * |
||
500 | * @throws InvalidArgumentException |
||
501 | */ |
||
502 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
511 | |||
512 | /** |
||
513 | * @psalm-pure |
||
514 | * @psalm-assert countable $value |
||
515 | * |
||
516 | * @param mixed $value |
||
517 | * @param string $message |
||
518 | * |
||
519 | * @throws InvalidArgumentException |
||
520 | */ |
||
521 | 28 | public static function isCountable($value, $message = '') |
|
535 | |||
536 | /** |
||
537 | * @psalm-pure |
||
538 | * @psalm-assert iterable $value |
||
539 | * |
||
540 | * @param mixed $value |
||
541 | * @param string $message |
||
542 | * |
||
543 | * @throws InvalidArgumentException |
||
544 | */ |
||
545 | 992 | View Code Duplication | public static function isIterable($value, $message = '') |
554 | |||
555 | /** |
||
556 | * @psalm-pure |
||
557 | * @psalm-template ExpectedType of object |
||
558 | * @psalm-param class-string<ExpectedType> $class |
||
559 | * @psalm-assert ExpectedType $value |
||
560 | * |
||
561 | * @param mixed $value |
||
562 | * @param string|object $class |
||
563 | * @param string $message |
||
564 | * |
||
565 | * @throws InvalidArgumentException |
||
566 | */ |
||
567 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
577 | |||
578 | /** |
||
579 | * @psalm-pure |
||
580 | * @psalm-template ExpectedType of object |
||
581 | * @psalm-param class-string<ExpectedType> $class |
||
582 | * @psalm-assert !ExpectedType $value |
||
583 | * |
||
584 | * @param mixed $value |
||
585 | * @param string|object $class |
||
586 | * @param string $message |
||
587 | * |
||
588 | * @throws InvalidArgumentException |
||
589 | */ |
||
590 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
600 | |||
601 | /** |
||
602 | * @psalm-pure |
||
603 | * @psalm-param array<class-string> $classes |
||
604 | * |
||
605 | * @param mixed $value |
||
606 | * @param array<object|string> $classes |
||
607 | * @param string $message |
||
608 | * |
||
609 | * @throws InvalidArgumentException |
||
610 | */ |
||
611 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
625 | |||
626 | /** |
||
627 | * @psalm-pure |
||
628 | * @psalm-template ExpectedType of object |
||
629 | * @psalm-param class-string<ExpectedType> $class |
||
630 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
||
631 | * |
||
632 | * @param object|string $value |
||
633 | * @param string $class |
||
634 | * @param string $message |
||
635 | * |
||
636 | * @throws InvalidArgumentException |
||
637 | */ |
||
638 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
650 | |||
651 | /** |
||
652 | * @psalm-pure |
||
653 | * @psalm-template UnexpectedType of object |
||
654 | * @psalm-param class-string<UnexpectedType> $class |
||
655 | * @psalm-assert !UnexpectedType $value |
||
656 | * @psalm-assert !class-string<UnexpectedType> $value |
||
657 | * |
||
658 | * @param object|string $value |
||
659 | * @param string $class |
||
660 | * @param string $message |
||
661 | * |
||
662 | * @throws InvalidArgumentException |
||
663 | */ |
||
664 | 20 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
676 | |||
677 | /** |
||
678 | * @psalm-pure |
||
679 | * @psalm-param array<class-string> $classes |
||
680 | * |
||
681 | * @param object|string $value |
||
682 | * @param string[] $classes |
||
683 | * @param string $message |
||
684 | * |
||
685 | * @throws InvalidArgumentException |
||
686 | */ |
||
687 | 24 | public static function isAnyOf($value, array $classes, $message = '') |
|
703 | |||
704 | /** |
||
705 | * @psalm-pure |
||
706 | * @psalm-assert empty $value |
||
707 | * |
||
708 | * @param mixed $value |
||
709 | * @param string $message |
||
710 | * |
||
711 | * @throws InvalidArgumentException |
||
712 | */ |
||
713 | 23 | public static function isEmpty($value, $message = '') |
|
722 | |||
723 | /** |
||
724 | * @psalm-pure |
||
725 | * @psalm-assert !empty $value |
||
726 | * |
||
727 | * @param mixed $value |
||
728 | * @param string $message |
||
729 | * |
||
730 | * @throws InvalidArgumentException |
||
731 | */ |
||
732 | 55 | public static function notEmpty($value, $message = '') |
|
741 | |||
742 | /** |
||
743 | * @psalm-pure |
||
744 | * @psalm-assert null $value |
||
745 | * |
||
746 | * @param mixed $value |
||
747 | * @param string $message |
||
748 | * |
||
749 | * @throws InvalidArgumentException |
||
750 | */ |
||
751 | 11 | public static function null($value, $message = '') |
|
760 | |||
761 | /** |
||
762 | * @psalm-pure |
||
763 | * @psalm-assert !null $value |
||
764 | * |
||
765 | * @param mixed $value |
||
766 | * @param string $message |
||
767 | * |
||
768 | * @throws InvalidArgumentException |
||
769 | */ |
||
770 | 11 | public static function notNull($value, $message = '') |
|
778 | |||
779 | /** |
||
780 | * @psalm-pure |
||
781 | * @psalm-assert true $value |
||
782 | * |
||
783 | * @param mixed $value |
||
784 | * @param string $message |
||
785 | * |
||
786 | * @throws InvalidArgumentException |
||
787 | */ |
||
788 | 15 | public static function true($value, $message = '') |
|
797 | |||
798 | /** |
||
799 | * @psalm-pure |
||
800 | * @psalm-assert false $value |
||
801 | * |
||
802 | * @param mixed $value |
||
803 | * @param string $message |
||
804 | * |
||
805 | * @throws InvalidArgumentException |
||
806 | */ |
||
807 | 19 | public static function false($value, $message = '') |
|
816 | |||
817 | /** |
||
818 | * @psalm-assert !false $value |
||
819 | * |
||
820 | * @param mixed $value |
||
821 | * @param string $message |
||
822 | * |
||
823 | * @throws InvalidArgumentException |
||
824 | */ |
||
825 | 19 | public static function notFalse($value, $message = '') |
|
833 | |||
834 | /** |
||
835 | * @param mixed $value |
||
836 | * @param string $message |
||
837 | * |
||
838 | * @throws InvalidArgumentException |
||
839 | */ |
||
840 | 51 | View Code Duplication | public static function ip($value, $message = '') |
849 | |||
850 | /** |
||
851 | * @param mixed $value |
||
852 | * @param string $message |
||
853 | * |
||
854 | * @throws InvalidArgumentException |
||
855 | */ |
||
856 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
865 | |||
866 | /** |
||
867 | * @param mixed $value |
||
868 | * @param string $message |
||
869 | * |
||
870 | * @throws InvalidArgumentException |
||
871 | */ |
||
872 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
881 | |||
882 | /** |
||
883 | * @param mixed $value |
||
884 | * @param string $message |
||
885 | * |
||
886 | * @throws InvalidArgumentException |
||
887 | */ |
||
888 | 20 | View Code Duplication | public static function email($value, $message = '') |
897 | |||
898 | /** |
||
899 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
900 | * |
||
901 | * @param array $values |
||
902 | * @param string $message |
||
903 | * |
||
904 | * @throws InvalidArgumentException |
||
905 | */ |
||
906 | 12 | public static function uniqueValues(array $values, $message = '') |
|
921 | |||
922 | /** |
||
923 | * @param mixed $value |
||
924 | * @param mixed $expect |
||
925 | * @param string $message |
||
926 | * |
||
927 | * @throws InvalidArgumentException |
||
928 | */ |
||
929 | 33 | public static function eq($value, $expect, $message = '') |
|
939 | |||
940 | /** |
||
941 | * @param mixed $value |
||
942 | * @param mixed $expect |
||
943 | * @param string $message |
||
944 | * |
||
945 | * @throws InvalidArgumentException |
||
946 | */ |
||
947 | 28 | public static function notEq($value, $expect, $message = '') |
|
956 | |||
957 | /** |
||
958 | * @psalm-pure |
||
959 | * |
||
960 | * @param mixed $value |
||
961 | * @param mixed $expect |
||
962 | * @param string $message |
||
963 | * |
||
964 | * @throws InvalidArgumentException |
||
965 | */ |
||
966 | 16 | public static function same($value, $expect, $message = '') |
|
976 | |||
977 | /** |
||
978 | * @psalm-pure |
||
979 | * |
||
980 | * @param mixed $value |
||
981 | * @param mixed $expect |
||
982 | * @param string $message |
||
983 | * |
||
984 | * @throws InvalidArgumentException |
||
985 | */ |
||
986 | 16 | public static function notSame($value, $expect, $message = '') |
|
995 | |||
996 | /** |
||
997 | * @psalm-pure |
||
998 | * |
||
999 | * @param mixed $value |
||
1000 | * @param mixed $limit |
||
1001 | * @param string $message |
||
1002 | * |
||
1003 | * @throws InvalidArgumentException |
||
1004 | */ |
||
1005 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
1015 | |||
1016 | /** |
||
1017 | * @psalm-pure |
||
1018 | * |
||
1019 | * @param mixed $value |
||
1020 | * @param mixed $limit |
||
1021 | * @param string $message |
||
1022 | * |
||
1023 | * @throws InvalidArgumentException |
||
1024 | */ |
||
1025 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
1035 | |||
1036 | /** |
||
1037 | * @psalm-pure |
||
1038 | * |
||
1039 | * @param mixed $value |
||
1040 | * @param mixed $limit |
||
1041 | * @param string $message |
||
1042 | * |
||
1043 | * @throws InvalidArgumentException |
||
1044 | */ |
||
1045 | 9 | public static function lessThan($value, $limit, $message = '') |
|
1055 | |||
1056 | /** |
||
1057 | * @psalm-pure |
||
1058 | * |
||
1059 | * @param mixed $value |
||
1060 | * @param mixed $limit |
||
1061 | * @param string $message |
||
1062 | * |
||
1063 | * @throws InvalidArgumentException |
||
1064 | */ |
||
1065 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
1075 | |||
1076 | /** |
||
1077 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
1078 | * |
||
1079 | * @psalm-pure |
||
1080 | * |
||
1081 | * @param mixed $value |
||
1082 | * @param mixed $min |
||
1083 | * @param mixed $max |
||
1084 | * @param string $message |
||
1085 | * |
||
1086 | * @throws InvalidArgumentException |
||
1087 | */ |
||
1088 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
1099 | |||
1100 | /** |
||
1101 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
1102 | * |
||
1103 | * @psalm-pure |
||
1104 | * |
||
1105 | * @param mixed $value |
||
1106 | * @param array $values |
||
1107 | * @param string $message |
||
1108 | * |
||
1109 | * @throws InvalidArgumentException |
||
1110 | */ |
||
1111 | 8 | public static function oneOf($value, array $values, $message = '') |
|
1121 | |||
1122 | /** |
||
1123 | * @psalm-pure |
||
1124 | * |
||
1125 | * @param string $value |
||
1126 | * @param string $subString |
||
1127 | * @param string $message |
||
1128 | * |
||
1129 | * @throws InvalidArgumentException |
||
1130 | */ |
||
1131 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
1141 | |||
1142 | /** |
||
1143 | * @psalm-pure |
||
1144 | * |
||
1145 | * @param string $value |
||
1146 | * @param string $subString |
||
1147 | * @param string $message |
||
1148 | * |
||
1149 | * @throws InvalidArgumentException |
||
1150 | */ |
||
1151 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1161 | |||
1162 | /** |
||
1163 | * @psalm-pure |
||
1164 | * |
||
1165 | * @param string $value |
||
1166 | * @param string $message |
||
1167 | * |
||
1168 | * @throws InvalidArgumentException |
||
1169 | */ |
||
1170 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
1179 | |||
1180 | /** |
||
1181 | * @psalm-pure |
||
1182 | * |
||
1183 | * @param string $value |
||
1184 | * @param string $prefix |
||
1185 | * @param string $message |
||
1186 | * |
||
1187 | * @throws InvalidArgumentException |
||
1188 | */ |
||
1189 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
1199 | |||
1200 | /** |
||
1201 | * @psalm-pure |
||
1202 | * |
||
1203 | * @param mixed $value |
||
1204 | * @param string $message |
||
1205 | * |
||
1206 | * @throws InvalidArgumentException |
||
1207 | */ |
||
1208 | 35 | public static function startsWithLetter($value, $message = '') |
|
1228 | |||
1229 | /** |
||
1230 | * @psalm-pure |
||
1231 | * |
||
1232 | * @param string $value |
||
1233 | * @param string $suffix |
||
1234 | * @param string $message |
||
1235 | * |
||
1236 | * @throws InvalidArgumentException |
||
1237 | */ |
||
1238 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
1248 | |||
1249 | /** |
||
1250 | * @psalm-pure |
||
1251 | * |
||
1252 | * @param string $value |
||
1253 | * @param string $pattern |
||
1254 | * @param string $message |
||
1255 | * |
||
1256 | * @throws InvalidArgumentException |
||
1257 | */ |
||
1258 | 12 | public static function regex($value, $pattern, $message = '') |
|
1267 | |||
1268 | /** |
||
1269 | * @psalm-pure |
||
1270 | * |
||
1271 | * @param string $value |
||
1272 | * @param string $pattern |
||
1273 | * @param string $message |
||
1274 | * |
||
1275 | * @throws InvalidArgumentException |
||
1276 | */ |
||
1277 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
1288 | |||
1289 | /** |
||
1290 | * @psalm-pure |
||
1291 | * |
||
1292 | * @param mixed $value |
||
1293 | * @param string $message |
||
1294 | * |
||
1295 | * @throws InvalidArgumentException |
||
1296 | */ |
||
1297 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
1308 | |||
1309 | /** |
||
1310 | * @psalm-pure |
||
1311 | * |
||
1312 | * @param mixed $value |
||
1313 | * @param string $message |
||
1314 | * |
||
1315 | * @throws InvalidArgumentException |
||
1316 | */ |
||
1317 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
1333 | |||
1334 | /** |
||
1335 | * @psalm-pure |
||
1336 | * |
||
1337 | * @param string $value |
||
1338 | * @param string $message |
||
1339 | * |
||
1340 | * @throws InvalidArgumentException |
||
1341 | */ |
||
1342 | 12 | View Code Duplication | public static function digits($value, $message = '') |
1356 | |||
1357 | /** |
||
1358 | * @psalm-pure |
||
1359 | * |
||
1360 | * @param string $value |
||
1361 | * @param string $message |
||
1362 | * |
||
1363 | * @throws InvalidArgumentException |
||
1364 | */ |
||
1365 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
1379 | |||
1380 | /** |
||
1381 | * @psalm-pure |
||
1382 | * @psalm-assert lowercase-string $value |
||
1383 | * |
||
1384 | * @param string $value |
||
1385 | * @param string $message |
||
1386 | * |
||
1387 | * @throws InvalidArgumentException |
||
1388 | */ |
||
1389 | 16 | View Code Duplication | public static function lower($value, $message = '') |
1403 | |||
1404 | /** |
||
1405 | * @psalm-pure |
||
1406 | * @psalm-assert !lowercase-string $value |
||
1407 | * |
||
1408 | * @param string $value |
||
1409 | * @param string $message |
||
1410 | * |
||
1411 | * @throws InvalidArgumentException |
||
1412 | */ |
||
1413 | 16 | View Code Duplication | public static function upper($value, $message = '') |
1427 | |||
1428 | /** |
||
1429 | * @psalm-pure |
||
1430 | * |
||
1431 | * @param string $value |
||
1432 | * @param int $length |
||
1433 | * @param string $message |
||
1434 | * |
||
1435 | * @throws InvalidArgumentException |
||
1436 | */ |
||
1437 | 36 | public static function length($value, $length, $message = '') |
|
1447 | |||
1448 | /** |
||
1449 | * Inclusive min. |
||
1450 | * |
||
1451 | * @psalm-pure |
||
1452 | * |
||
1453 | * @param string $value |
||
1454 | * @param int|float $min |
||
1455 | * @param string $message |
||
1456 | * |
||
1457 | * @throws InvalidArgumentException |
||
1458 | */ |
||
1459 | 36 | public static function minLength($value, $min, $message = '') |
|
1469 | |||
1470 | /** |
||
1471 | * Inclusive max. |
||
1472 | * |
||
1473 | * @psalm-pure |
||
1474 | * |
||
1475 | * @param string $value |
||
1476 | * @param int|float $max |
||
1477 | * @param string $message |
||
1478 | * |
||
1479 | * @throws InvalidArgumentException |
||
1480 | */ |
||
1481 | 36 | public static function maxLength($value, $max, $message = '') |
|
1491 | |||
1492 | /** |
||
1493 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1494 | * |
||
1495 | * @psalm-pure |
||
1496 | * |
||
1497 | * @param string $value |
||
1498 | * @param int|float $min |
||
1499 | * @param int|float $max |
||
1500 | * @param string $message |
||
1501 | * |
||
1502 | * @throws InvalidArgumentException |
||
1503 | */ |
||
1504 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
1517 | |||
1518 | /** |
||
1519 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1520 | * |
||
1521 | * @param mixed $value |
||
1522 | * @param string $message |
||
1523 | * |
||
1524 | * @throws InvalidArgumentException |
||
1525 | */ |
||
1526 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
1537 | |||
1538 | /** |
||
1539 | * @param mixed $value |
||
1540 | * @param string $message |
||
1541 | * |
||
1542 | * @throws InvalidArgumentException |
||
1543 | */ |
||
1544 | 12 | View Code Duplication | public static function file($value, $message = '') |
1555 | |||
1556 | /** |
||
1557 | * @param mixed $value |
||
1558 | * @param string $message |
||
1559 | * |
||
1560 | * @throws InvalidArgumentException |
||
1561 | */ |
||
1562 | 12 | View Code Duplication | public static function directory($value, $message = '') |
1573 | |||
1574 | /** |
||
1575 | * @param string $value |
||
1576 | * @param string $message |
||
1577 | * |
||
1578 | * @throws InvalidArgumentException |
||
1579 | */ |
||
1580 | public static function readable($value, $message = '') |
||
1589 | |||
1590 | /** |
||
1591 | * @param string $value |
||
1592 | * @param string $message |
||
1593 | * |
||
1594 | * @throws InvalidArgumentException |
||
1595 | */ |
||
1596 | public static function writable($value, $message = '') |
||
1605 | |||
1606 | /** |
||
1607 | * @param mixed $value |
||
1608 | * @param string $message |
||
1609 | * |
||
1610 | * @throws InvalidArgumentException |
||
1611 | */ |
||
1612 | 8 | public static function classExists($value, $message = '') |
|
1621 | |||
1622 | /** |
||
1623 | * @psalm-pure |
||
1624 | * @psalm-template ExpectedType of object |
||
1625 | * @psalm-param class-string<ExpectedType> $class |
||
1626 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
1627 | * |
||
1628 | * @param mixed $value |
||
1629 | * @param string|object $class |
||
1630 | * @param string $message |
||
1631 | * |
||
1632 | * @throws InvalidArgumentException |
||
1633 | */ |
||
1634 | 8 | public static function subclassOf($value, $class, $message = '') |
|
1644 | |||
1645 | /** |
||
1646 | * @param mixed $value |
||
1647 | * @param string $message |
||
1648 | * |
||
1649 | * @throws InvalidArgumentException |
||
1650 | */ |
||
1651 | 8 | public static function interfaceExists($value, $message = '') |
|
1660 | |||
1661 | /** |
||
1662 | * @psalm-pure |
||
1663 | * @psalm-template ExpectedType of object |
||
1664 | * @psalm-param class-string<ExpectedType> $interface |
||
1665 | * @psalm-assert class-string<ExpectedType> $value |
||
1666 | * |
||
1667 | * @param mixed $value |
||
1668 | * @param mixed $interface |
||
1669 | * @param string $message |
||
1670 | * |
||
1671 | * @throws InvalidArgumentException |
||
1672 | */ |
||
1673 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
1683 | |||
1684 | /** |
||
1685 | * @psalm-pure |
||
1686 | * @psalm-param class-string|object $classOrObject |
||
1687 | * |
||
1688 | * @param string|object $classOrObject |
||
1689 | * @param mixed $property |
||
1690 | * @param string $message |
||
1691 | * |
||
1692 | * @throws InvalidArgumentException |
||
1693 | */ |
||
1694 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1703 | |||
1704 | /** |
||
1705 | * @psalm-pure |
||
1706 | * @psalm-param class-string|object $classOrObject |
||
1707 | * |
||
1708 | * @param string|object $classOrObject |
||
1709 | * @param mixed $property |
||
1710 | * @param string $message |
||
1711 | * |
||
1712 | * @throws InvalidArgumentException |
||
1713 | */ |
||
1714 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
1723 | |||
1724 | /** |
||
1725 | * @psalm-pure |
||
1726 | * @psalm-param class-string|object $classOrObject |
||
1727 | * |
||
1728 | * @param string|object $classOrObject |
||
1729 | * @param mixed $method |
||
1730 | * @param string $message |
||
1731 | * |
||
1732 | * @throws InvalidArgumentException |
||
1733 | */ |
||
1734 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1743 | |||
1744 | /** |
||
1745 | * @psalm-pure |
||
1746 | * @psalm-param class-string|object $classOrObject |
||
1747 | * |
||
1748 | * @param string|object $classOrObject |
||
1749 | * @param mixed $method |
||
1750 | * @param string $message |
||
1751 | * |
||
1752 | * @throws InvalidArgumentException |
||
1753 | */ |
||
1754 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
1763 | |||
1764 | /** |
||
1765 | * @psalm-pure |
||
1766 | * |
||
1767 | * @param array $array |
||
1768 | * @param string|int $key |
||
1769 | * @param string $message |
||
1770 | * |
||
1771 | * @throws InvalidArgumentException |
||
1772 | */ |
||
1773 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
1782 | |||
1783 | /** |
||
1784 | * @psalm-pure |
||
1785 | * |
||
1786 | * @param array $array |
||
1787 | * @param string|int $key |
||
1788 | * @param string $message |
||
1789 | * |
||
1790 | * @throws InvalidArgumentException |
||
1791 | */ |
||
1792 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1801 | |||
1802 | /** |
||
1803 | * Checks if a value is a valid array key (int or string). |
||
1804 | * |
||
1805 | * @psalm-pure |
||
1806 | * @psalm-assert array-key $value |
||
1807 | * |
||
1808 | * @param mixed $value |
||
1809 | * @param string $message |
||
1810 | * |
||
1811 | * @throws InvalidArgumentException |
||
1812 | */ |
||
1813 | 28 | public static function validArrayKey($value, $message = '') |
|
1822 | |||
1823 | /** |
||
1824 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1825 | * |
||
1826 | * @param Countable|array $array |
||
1827 | * @param int $number |
||
1828 | * @param string $message |
||
1829 | * |
||
1830 | * @throws InvalidArgumentException |
||
1831 | */ |
||
1832 | 8 | public static function count($array, $number, $message = '') |
|
1840 | |||
1841 | /** |
||
1842 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1843 | * |
||
1844 | * @param Countable|array $array |
||
1845 | * @param int|float $min |
||
1846 | * @param string $message |
||
1847 | * |
||
1848 | * @throws InvalidArgumentException |
||
1849 | */ |
||
1850 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1860 | |||
1861 | /** |
||
1862 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1863 | * |
||
1864 | * @param Countable|array $array |
||
1865 | * @param int|float $max |
||
1866 | * @param string $message |
||
1867 | * |
||
1868 | * @throws InvalidArgumentException |
||
1869 | */ |
||
1870 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1880 | |||
1881 | /** |
||
1882 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1883 | * |
||
1884 | * @param Countable|array $array |
||
1885 | * @param int|float $min |
||
1886 | * @param int|float $max |
||
1887 | * @param string $message |
||
1888 | * |
||
1889 | * @throws InvalidArgumentException |
||
1890 | */ |
||
1891 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
1904 | |||
1905 | /** |
||
1906 | * @psalm-pure |
||
1907 | * @psalm-assert list $array |
||
1908 | * |
||
1909 | * @param mixed $array |
||
1910 | * @param string $message |
||
1911 | * |
||
1912 | * @throws InvalidArgumentException |
||
1913 | */ |
||
1914 | 80 | View Code Duplication | public static function isList($array, $message = '') |
1922 | |||
1923 | /** |
||
1924 | * @psalm-pure |
||
1925 | * @psalm-assert list $array |
||
1926 | * @psalm-assert !empty $array |
||
1927 | * |
||
1928 | * @param mixed $array |
||
1929 | * @param string $message |
||
1930 | * |
||
1931 | * @throws InvalidArgumentException |
||
1932 | */ |
||
1933 | 40 | public static function isNonEmptyList($array, $message = '') |
|
1938 | |||
1939 | /** |
||
1940 | * @psalm-pure |
||
1941 | * @psalm-template T |
||
1942 | * @psalm-param mixed|array<T> $array |
||
1943 | * @psalm-assert array<string, T> $array |
||
1944 | * |
||
1945 | * @param mixed $array |
||
1946 | * @param string $message |
||
1947 | * |
||
1948 | * @throws InvalidArgumentException |
||
1949 | */ |
||
1950 | 32 | public static function isMap($array, $message = '') |
|
1961 | |||
1962 | /** |
||
1963 | * @psalm-pure |
||
1964 | * @psalm-template T |
||
1965 | * @psalm-param mixed|array<T> $array |
||
1966 | * @psalm-assert array<string, T> $array |
||
1967 | * @psalm-assert !empty $array |
||
1968 | * |
||
1969 | * @param mixed $array |
||
1970 | * @param string $message |
||
1971 | * |
||
1972 | * @throws InvalidArgumentException |
||
1973 | */ |
||
1974 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
1979 | |||
1980 | /** |
||
1981 | * @psalm-pure |
||
1982 | * |
||
1983 | * @param string $value |
||
1984 | * @param string $message |
||
1985 | * |
||
1986 | * @throws InvalidArgumentException |
||
1987 | */ |
||
1988 | 56 | public static function uuid($value, $message = '') |
|
2005 | |||
2006 | /** |
||
2007 | * @psalm-param class-string<Throwable> |
||
2008 | * |
||
2009 | * @param Closure $expression |
||
2010 | * @param string $class |
||
2011 | * @param string $message |
||
2012 | * |
||
2013 | * @throws InvalidArgumentException |
||
2014 | */ |
||
2015 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
2041 | |||
2042 | /** |
||
2043 | * @throws BadMethodCallException |
||
2044 | */ |
||
2045 | 1561 | public static function __callStatic($name, $arguments) |
|
2073 | |||
2074 | /** |
||
2075 | * @param mixed $value |
||
2076 | * |
||
2077 | * @return string |
||
2078 | */ |
||
2079 | 715 | protected static function valueToString($value) |
|
2119 | |||
2120 | /** |
||
2121 | * @param mixed $value |
||
2122 | * |
||
2123 | * @return string |
||
2124 | */ |
||
2125 | 251 | protected static function typeToString($value) |
|
2129 | |||
2130 | 168 | protected static function strlen($value) |
|
2142 | |||
2143 | /** |
||
2144 | * @param string $message |
||
2145 | * |
||
2146 | * @throws InvalidArgumentException |
||
2147 | * |
||
2148 | * @psalm-pure this method is not supposed to perform side-effects |
||
2149 | */ |
||
2150 | 1029 | protected static function reportInvalidArgument($message) |
|
2154 | |||
2155 | private function __construct() |
||
2158 | } |
||
2159 |