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 |
||
217 | class Assert |
||
|
|||
218 | { |
||
219 | /** |
||
220 | * @psalm-pure |
||
221 | * @psalm-assert string $value |
||
222 | * |
||
223 | * @param mixed $value |
||
224 | * @param string $message |
||
225 | * |
||
226 | * @throws InvalidArgumentException |
||
227 | */ |
||
228 | 241 | public static function string($value, $message = '') |
|
237 | |||
238 | /** |
||
239 | * @psalm-pure |
||
240 | * @psalm-assert string $value |
||
241 | * @psalm-assert !empty $value |
||
242 | * |
||
243 | * @param mixed $value |
||
244 | * @param string $message |
||
245 | * |
||
246 | * @throws InvalidArgumentException |
||
247 | */ |
||
248 | 16 | public static function stringNotEmpty($value, $message = '') |
|
253 | |||
254 | /** |
||
255 | * @psalm-pure |
||
256 | * @psalm-assert int $value |
||
257 | * |
||
258 | * @param mixed $value |
||
259 | * @param string $message |
||
260 | * |
||
261 | * @throws InvalidArgumentException |
||
262 | */ |
||
263 | 17 | public static function integer($value, $message = '') |
|
272 | |||
273 | /** |
||
274 | * @psalm-pure |
||
275 | * @psalm-assert numeric $value |
||
276 | * |
||
277 | * @param mixed $value |
||
278 | * @param string $message |
||
279 | * |
||
280 | * @throws InvalidArgumentException |
||
281 | */ |
||
282 | 16 | public static function integerish($value, $message = '') |
|
291 | |||
292 | /** |
||
293 | * @psalm-pure |
||
294 | * @psalm-assert float $value |
||
295 | * |
||
296 | * @param mixed $value |
||
297 | * @param string $message |
||
298 | * |
||
299 | * @throws InvalidArgumentException |
||
300 | */ |
||
301 | 16 | public static function float($value, $message = '') |
|
310 | |||
311 | /** |
||
312 | * @psalm-pure |
||
313 | * @psalm-assert numeric $value |
||
314 | * |
||
315 | * @param mixed $value |
||
316 | * @param string $message |
||
317 | * |
||
318 | * @throws InvalidArgumentException |
||
319 | */ |
||
320 | 20 | public static function numeric($value, $message = '') |
|
329 | |||
330 | /** |
||
331 | * @psalm-pure |
||
332 | * @psalm-assert int $value |
||
333 | * |
||
334 | * @param mixed $value |
||
335 | * @param string $message |
||
336 | * |
||
337 | * @throws InvalidArgumentException |
||
338 | */ |
||
339 | 24 | public static function natural($value, $message = '') |
|
348 | |||
349 | /** |
||
350 | * @psalm-pure |
||
351 | * @psalm-assert bool $value |
||
352 | * |
||
353 | * @param mixed $value |
||
354 | * @param string $message |
||
355 | * |
||
356 | * @throws InvalidArgumentException |
||
357 | */ |
||
358 | 16 | public static function boolean($value, $message = '') |
|
367 | |||
368 | /** |
||
369 | * @psalm-pure |
||
370 | * @psalm-assert scalar $value |
||
371 | * |
||
372 | * @param mixed $value |
||
373 | * @param string $message |
||
374 | * |
||
375 | * @throws InvalidArgumentException |
||
376 | */ |
||
377 | 23 | public static function scalar($value, $message = '') |
|
386 | |||
387 | /** |
||
388 | * @psalm-pure |
||
389 | * @psalm-assert object $value |
||
390 | * |
||
391 | * @param mixed $value |
||
392 | * @param string $message |
||
393 | * |
||
394 | * @throws InvalidArgumentException |
||
395 | */ |
||
396 | 23 | public static function object($value, $message = '') |
|
405 | |||
406 | /** |
||
407 | * @psalm-pure |
||
408 | * @psalm-assert resource $value |
||
409 | * |
||
410 | * @param mixed $value |
||
411 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
412 | * @param string $message |
||
413 | * |
||
414 | * @throws InvalidArgumentException |
||
415 | */ |
||
416 | 16 | public static function resource($value, $type = null, $message = '') |
|
433 | |||
434 | /** |
||
435 | * @psalm-pure |
||
436 | * @psalm-assert callable $value |
||
437 | * |
||
438 | * @param mixed $value |
||
439 | * @param string $message |
||
440 | * |
||
441 | * @throws InvalidArgumentException |
||
442 | */ |
||
443 | 20 | public static function isCallable($value, $message = '') |
|
452 | |||
453 | /** |
||
454 | * @psalm-pure |
||
455 | * @psalm-assert array $value |
||
456 | * |
||
457 | * @param mixed $value |
||
458 | * @param string $message |
||
459 | * |
||
460 | * @throws InvalidArgumentException |
||
461 | */ |
||
462 | 20 | public static function isArray($value, $message = '') |
|
471 | |||
472 | /** |
||
473 | * @psalm-pure |
||
474 | * @psalm-assert iterable $value |
||
475 | * |
||
476 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
477 | * |
||
478 | * @param mixed $value |
||
479 | * @param string $message |
||
480 | * |
||
481 | * @throws InvalidArgumentException |
||
482 | */ |
||
483 | 20 | public static function isTraversable($value, $message = '') |
|
500 | |||
501 | /** |
||
502 | * @psalm-pure |
||
503 | * @psalm-assert array|ArrayAccess $value |
||
504 | * |
||
505 | * @param mixed $value |
||
506 | * @param string $message |
||
507 | * |
||
508 | * @throws InvalidArgumentException |
||
509 | */ |
||
510 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
519 | |||
520 | /** |
||
521 | * @psalm-pure |
||
522 | * @psalm-assert countable $value |
||
523 | * |
||
524 | * @param mixed $value |
||
525 | * @param string $message |
||
526 | * |
||
527 | * @throws InvalidArgumentException |
||
528 | */ |
||
529 | 28 | public static function isCountable($value, $message = '') |
|
543 | |||
544 | /** |
||
545 | * @psalm-pure |
||
546 | * @psalm-assert iterable $value |
||
547 | * |
||
548 | * @param mixed $value |
||
549 | * @param string $message |
||
550 | * |
||
551 | * @throws InvalidArgumentException |
||
552 | */ |
||
553 | 1044 | View Code Duplication | public static function isIterable($value, $message = '') |
562 | |||
563 | /** |
||
564 | * @psalm-pure |
||
565 | * @psalm-template ExpectedType of object |
||
566 | * @psalm-param class-string<ExpectedType> $class |
||
567 | * @psalm-assert ExpectedType $value |
||
568 | * |
||
569 | * @param mixed $value |
||
570 | * @param string|object $class |
||
571 | * @param string $message |
||
572 | * |
||
573 | * @throws InvalidArgumentException |
||
574 | */ |
||
575 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
585 | |||
586 | /** |
||
587 | * @psalm-pure |
||
588 | * @psalm-template ExpectedType of object |
||
589 | * @psalm-param class-string<ExpectedType> $class |
||
590 | * @psalm-assert !ExpectedType $value |
||
591 | * |
||
592 | * @param mixed $value |
||
593 | * @param string|object $class |
||
594 | * @param string $message |
||
595 | * |
||
596 | * @throws InvalidArgumentException |
||
597 | */ |
||
598 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
608 | |||
609 | /** |
||
610 | * @psalm-pure |
||
611 | * @psalm-param array<class-string> $classes |
||
612 | * |
||
613 | * @param mixed $value |
||
614 | * @param array<object|string> $classes |
||
615 | * @param string $message |
||
616 | * |
||
617 | * @throws InvalidArgumentException |
||
618 | */ |
||
619 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
633 | |||
634 | /** |
||
635 | * @psalm-pure |
||
636 | * @psalm-template ExpectedType of object |
||
637 | * @psalm-param class-string<ExpectedType> $class |
||
638 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
||
639 | * |
||
640 | * @param object|string $value |
||
641 | * @param string $class |
||
642 | * @param string $message |
||
643 | * |
||
644 | * @throws InvalidArgumentException |
||
645 | */ |
||
646 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
658 | |||
659 | /** |
||
660 | * @psalm-pure |
||
661 | * @psalm-template UnexpectedType of object |
||
662 | * @psalm-param class-string<UnexpectedType> $class |
||
663 | * @psalm-assert !UnexpectedType $value |
||
664 | * @psalm-assert !class-string<UnexpectedType> $value |
||
665 | * |
||
666 | * @param object|string $value |
||
667 | * @param string $class |
||
668 | * @param string $message |
||
669 | * |
||
670 | * @throws InvalidArgumentException |
||
671 | */ |
||
672 | 20 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
684 | |||
685 | /** |
||
686 | * @psalm-pure |
||
687 | * @psalm-param array<class-string> $classes |
||
688 | * |
||
689 | * @param object|string $value |
||
690 | * @param string[] $classes |
||
691 | * @param string $message |
||
692 | * |
||
693 | * @throws InvalidArgumentException |
||
694 | */ |
||
695 | 24 | public static function isAnyOf($value, array $classes, $message = '') |
|
711 | |||
712 | /** |
||
713 | * @psalm-pure |
||
714 | * @psalm-assert empty $value |
||
715 | * |
||
716 | * @param mixed $value |
||
717 | * @param string $message |
||
718 | * |
||
719 | * @throws InvalidArgumentException |
||
720 | */ |
||
721 | 23 | public static function isEmpty($value, $message = '') |
|
730 | |||
731 | /** |
||
732 | * @psalm-pure |
||
733 | * @psalm-assert !empty $value |
||
734 | * |
||
735 | * @param mixed $value |
||
736 | * @param string $message |
||
737 | * |
||
738 | * @throws InvalidArgumentException |
||
739 | */ |
||
740 | 55 | public static function notEmpty($value, $message = '') |
|
749 | |||
750 | /** |
||
751 | * @psalm-pure |
||
752 | * @psalm-assert null $value |
||
753 | * |
||
754 | * @param mixed $value |
||
755 | * @param string $message |
||
756 | * |
||
757 | * @throws InvalidArgumentException |
||
758 | */ |
||
759 | 11 | public static function null($value, $message = '') |
|
768 | |||
769 | /** |
||
770 | * @psalm-pure |
||
771 | * @psalm-assert !null $value |
||
772 | * |
||
773 | * @param mixed $value |
||
774 | * @param string $message |
||
775 | * |
||
776 | * @throws InvalidArgumentException |
||
777 | */ |
||
778 | 11 | public static function notNull($value, $message = '') |
|
786 | |||
787 | /** |
||
788 | * @psalm-pure |
||
789 | * @psalm-assert true $value |
||
790 | * |
||
791 | * @param mixed $value |
||
792 | * @param string $message |
||
793 | * |
||
794 | * @throws InvalidArgumentException |
||
795 | */ |
||
796 | 15 | public static function true($value, $message = '') |
|
805 | |||
806 | /** |
||
807 | * @psalm-pure |
||
808 | * @psalm-assert false $value |
||
809 | * |
||
810 | * @param mixed $value |
||
811 | * @param string $message |
||
812 | * |
||
813 | * @throws InvalidArgumentException |
||
814 | */ |
||
815 | 19 | public static function false($value, $message = '') |
|
824 | |||
825 | /** |
||
826 | * @psalm-assert !false $value |
||
827 | * |
||
828 | * @param mixed $value |
||
829 | * @param string $message |
||
830 | * |
||
831 | * @throws InvalidArgumentException |
||
832 | */ |
||
833 | 19 | public static function notFalse($value, $message = '') |
|
841 | |||
842 | /** |
||
843 | * @param mixed $value |
||
844 | * @param string $message |
||
845 | * |
||
846 | * @throws InvalidArgumentException |
||
847 | */ |
||
848 | 51 | View Code Duplication | public static function ip($value, $message = '') |
857 | |||
858 | /** |
||
859 | * @param mixed $value |
||
860 | * @param string $message |
||
861 | * |
||
862 | * @throws InvalidArgumentException |
||
863 | */ |
||
864 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
873 | |||
874 | /** |
||
875 | * @param mixed $value |
||
876 | * @param string $message |
||
877 | * |
||
878 | * @throws InvalidArgumentException |
||
879 | */ |
||
880 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
889 | |||
890 | /** |
||
891 | * @param mixed $value |
||
892 | * @param string $message |
||
893 | * |
||
894 | * @throws InvalidArgumentException |
||
895 | */ |
||
896 | 20 | View Code Duplication | public static function email($value, $message = '') |
905 | |||
906 | /** |
||
907 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
908 | * |
||
909 | * @param array $values |
||
910 | * @param string $message |
||
911 | * |
||
912 | * @throws InvalidArgumentException |
||
913 | */ |
||
914 | 12 | public static function uniqueValues(array $values, $message = '') |
|
929 | |||
930 | /** |
||
931 | * @param mixed $value |
||
932 | * @param mixed $expect |
||
933 | * @param string $message |
||
934 | * |
||
935 | * @throws InvalidArgumentException |
||
936 | */ |
||
937 | 33 | public static function eq($value, $expect, $message = '') |
|
947 | |||
948 | /** |
||
949 | * @param mixed $value |
||
950 | * @param mixed $expect |
||
951 | * @param string $message |
||
952 | * |
||
953 | * @throws InvalidArgumentException |
||
954 | */ |
||
955 | 28 | public static function notEq($value, $expect, $message = '') |
|
964 | |||
965 | /** |
||
966 | * @psalm-pure |
||
967 | * |
||
968 | * @param mixed $value |
||
969 | * @param mixed $expect |
||
970 | * @param string $message |
||
971 | * |
||
972 | * @throws InvalidArgumentException |
||
973 | */ |
||
974 | 16 | public static function same($value, $expect, $message = '') |
|
984 | |||
985 | /** |
||
986 | * @psalm-pure |
||
987 | * |
||
988 | * @param mixed $value |
||
989 | * @param mixed $expect |
||
990 | * @param string $message |
||
991 | * |
||
992 | * @throws InvalidArgumentException |
||
993 | */ |
||
994 | 16 | public static function notSame($value, $expect, $message = '') |
|
1003 | |||
1004 | /** |
||
1005 | * @psalm-pure |
||
1006 | * |
||
1007 | * @param mixed $value |
||
1008 | * @param mixed $limit |
||
1009 | * @param string $message |
||
1010 | * |
||
1011 | * @throws InvalidArgumentException |
||
1012 | */ |
||
1013 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
1023 | |||
1024 | /** |
||
1025 | * @psalm-pure |
||
1026 | * |
||
1027 | * @param mixed $value |
||
1028 | * @param mixed $limit |
||
1029 | * @param string $message |
||
1030 | * |
||
1031 | * @throws InvalidArgumentException |
||
1032 | */ |
||
1033 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
1043 | |||
1044 | /** |
||
1045 | * @psalm-pure |
||
1046 | * |
||
1047 | * @param mixed $value |
||
1048 | * @param mixed $limit |
||
1049 | * @param string $message |
||
1050 | * |
||
1051 | * @throws InvalidArgumentException |
||
1052 | */ |
||
1053 | 9 | public static function lessThan($value, $limit, $message = '') |
|
1063 | |||
1064 | /** |
||
1065 | * @psalm-pure |
||
1066 | * |
||
1067 | * @param mixed $value |
||
1068 | * @param mixed $limit |
||
1069 | * @param string $message |
||
1070 | * |
||
1071 | * @throws InvalidArgumentException |
||
1072 | */ |
||
1073 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
1083 | |||
1084 | /** |
||
1085 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
1086 | * |
||
1087 | * @psalm-pure |
||
1088 | * |
||
1089 | * @param mixed $value |
||
1090 | * @param mixed $min |
||
1091 | * @param mixed $max |
||
1092 | * @param string $message |
||
1093 | * |
||
1094 | * @throws InvalidArgumentException |
||
1095 | */ |
||
1096 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
1107 | |||
1108 | /** |
||
1109 | * A more human-readable alias of Assert::inArray(). |
||
1110 | * |
||
1111 | * @psalm-pure |
||
1112 | * |
||
1113 | * @param mixed $value |
||
1114 | * @param array $values |
||
1115 | * @param string $message |
||
1116 | * |
||
1117 | * @throws InvalidArgumentException |
||
1118 | */ |
||
1119 | 8 | public static function oneOf($value, array $values, $message = '') |
|
1123 | |||
1124 | /** |
||
1125 | * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion. |
||
1126 | * |
||
1127 | * @psalm-pure |
||
1128 | * |
||
1129 | * @param mixed $value |
||
1130 | * @param array $values |
||
1131 | * @param string $message |
||
1132 | * |
||
1133 | * @throws InvalidArgumentException |
||
1134 | */ |
||
1135 | 16 | public static function inArray($value, array $values, $message = '') |
|
1145 | |||
1146 | /** |
||
1147 | * @psalm-pure |
||
1148 | * |
||
1149 | * @param string $value |
||
1150 | * @param string $subString |
||
1151 | * @param string $message |
||
1152 | * |
||
1153 | * @throws InvalidArgumentException |
||
1154 | */ |
||
1155 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
1165 | |||
1166 | /** |
||
1167 | * @psalm-pure |
||
1168 | * |
||
1169 | * @param string $value |
||
1170 | * @param string $subString |
||
1171 | * @param string $message |
||
1172 | * |
||
1173 | * @throws InvalidArgumentException |
||
1174 | */ |
||
1175 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1185 | |||
1186 | /** |
||
1187 | * @psalm-pure |
||
1188 | * |
||
1189 | * @param string $value |
||
1190 | * @param string $message |
||
1191 | * |
||
1192 | * @throws InvalidArgumentException |
||
1193 | */ |
||
1194 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
1203 | |||
1204 | /** |
||
1205 | * @psalm-pure |
||
1206 | * |
||
1207 | * @param string $value |
||
1208 | * @param string $prefix |
||
1209 | * @param string $message |
||
1210 | * |
||
1211 | * @throws InvalidArgumentException |
||
1212 | */ |
||
1213 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
1223 | |||
1224 | /** |
||
1225 | * @psalm-pure |
||
1226 | * |
||
1227 | * @param string $value |
||
1228 | * @param string $prefix |
||
1229 | * @param string $message |
||
1230 | * |
||
1231 | * @throws InvalidArgumentException |
||
1232 | */ |
||
1233 | 48 | View Code Duplication | public static function notStartsWith($value, $prefix, $message = '') |
1243 | |||
1244 | /** |
||
1245 | * @psalm-pure |
||
1246 | * |
||
1247 | * @param mixed $value |
||
1248 | * @param string $message |
||
1249 | * |
||
1250 | * @throws InvalidArgumentException |
||
1251 | */ |
||
1252 | 35 | public static function startsWithLetter($value, $message = '') |
|
1272 | |||
1273 | /** |
||
1274 | * @psalm-pure |
||
1275 | * |
||
1276 | * @param string $value |
||
1277 | * @param string $suffix |
||
1278 | * @param string $message |
||
1279 | * |
||
1280 | * @throws InvalidArgumentException |
||
1281 | */ |
||
1282 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
1292 | |||
1293 | /** |
||
1294 | * @psalm-pure |
||
1295 | * |
||
1296 | * @param string $value |
||
1297 | * @param string $suffix |
||
1298 | * @param string $message |
||
1299 | * |
||
1300 | * @throws InvalidArgumentException |
||
1301 | */ |
||
1302 | 48 | View Code Duplication | public static function notEndsWith($value, $suffix, $message = '') |
1312 | |||
1313 | /** |
||
1314 | * @psalm-pure |
||
1315 | * |
||
1316 | * @param string $value |
||
1317 | * @param string $pattern |
||
1318 | * @param string $message |
||
1319 | * |
||
1320 | * @throws InvalidArgumentException |
||
1321 | */ |
||
1322 | 12 | public static function regex($value, $pattern, $message = '') |
|
1331 | |||
1332 | /** |
||
1333 | * @psalm-pure |
||
1334 | * |
||
1335 | * @param string $value |
||
1336 | * @param string $pattern |
||
1337 | * @param string $message |
||
1338 | * |
||
1339 | * @throws InvalidArgumentException |
||
1340 | */ |
||
1341 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
1352 | |||
1353 | /** |
||
1354 | * @psalm-pure |
||
1355 | * |
||
1356 | * @param mixed $value |
||
1357 | * @param string $message |
||
1358 | * |
||
1359 | * @throws InvalidArgumentException |
||
1360 | */ |
||
1361 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
1372 | |||
1373 | /** |
||
1374 | * @psalm-pure |
||
1375 | * |
||
1376 | * @param mixed $value |
||
1377 | * @param string $message |
||
1378 | * |
||
1379 | * @throws InvalidArgumentException |
||
1380 | */ |
||
1381 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
1397 | |||
1398 | /** |
||
1399 | * @psalm-pure |
||
1400 | * |
||
1401 | * @param string $value |
||
1402 | * @param string $message |
||
1403 | * |
||
1404 | * @throws InvalidArgumentException |
||
1405 | */ |
||
1406 | 12 | View Code Duplication | public static function digits($value, $message = '') |
1420 | |||
1421 | /** |
||
1422 | * @psalm-pure |
||
1423 | * |
||
1424 | * @param string $value |
||
1425 | * @param string $message |
||
1426 | * |
||
1427 | * @throws InvalidArgumentException |
||
1428 | */ |
||
1429 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
1443 | |||
1444 | /** |
||
1445 | * @psalm-pure |
||
1446 | * @psalm-assert lowercase-string $value |
||
1447 | * |
||
1448 | * @param string $value |
||
1449 | * @param string $message |
||
1450 | * |
||
1451 | * @throws InvalidArgumentException |
||
1452 | */ |
||
1453 | 16 | View Code Duplication | public static function lower($value, $message = '') |
1467 | |||
1468 | /** |
||
1469 | * @psalm-pure |
||
1470 | * @psalm-assert !lowercase-string $value |
||
1471 | * |
||
1472 | * @param string $value |
||
1473 | * @param string $message |
||
1474 | * |
||
1475 | * @throws InvalidArgumentException |
||
1476 | */ |
||
1477 | 16 | View Code Duplication | public static function upper($value, $message = '') |
1491 | |||
1492 | /** |
||
1493 | * @psalm-pure |
||
1494 | * |
||
1495 | * @param string $value |
||
1496 | * @param int $length |
||
1497 | * @param string $message |
||
1498 | * |
||
1499 | * @throws InvalidArgumentException |
||
1500 | */ |
||
1501 | 36 | public static function length($value, $length, $message = '') |
|
1511 | |||
1512 | /** |
||
1513 | * Inclusive min. |
||
1514 | * |
||
1515 | * @psalm-pure |
||
1516 | * |
||
1517 | * @param string $value |
||
1518 | * @param int|float $min |
||
1519 | * @param string $message |
||
1520 | * |
||
1521 | * @throws InvalidArgumentException |
||
1522 | */ |
||
1523 | 36 | public static function minLength($value, $min, $message = '') |
|
1533 | |||
1534 | /** |
||
1535 | * Inclusive max. |
||
1536 | * |
||
1537 | * @psalm-pure |
||
1538 | * |
||
1539 | * @param string $value |
||
1540 | * @param int|float $max |
||
1541 | * @param string $message |
||
1542 | * |
||
1543 | * @throws InvalidArgumentException |
||
1544 | */ |
||
1545 | 36 | public static function maxLength($value, $max, $message = '') |
|
1555 | |||
1556 | /** |
||
1557 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1558 | * |
||
1559 | * @psalm-pure |
||
1560 | * |
||
1561 | * @param string $value |
||
1562 | * @param int|float $min |
||
1563 | * @param int|float $max |
||
1564 | * @param string $message |
||
1565 | * |
||
1566 | * @throws InvalidArgumentException |
||
1567 | */ |
||
1568 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
1581 | |||
1582 | /** |
||
1583 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1584 | * |
||
1585 | * @param mixed $value |
||
1586 | * @param string $message |
||
1587 | * |
||
1588 | * @throws InvalidArgumentException |
||
1589 | */ |
||
1590 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
1601 | |||
1602 | /** |
||
1603 | * @param mixed $value |
||
1604 | * @param string $message |
||
1605 | * |
||
1606 | * @throws InvalidArgumentException |
||
1607 | */ |
||
1608 | 12 | View Code Duplication | public static function file($value, $message = '') |
1619 | |||
1620 | /** |
||
1621 | * @param mixed $value |
||
1622 | * @param string $message |
||
1623 | * |
||
1624 | * @throws InvalidArgumentException |
||
1625 | */ |
||
1626 | 12 | View Code Duplication | public static function directory($value, $message = '') |
1637 | |||
1638 | /** |
||
1639 | * @param string $value |
||
1640 | * @param string $message |
||
1641 | * |
||
1642 | * @throws InvalidArgumentException |
||
1643 | */ |
||
1644 | public static function readable($value, $message = '') |
||
1653 | |||
1654 | /** |
||
1655 | * @param string $value |
||
1656 | * @param string $message |
||
1657 | * |
||
1658 | * @throws InvalidArgumentException |
||
1659 | */ |
||
1660 | public static function writable($value, $message = '') |
||
1669 | |||
1670 | /** |
||
1671 | * @param mixed $value |
||
1672 | * @param string $message |
||
1673 | * |
||
1674 | * @throws InvalidArgumentException |
||
1675 | */ |
||
1676 | 8 | public static function classExists($value, $message = '') |
|
1685 | |||
1686 | /** |
||
1687 | * @psalm-pure |
||
1688 | * @psalm-template ExpectedType of object |
||
1689 | * @psalm-param class-string<ExpectedType> $class |
||
1690 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
1691 | * |
||
1692 | * @param mixed $value |
||
1693 | * @param string|object $class |
||
1694 | * @param string $message |
||
1695 | * |
||
1696 | * @throws InvalidArgumentException |
||
1697 | */ |
||
1698 | 8 | public static function subclassOf($value, $class, $message = '') |
|
1708 | |||
1709 | /** |
||
1710 | * @param mixed $value |
||
1711 | * @param string $message |
||
1712 | * |
||
1713 | * @throws InvalidArgumentException |
||
1714 | */ |
||
1715 | 8 | public static function interfaceExists($value, $message = '') |
|
1724 | |||
1725 | /** |
||
1726 | * @psalm-pure |
||
1727 | * @psalm-template ExpectedType of object |
||
1728 | * @psalm-param class-string<ExpectedType> $interface |
||
1729 | * @psalm-assert class-string<ExpectedType> $value |
||
1730 | * |
||
1731 | * @param mixed $value |
||
1732 | * @param mixed $interface |
||
1733 | * @param string $message |
||
1734 | * |
||
1735 | * @throws InvalidArgumentException |
||
1736 | */ |
||
1737 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
1747 | |||
1748 | /** |
||
1749 | * @psalm-pure |
||
1750 | * @psalm-param class-string|object $classOrObject |
||
1751 | * |
||
1752 | * @param string|object $classOrObject |
||
1753 | * @param mixed $property |
||
1754 | * @param string $message |
||
1755 | * |
||
1756 | * @throws InvalidArgumentException |
||
1757 | */ |
||
1758 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1767 | |||
1768 | /** |
||
1769 | * @psalm-pure |
||
1770 | * @psalm-param class-string|object $classOrObject |
||
1771 | * |
||
1772 | * @param string|object $classOrObject |
||
1773 | * @param mixed $property |
||
1774 | * @param string $message |
||
1775 | * |
||
1776 | * @throws InvalidArgumentException |
||
1777 | */ |
||
1778 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
1787 | |||
1788 | /** |
||
1789 | * @psalm-pure |
||
1790 | * @psalm-param class-string|object $classOrObject |
||
1791 | * |
||
1792 | * @param string|object $classOrObject |
||
1793 | * @param mixed $method |
||
1794 | * @param string $message |
||
1795 | * |
||
1796 | * @throws InvalidArgumentException |
||
1797 | */ |
||
1798 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1807 | |||
1808 | /** |
||
1809 | * @psalm-pure |
||
1810 | * @psalm-param class-string|object $classOrObject |
||
1811 | * |
||
1812 | * @param string|object $classOrObject |
||
1813 | * @param mixed $method |
||
1814 | * @param string $message |
||
1815 | * |
||
1816 | * @throws InvalidArgumentException |
||
1817 | */ |
||
1818 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
1827 | |||
1828 | /** |
||
1829 | * @psalm-pure |
||
1830 | * |
||
1831 | * @param array $array |
||
1832 | * @param string|int $key |
||
1833 | * @param string $message |
||
1834 | * |
||
1835 | * @throws InvalidArgumentException |
||
1836 | */ |
||
1837 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
1846 | |||
1847 | /** |
||
1848 | * @psalm-pure |
||
1849 | * |
||
1850 | * @param array $array |
||
1851 | * @param string|int $key |
||
1852 | * @param string $message |
||
1853 | * |
||
1854 | * @throws InvalidArgumentException |
||
1855 | */ |
||
1856 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1865 | |||
1866 | /** |
||
1867 | * Checks if a value is a valid array key (int or string). |
||
1868 | * |
||
1869 | * @psalm-pure |
||
1870 | * @psalm-assert array-key $value |
||
1871 | * |
||
1872 | * @param mixed $value |
||
1873 | * @param string $message |
||
1874 | * |
||
1875 | * @throws InvalidArgumentException |
||
1876 | */ |
||
1877 | 28 | public static function validArrayKey($value, $message = '') |
|
1886 | |||
1887 | /** |
||
1888 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1889 | * |
||
1890 | * @param Countable|array $array |
||
1891 | * @param int $number |
||
1892 | * @param string $message |
||
1893 | * |
||
1894 | * @throws InvalidArgumentException |
||
1895 | */ |
||
1896 | 8 | public static function count($array, $number, $message = '') |
|
1908 | |||
1909 | /** |
||
1910 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1911 | * |
||
1912 | * @param Countable|array $array |
||
1913 | * @param int|float $min |
||
1914 | * @param string $message |
||
1915 | * |
||
1916 | * @throws InvalidArgumentException |
||
1917 | */ |
||
1918 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1928 | |||
1929 | /** |
||
1930 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1931 | * |
||
1932 | * @param Countable|array $array |
||
1933 | * @param int|float $max |
||
1934 | * @param string $message |
||
1935 | * |
||
1936 | * @throws InvalidArgumentException |
||
1937 | */ |
||
1938 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1948 | |||
1949 | /** |
||
1950 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1951 | * |
||
1952 | * @param Countable|array $array |
||
1953 | * @param int|float $min |
||
1954 | * @param int|float $max |
||
1955 | * @param string $message |
||
1956 | * |
||
1957 | * @throws InvalidArgumentException |
||
1958 | */ |
||
1959 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
1972 | |||
1973 | /** |
||
1974 | * @psalm-pure |
||
1975 | * @psalm-assert list $array |
||
1976 | * |
||
1977 | * @param mixed $array |
||
1978 | * @param string $message |
||
1979 | * |
||
1980 | * @throws InvalidArgumentException |
||
1981 | */ |
||
1982 | 80 | View Code Duplication | public static function isList($array, $message = '') |
1990 | |||
1991 | /** |
||
1992 | * @psalm-pure |
||
1993 | * @psalm-assert list $array |
||
1994 | * @psalm-assert !empty $array |
||
1995 | * |
||
1996 | * @param mixed $array |
||
1997 | * @param string $message |
||
1998 | * |
||
1999 | * @throws InvalidArgumentException |
||
2000 | */ |
||
2001 | 40 | public static function isNonEmptyList($array, $message = '') |
|
2006 | |||
2007 | /** |
||
2008 | * @psalm-pure |
||
2009 | * @psalm-template T |
||
2010 | * @psalm-param mixed|array<T> $array |
||
2011 | * @psalm-assert array<string, T> $array |
||
2012 | * |
||
2013 | * @param mixed $array |
||
2014 | * @param string $message |
||
2015 | * |
||
2016 | * @throws InvalidArgumentException |
||
2017 | */ |
||
2018 | 32 | public static function isMap($array, $message = '') |
|
2029 | |||
2030 | /** |
||
2031 | * @psalm-pure |
||
2032 | * @psalm-template T |
||
2033 | * @psalm-param mixed|array<T> $array |
||
2034 | * @psalm-assert array<string, T> $array |
||
2035 | * @psalm-assert !empty $array |
||
2036 | * |
||
2037 | * @param mixed $array |
||
2038 | * @param string $message |
||
2039 | * |
||
2040 | * @throws InvalidArgumentException |
||
2041 | */ |
||
2042 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
2047 | |||
2048 | /** |
||
2049 | * @psalm-pure |
||
2050 | * |
||
2051 | * @param string $value |
||
2052 | * @param string $message |
||
2053 | * |
||
2054 | * @throws InvalidArgumentException |
||
2055 | */ |
||
2056 | 56 | public static function uuid($value, $message = '') |
|
2073 | |||
2074 | /** |
||
2075 | * @psalm-param class-string<Throwable> |
||
2076 | * |
||
2077 | * @param Closure $expression |
||
2078 | * @param string $class |
||
2079 | * @param string $message |
||
2080 | * |
||
2081 | * @throws InvalidArgumentException |
||
2082 | */ |
||
2083 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
2109 | |||
2110 | /** |
||
2111 | * @throws BadMethodCallException |
||
2112 | */ |
||
2113 | 1642 | public static function __callStatic($name, $arguments) |
|
2141 | |||
2142 | /** |
||
2143 | * @param mixed $value |
||
2144 | * |
||
2145 | * @return string |
||
2146 | */ |
||
2147 | 751 | protected static function valueToString($value) |
|
2187 | |||
2188 | /** |
||
2189 | * @param mixed $value |
||
2190 | * |
||
2191 | * @return string |
||
2192 | */ |
||
2193 | 251 | protected static function typeToString($value) |
|
2197 | |||
2198 | 168 | protected static function strlen($value) |
|
2210 | |||
2211 | /** |
||
2212 | * @param string $message |
||
2213 | * |
||
2214 | * @throws InvalidArgumentException |
||
2215 | * |
||
2216 | * @psalm-pure this method is not supposed to perform side-effects |
||
2217 | */ |
||
2218 | 1065 | protected static function reportInvalidArgument($message) |
|
2222 | |||
2223 | private function __construct() |
||
2226 | } |
||
2227 |