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 |
||
| 197 | class Assert |
||
|
|
|||
| 198 | { |
||
| 199 | /** |
||
| 200 | * @psalm-assert string $value |
||
| 201 | * |
||
| 202 | * @param mixed $value |
||
| 203 | * @param string $message |
||
| 204 | * |
||
| 205 | 350 | * @throws InvalidArgumentException |
|
| 206 | */ |
||
| 207 | 350 | public static function string($value, $message = '') |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @psalm-assert string $value |
||
| 219 | * |
||
| 220 | * @param mixed $value |
||
| 221 | * @param string $message |
||
| 222 | * |
||
| 223 | 16 | * @throws InvalidArgumentException |
|
| 224 | */ |
||
| 225 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @psalm-assert int $value |
||
| 233 | * |
||
| 234 | * @param mixed $value |
||
| 235 | * @param string $message |
||
| 236 | * |
||
| 237 | 17 | * @throws InvalidArgumentException |
|
| 238 | */ |
||
| 239 | 17 | public static function integer($value, $message = '') |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @psalm-assert numeric $value |
||
| 251 | * |
||
| 252 | * @param mixed $value |
||
| 253 | * @param string $message |
||
| 254 | * |
||
| 255 | 16 | * @throws InvalidArgumentException |
|
| 256 | */ |
||
| 257 | 16 | public static function integerish($value, $message = '') |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @psalm-assert float $value |
||
| 269 | * |
||
| 270 | * @param mixed $value |
||
| 271 | * @param string $message |
||
| 272 | * |
||
| 273 | 16 | * @throws InvalidArgumentException |
|
| 274 | */ |
||
| 275 | 16 | public static function float($value, $message = '') |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @psalm-assert numeric $value |
||
| 287 | * |
||
| 288 | * @param mixed $value |
||
| 289 | * @param string $message |
||
| 290 | * |
||
| 291 | 20 | * @throws InvalidArgumentException |
|
| 292 | */ |
||
| 293 | 20 | public static function numeric($value, $message = '') |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @psalm-assert int $value |
||
| 305 | * |
||
| 306 | * @param mixed $value |
||
| 307 | * @param string $message |
||
| 308 | * |
||
| 309 | 24 | * @throws InvalidArgumentException |
|
| 310 | */ |
||
| 311 | 24 | public static function natural($value, $message = '') |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @psalm-assert bool $value |
||
| 323 | * |
||
| 324 | * @param mixed $value |
||
| 325 | * @param string $message |
||
| 326 | * |
||
| 327 | 16 | * @throws InvalidArgumentException |
|
| 328 | */ |
||
| 329 | 16 | public static function boolean($value, $message = '') |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @psalm-assert scalar $value |
||
| 341 | * |
||
| 342 | * @param mixed $value |
||
| 343 | * @param string $message |
||
| 344 | * |
||
| 345 | 23 | * @throws InvalidArgumentException |
|
| 346 | */ |
||
| 347 | 23 | public static function scalar($value, $message = '') |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @psalm-assert object $value |
||
| 359 | * |
||
| 360 | * @param mixed $value |
||
| 361 | * @param string $message |
||
| 362 | * |
||
| 363 | 23 | * @throws InvalidArgumentException |
|
| 364 | */ |
||
| 365 | 23 | public static function object($value, $message = '') |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @psalm-assert resource $value |
||
| 377 | * |
||
| 378 | * @param mixed $value |
||
| 379 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
| 380 | * @param string $message |
||
| 381 | * |
||
| 382 | 16 | * @throws InvalidArgumentException |
|
| 383 | */ |
||
| 384 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @psalm-assert callable $value |
||
| 404 | * |
||
| 405 | * @param mixed $value |
||
| 406 | * @param string $message |
||
| 407 | * |
||
| 408 | 20 | * @throws InvalidArgumentException |
|
| 409 | */ |
||
| 410 | 20 | public static function isCallable($value, $message = '') |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @psalm-assert array $value |
||
| 422 | * |
||
| 423 | * @param mixed $value |
||
| 424 | * @param string $message |
||
| 425 | * |
||
| 426 | 20 | * @throws InvalidArgumentException |
|
| 427 | */ |
||
| 428 | 20 | public static function isArray($value, $message = '') |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @psalm-assert iterable $value |
||
| 440 | * |
||
| 441 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
| 442 | * |
||
| 443 | * @param mixed $value |
||
| 444 | * @param string $message |
||
| 445 | * |
||
| 446 | 20 | * @throws InvalidArgumentException |
|
| 447 | */ |
||
| 448 | 20 | public static function isTraversable($value, $message = '') |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @param mixed $value |
||
| 468 | * @param string $message |
||
| 469 | * |
||
| 470 | 20 | * @throws InvalidArgumentException |
|
| 471 | */ |
||
| 472 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 481 | |||
| 482 | /** |
||
| 483 | * @psalm-assert countable $value |
||
| 484 | * |
||
| 485 | * @param mixed $value |
||
| 486 | * @param string $message |
||
| 487 | * |
||
| 488 | 24 | * @throws InvalidArgumentException |
|
| 489 | */ |
||
| 490 | 24 | public static function isCountable($value, $message = '') |
|
| 504 | |||
| 505 | /** |
||
| 506 | 912 | * @psalm-assert iterable $value |
|
| 507 | * |
||
| 508 | 912 | * @param mixed $value |
|
| 509 | 8 | * @param string $message |
|
| 510 | 8 | * |
|
| 511 | 8 | * @throws InvalidArgumentException |
|
| 512 | */ |
||
| 513 | View Code Duplication | public static function isIterable($value, $message = '') |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @psalm-template ExpectedType of object |
||
| 525 | * @psalm-param class-string<ExpectedType> $class |
||
| 526 | * @psalm-assert ExpectedType $value |
||
| 527 | 19 | * |
|
| 528 | * @param mixed $value |
||
| 529 | 19 | * @param string|object $class |
|
| 530 | 15 | * @param string $message |
|
| 531 | 15 | * |
|
| 532 | 15 | * @throws InvalidArgumentException |
|
| 533 | 15 | */ |
|
| 534 | public static function isInstanceOf($value, $class, $message = '') |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @psalm-template ExpectedType of object |
||
| 547 | * @psalm-param class-string<ExpectedType> $class |
||
| 548 | * @psalm-assert !ExpectedType $value |
||
| 549 | 16 | * |
|
| 550 | * @param mixed $value |
||
| 551 | 16 | * @param string|object $class |
|
| 552 | 4 | * @param string $message |
|
| 553 | 4 | * |
|
| 554 | 4 | * @throws InvalidArgumentException |
|
| 555 | 4 | */ |
|
| 556 | public static function notInstanceOf($value, $class, $message = '') |
||
| 566 | |||
| 567 | 20 | /** |
|
| 568 | * @param mixed $value |
||
| 569 | 20 | * @param array<object|string> $classes |
|
| 570 | 20 | * @param string $message |
|
| 571 | 8 | * |
|
| 572 | * @throws InvalidArgumentException |
||
| 573 | */ |
||
| 574 | public static function isInstanceOfAny($value, array $classes, $message = '') |
||
| 588 | |||
| 589 | /** |
||
| 590 | 23 | * @psalm-assert empty $value |
|
| 591 | * |
||
| 592 | 23 | * @param mixed $value |
|
| 593 | 8 | * @param string $message |
|
| 594 | 8 | * |
|
| 595 | 8 | * @throws InvalidArgumentException |
|
| 596 | */ |
||
| 597 | public static function isEmpty($value, $message = '') |
||
| 606 | |||
| 607 | /** |
||
| 608 | 23 | * @psalm-assert !empty $value |
|
| 609 | * |
||
| 610 | 23 | * @param mixed $value |
|
| 611 | 15 | * @param string $message |
|
| 612 | 15 | * |
|
| 613 | 15 | * @throws InvalidArgumentException |
|
| 614 | */ |
||
| 615 | public static function notEmpty($value, $message = '') |
||
| 624 | |||
| 625 | /** |
||
| 626 | 11 | * @psalm-assert null $value |
|
| 627 | * |
||
| 628 | 11 | * @param mixed $value |
|
| 629 | 8 | * @param string $message |
|
| 630 | 8 | * |
|
| 631 | 8 | * @throws InvalidArgumentException |
|
| 632 | */ |
||
| 633 | public static function null($value, $message = '') |
||
| 642 | |||
| 643 | /** |
||
| 644 | 11 | * @psalm-assert !null $value |
|
| 645 | * |
||
| 646 | 11 | * @param mixed $value |
|
| 647 | 3 | * @param string $message |
|
| 648 | 3 | * |
|
| 649 | * @throws InvalidArgumentException |
||
| 650 | */ |
||
| 651 | 8 | public static function notNull($value, $message = '') |
|
| 659 | |||
| 660 | /** |
||
| 661 | 15 | * @psalm-assert true $value |
|
| 662 | * |
||
| 663 | 15 | * @param mixed $value |
|
| 664 | 11 | * @param string $message |
|
| 665 | 11 | * |
|
| 666 | 11 | * @throws InvalidArgumentException |
|
| 667 | */ |
||
| 668 | public static function true($value, $message = '') |
||
| 677 | |||
| 678 | /** |
||
| 679 | 19 | * @psalm-assert false $value |
|
| 680 | * |
||
| 681 | 19 | * @param mixed $value |
|
| 682 | 15 | * @param string $message |
|
| 683 | 15 | * |
|
| 684 | 15 | * @throws InvalidArgumentException |
|
| 685 | */ |
||
| 686 | public static function false($value, $message = '') |
||
| 695 | |||
| 696 | /** |
||
| 697 | 51 | * @psalm-assert string $value |
|
| 698 | * |
||
| 699 | 51 | * @param mixed $value |
|
| 700 | 32 | * @param string $message |
|
| 701 | 4 | * |
|
| 702 | 4 | * @throws InvalidArgumentException |
|
| 703 | 4 | */ |
|
| 704 | View Code Duplication | public static function ip($value, $message = '') |
|
| 714 | |||
| 715 | /** |
||
| 716 | 51 | * @psalm-assert string $value |
|
| 717 | * |
||
| 718 | 51 | * @param mixed $value |
|
| 719 | 32 | * @param string $message |
|
| 720 | 20 | * |
|
| 721 | 20 | * @throws InvalidArgumentException |
|
| 722 | 20 | */ |
|
| 723 | View Code Duplication | public static function ipv4($value, $message = '') |
|
| 733 | |||
| 734 | /** |
||
| 735 | 51 | * @psalm-assert string $value |
|
| 736 | * |
||
| 737 | 51 | * @param mixed $value |
|
| 738 | 32 | * @param string $message |
|
| 739 | 16 | * |
|
| 740 | 16 | * @throws InvalidArgumentException |
|
| 741 | 16 | */ |
|
| 742 | View Code Duplication | public static function ipv6($value, $message = '') |
|
| 752 | |||
| 753 | /** |
||
| 754 | 20 | * @psalm-assert string $value |
|
| 755 | * |
||
| 756 | 20 | * @param mixed $value |
|
| 757 | 12 | * @param string $message |
|
| 758 | 8 | * |
|
| 759 | 8 | * @throws InvalidArgumentException |
|
| 760 | 8 | */ |
|
| 761 | View Code Duplication | public static function email($value, $message = '') |
|
| 771 | |||
| 772 | /** |
||
| 773 | 12 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
|
| 774 | * |
||
| 775 | 12 | * @param array $values |
|
| 776 | 12 | * @param string $message |
|
| 777 | * |
||
| 778 | 12 | * @throws InvalidArgumentException |
|
| 779 | 8 | */ |
|
| 780 | public static function uniqueValues(array $values, $message = '') |
||
| 795 | |||
| 796 | 33 | /** |
|
| 797 | * @param mixed $value |
||
| 798 | 33 | * @param mixed $expect |
|
| 799 | 17 | * @param string $message |
|
| 800 | 17 | * |
|
| 801 | 17 | * @throws InvalidArgumentException |
|
| 802 | 17 | */ |
|
| 803 | public static function eq($value, $expect, $message = '') |
||
| 813 | |||
| 814 | 28 | /** |
|
| 815 | * @param mixed $value |
||
| 816 | 28 | * @param mixed $expect |
|
| 817 | 16 | * @param string $message |
|
| 818 | 16 | * |
|
| 819 | 16 | * @throws InvalidArgumentException |
|
| 820 | */ |
||
| 821 | public static function notEq($value, $expect, $message = '') |
||
| 830 | |||
| 831 | /** |
||
| 832 | * @psalm-template ExpectedType |
||
| 833 | * @psalm-param ExpectedType $expect |
||
| 834 | * @psalm-assert =ExpectedType $value |
||
| 835 | 16 | * |
|
| 836 | * @param mixed $value |
||
| 837 | 16 | * @param mixed $expect |
|
| 838 | 12 | * @param string $message |
|
| 839 | 12 | * |
|
| 840 | 12 | * @throws InvalidArgumentException |
|
| 841 | 12 | */ |
|
| 842 | public static function same($value, $expect, $message = '') |
||
| 852 | |||
| 853 | 16 | /** |
|
| 854 | * @param mixed $value |
||
| 855 | 16 | * @param mixed $expect |
|
| 856 | 4 | * @param string $message |
|
| 857 | 4 | * |
|
| 858 | 4 | * @throws InvalidArgumentException |
|
| 859 | */ |
||
| 860 | public static function notSame($value, $expect, $message = '') |
||
| 869 | |||
| 870 | 8 | /** |
|
| 871 | * @param mixed $value |
||
| 872 | 8 | * @param mixed $limit |
|
| 873 | 4 | * @param string $message |
|
| 874 | 4 | * |
|
| 875 | 4 | * @throws InvalidArgumentException |
|
| 876 | 4 | */ |
|
| 877 | public static function greaterThan($value, $limit, $message = '') |
||
| 887 | |||
| 888 | 12 | /** |
|
| 889 | * @param mixed $value |
||
| 890 | 12 | * @param mixed $limit |
|
| 891 | 4 | * @param string $message |
|
| 892 | 4 | * |
|
| 893 | 4 | * @throws InvalidArgumentException |
|
| 894 | 4 | */ |
|
| 895 | public static function greaterThanEq($value, $limit, $message = '') |
||
| 905 | |||
| 906 | 8 | /** |
|
| 907 | * @param mixed $value |
||
| 908 | 8 | * @param mixed $limit |
|
| 909 | 4 | * @param string $message |
|
| 910 | 4 | * |
|
| 911 | 4 | * @throws InvalidArgumentException |
|
| 912 | 4 | */ |
|
| 913 | public static function lessThan($value, $limit, $message = '') |
||
| 923 | |||
| 924 | 12 | /** |
|
| 925 | * @param mixed $value |
||
| 926 | 12 | * @param mixed $limit |
|
| 927 | 4 | * @param string $message |
|
| 928 | 4 | * |
|
| 929 | 4 | * @throws InvalidArgumentException |
|
| 930 | 4 | */ |
|
| 931 | public static function lessThanEq($value, $limit, $message = '') |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
| 944 | * |
||
| 945 | 16 | * @param mixed $value |
|
| 946 | * @param mixed $min |
||
| 947 | 16 | * @param mixed $max |
|
| 948 | 8 | * @param string $message |
|
| 949 | 8 | * |
|
| 950 | 8 | * @throws InvalidArgumentException |
|
| 951 | 8 | */ |
|
| 952 | 8 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 963 | |||
| 964 | /** |
||
| 965 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
| 966 | * |
||
| 967 | * @psalm-template ExpectedType |
||
| 968 | * @psalm-param array<ExpectedType> $values |
||
| 969 | * @psalm-assert ExpectedType $value |
||
| 970 | 8 | * |
|
| 971 | * @param mixed $value |
||
| 972 | 8 | * @param array $values |
|
| 973 | 4 | * @param string $message |
|
| 974 | 4 | * |
|
| 975 | 4 | * @throws InvalidArgumentException |
|
| 976 | 4 | */ |
|
| 977 | public static function oneOf($value, array $values, $message = '') |
||
| 987 | |||
| 988 | 80 | /** |
|
| 989 | * @param mixed $value |
||
| 990 | 80 | * @param string $subString |
|
| 991 | 32 | * @param string $message |
|
| 992 | 32 | * |
|
| 993 | 32 | * @throws InvalidArgumentException |
|
| 994 | 32 | */ |
|
| 995 | View Code Duplication | public static function contains($value, $subString, $message = '') |
|
| 1005 | |||
| 1006 | 80 | /** |
|
| 1007 | * @param mixed $value |
||
| 1008 | 80 | * @param string $subString |
|
| 1009 | 48 | * @param string $message |
|
| 1010 | 48 | * |
|
| 1011 | 48 | * @throws InvalidArgumentException |
|
| 1012 | 48 | */ |
|
| 1013 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
|
| 1023 | 40 | ||
| 1024 | /** |
||
| 1025 | 40 | * @param mixed $value |
|
| 1026 | 24 | * @param string $message |
|
| 1027 | 24 | * |
|
| 1028 | 24 | * @throws InvalidArgumentException |
|
| 1029 | */ |
||
| 1030 | public static function notWhitespaceOnly($value, $message = '') |
||
| 1039 | |||
| 1040 | 48 | /** |
|
| 1041 | * @param mixed $value |
||
| 1042 | 48 | * @param string $prefix |
|
| 1043 | 32 | * @param string $message |
|
| 1044 | 32 | * |
|
| 1045 | 32 | * @throws InvalidArgumentException |
|
| 1046 | 32 | */ |
|
| 1047 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | 35 | * @psalm-assert string $value |
|
| 1060 | * |
||
| 1061 | 35 | * @param mixed $value |
|
| 1062 | * @param string $message |
||
| 1063 | 24 | * |
|
| 1064 | * @throws InvalidArgumentException |
||
| 1065 | 24 | */ |
|
| 1066 | 20 | public static function startsWithLetter($value, $message = '') |
|
| 1086 | |||
| 1087 | 48 | /** |
|
| 1088 | * @param mixed $value |
||
| 1089 | 48 | * @param string $suffix |
|
| 1090 | 32 | * @param string $message |
|
| 1091 | 32 | * |
|
| 1092 | 32 | * @throws InvalidArgumentException |
|
| 1093 | 32 | */ |
|
| 1094 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
|
| 1104 | |||
| 1105 | 12 | /** |
|
| 1106 | * @param mixed $value |
||
| 1107 | 12 | * @param mixed $pattern |
|
| 1108 | 8 | * @param string $message |
|
| 1109 | 8 | * |
|
| 1110 | 8 | * @throws InvalidArgumentException |
|
| 1111 | */ |
||
| 1112 | public static function regex($value, $pattern, $message = '') |
||
| 1121 | |||
| 1122 | 12 | /** |
|
| 1123 | * @param mixed $value |
||
| 1124 | 12 | * @param mixed $pattern |
|
| 1125 | 4 | * @param string $message |
|
| 1126 | 4 | * |
|
| 1127 | 4 | * @throws InvalidArgumentException |
|
| 1128 | 4 | */ |
|
| 1129 | 4 | public static function notRegex($value, $pattern, $message = '') |
|
| 1140 | |||
| 1141 | /** |
||
| 1142 | 28 | * @psalm-assert !numeric $value |
|
| 1143 | * |
||
| 1144 | 28 | * @param mixed $value |
|
| 1145 | * @param string $message |
||
| 1146 | 28 | * |
|
| 1147 | 16 | * @throws InvalidArgumentException |
|
| 1148 | 16 | */ |
|
| 1149 | 16 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
| 1160 | |||
| 1161 | /** |
||
| 1162 | 20 | * @psalm-assert string $value |
|
| 1163 | * |
||
| 1164 | 20 | * @param mixed $value |
|
| 1165 | * @param string $message |
||
| 1166 | 12 | * |
|
| 1167 | 12 | * @throws InvalidArgumentException |
|
| 1168 | 12 | */ |
|
| 1169 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
| 1185 | 12 | ||
| 1186 | /** |
||
| 1187 | 12 | * @param mixed $value |
|
| 1188 | 12 | * @param string $message |
|
| 1189 | 12 | * |
|
| 1190 | 12 | * @throws InvalidArgumentException |
|
| 1191 | */ |
||
| 1192 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 1206 | 12 | ||
| 1207 | /** |
||
| 1208 | 12 | * @param mixed $value |
|
| 1209 | 12 | * @param string $message |
|
| 1210 | 12 | * |
|
| 1211 | 12 | * @throws InvalidArgumentException |
|
| 1212 | */ |
||
| 1213 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 1227 | 16 | ||
| 1228 | /** |
||
| 1229 | 16 | * @param mixed $value |
|
| 1230 | 16 | * @param string $message |
|
| 1231 | 16 | * |
|
| 1232 | 16 | * @throws InvalidArgumentException |
|
| 1233 | */ |
||
| 1234 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 1248 | 16 | ||
| 1249 | /** |
||
| 1250 | 16 | * @param mixed $value |
|
| 1251 | 16 | * @param string $message |
|
| 1252 | 16 | * |
|
| 1253 | 16 | * @throws InvalidArgumentException |
|
| 1254 | */ |
||
| 1255 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 1269 | |||
| 1270 | 36 | /** |
|
| 1271 | * @param mixed $value |
||
| 1272 | 36 | * @param mixed $length |
|
| 1273 | 24 | * @param string $message |
|
| 1274 | 24 | * |
|
| 1275 | 24 | * @throws InvalidArgumentException |
|
| 1276 | 24 | */ |
|
| 1277 | public static function length($value, $length, $message = '') |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Inclusive min. |
||
| 1290 | 36 | * |
|
| 1291 | * @param mixed $value |
||
| 1292 | 36 | * @param mixed $min |
|
| 1293 | 12 | * @param string $message |
|
| 1294 | 12 | * |
|
| 1295 | 12 | * @throws InvalidArgumentException |
|
| 1296 | 12 | */ |
|
| 1297 | public static function minLength($value, $min, $message = '') |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Inclusive max. |
||
| 1310 | 36 | * |
|
| 1311 | * @param mixed $value |
||
| 1312 | 36 | * @param mixed $max |
|
| 1313 | 12 | * @param string $message |
|
| 1314 | 12 | * |
|
| 1315 | 12 | * @throws InvalidArgumentException |
|
| 1316 | 12 | */ |
|
| 1317 | public static function maxLength($value, $max, $message = '') |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
| 1330 | * |
||
| 1331 | 60 | * @param mixed $value |
|
| 1332 | * @param mixed $min |
||
| 1333 | 60 | * @param mixed $max |
|
| 1334 | * @param string $message |
||
| 1335 | 60 | * |
|
| 1336 | 24 | * @throws InvalidArgumentException |
|
| 1337 | 24 | */ |
|
| 1338 | 24 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 1351 | |||
| 1352 | /** |
||
| 1353 | 36 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
|
| 1354 | * |
||
| 1355 | 36 | * @param mixed $value |
|
| 1356 | * @param string $message |
||
| 1357 | 36 | * |
|
| 1358 | 12 | * @throws InvalidArgumentException |
|
| 1359 | 12 | */ |
|
| 1360 | 12 | View Code Duplication | public static function fileExists($value, $message = '') |
| 1371 | 12 | ||
| 1372 | /** |
||
| 1373 | 12 | * @param mixed $value |
|
| 1374 | * @param string $message |
||
| 1375 | 8 | * |
|
| 1376 | 4 | * @throws InvalidArgumentException |
|
| 1377 | 4 | */ |
|
| 1378 | 4 | View Code Duplication | public static function file($value, $message = '') |
| 1389 | 12 | ||
| 1390 | /** |
||
| 1391 | 12 | * @param mixed $value |
|
| 1392 | * @param string $message |
||
| 1393 | 8 | * |
|
| 1394 | 4 | * @throws InvalidArgumentException |
|
| 1395 | 4 | */ |
|
| 1396 | 4 | View Code Duplication | public static function directory($value, $message = '') |
| 1407 | |||
| 1408 | /** |
||
| 1409 | * @param mixed $value |
||
| 1410 | * @param string $message |
||
| 1411 | * |
||
| 1412 | * @throws InvalidArgumentException |
||
| 1413 | */ |
||
| 1414 | public static function readable($value, $message = '') |
||
| 1423 | |||
| 1424 | /** |
||
| 1425 | * @param mixed $value |
||
| 1426 | * @param string $message |
||
| 1427 | * |
||
| 1428 | * @throws InvalidArgumentException |
||
| 1429 | */ |
||
| 1430 | public static function writable($value, $message = '') |
||
| 1439 | |||
| 1440 | /** |
||
| 1441 | 8 | * @psalm-assert class-string $value |
|
| 1442 | * |
||
| 1443 | 8 | * @param mixed $value |
|
| 1444 | 4 | * @param string $message |
|
| 1445 | 4 | * |
|
| 1446 | 4 | * @throws InvalidArgumentException |
|
| 1447 | */ |
||
| 1448 | public static function classExists($value, $message = '') |
||
| 1457 | |||
| 1458 | 8 | /** |
|
| 1459 | * @param mixed $value |
||
| 1460 | 8 | * @param string|object $class |
|
| 1461 | 4 | * @param string $message |
|
| 1462 | 4 | * |
|
| 1463 | 4 | * @throws InvalidArgumentException |
|
| 1464 | 4 | */ |
|
| 1465 | public static function subclassOf($value, $class, $message = '') |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | 8 | * @psalm-assert class-string $value |
|
| 1478 | * |
||
| 1479 | 8 | * @param mixed $value |
|
| 1480 | 4 | * @param string $message |
|
| 1481 | 4 | * |
|
| 1482 | 4 | * @throws InvalidArgumentException |
|
| 1483 | */ |
||
| 1484 | public static function interfaceExists($value, $message = '') |
||
| 1493 | |||
| 1494 | 8 | /** |
|
| 1495 | * @param mixed $value |
||
| 1496 | 8 | * @param mixed $interface |
|
| 1497 | 4 | * @param string $message |
|
| 1498 | 4 | * |
|
| 1499 | 4 | * @throws InvalidArgumentException |
|
| 1500 | 4 | */ |
|
| 1501 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
|
| 1511 | |||
| 1512 | 12 | /** |
|
| 1513 | * @param string|object $classOrObject |
||
| 1514 | 12 | * @param mixed $property |
|
| 1515 | 4 | * @param string $message |
|
| 1516 | 4 | * |
|
| 1517 | 4 | * @throws InvalidArgumentException |
|
| 1518 | */ |
||
| 1519 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
|
| 1528 | |||
| 1529 | 12 | /** |
|
| 1530 | * @param string|object $classOrObject |
||
| 1531 | 12 | * @param mixed $property |
|
| 1532 | 8 | * @param string $message |
|
| 1533 | 8 | * |
|
| 1534 | 8 | * @throws InvalidArgumentException |
|
| 1535 | */ |
||
| 1536 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
|
| 1545 | |||
| 1546 | 27 | /** |
|
| 1547 | * @param string|object $classOrObject |
||
| 1548 | 27 | * @param mixed $method |
|
| 1549 | 19 | * @param string $message |
|
| 1550 | 19 | * |
|
| 1551 | 19 | * @throws InvalidArgumentException |
|
| 1552 | */ |
||
| 1553 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
|
| 1562 | |||
| 1563 | 27 | /** |
|
| 1564 | * @param string|object $classOrObject |
||
| 1565 | 27 | * @param mixed $method |
|
| 1566 | 8 | * @param string $message |
|
| 1567 | 8 | * |
|
| 1568 | 8 | * @throws InvalidArgumentException |
|
| 1569 | */ |
||
| 1570 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
|
| 1579 | |||
| 1580 | 12 | /** |
|
| 1581 | * @param array $array |
||
| 1582 | 12 | * @param string|int $key |
|
| 1583 | 4 | * @param string $message |
|
| 1584 | 4 | * |
|
| 1585 | 4 | * @throws InvalidArgumentException |
|
| 1586 | */ |
||
| 1587 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
|
| 1596 | |||
| 1597 | 12 | /** |
|
| 1598 | * @param array $array |
||
| 1599 | 12 | * @param string|int $key |
|
| 1600 | 8 | * @param string $message |
|
| 1601 | 8 | * |
|
| 1602 | 8 | * @throws InvalidArgumentException |
|
| 1603 | */ |
||
| 1604 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
|
| 1613 | |||
| 1614 | /** |
||
| 1615 | 28 | * Checks if a value is a valid array key (int or string). |
|
| 1616 | * |
||
| 1617 | 28 | * @param mixed $key |
|
| 1618 | 20 | * @param string $message |
|
| 1619 | 20 | * |
|
| 1620 | 20 | * @throws InvalidArgumentException |
|
| 1621 | */ |
||
| 1622 | public static function validArrayKey($value, $message = '') |
||
| 1631 | |||
| 1632 | /** |
||
| 1633 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1634 | 8 | * |
|
| 1635 | * @param mixed $array |
||
| 1636 | 8 | * @param mixed $number |
|
| 1637 | 8 | * @param string $message |
|
| 1638 | * |
||
| 1639 | 8 | * @throws InvalidArgumentException |
|
| 1640 | */ |
||
| 1641 | 4 | public static function count($array, $number, $message = '') |
|
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1652 | 12 | * |
|
| 1653 | * @param mixed $array |
||
| 1654 | 12 | * @param mixed $min |
|
| 1655 | 4 | * @param string $message |
|
| 1656 | 4 | * |
|
| 1657 | 4 | * @throws InvalidArgumentException |
|
| 1658 | 4 | */ |
|
| 1659 | View Code Duplication | public static function minCount($array, $min, $message = '') |
|
| 1669 | |||
| 1670 | /** |
||
| 1671 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1672 | 12 | * |
|
| 1673 | * @param mixed $array |
||
| 1674 | 12 | * @param mixed $max |
|
| 1675 | 4 | * @param string $message |
|
| 1676 | 4 | * |
|
| 1677 | 4 | * @throws InvalidArgumentException |
|
| 1678 | 4 | */ |
|
| 1679 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
|
| 1689 | |||
| 1690 | /** |
||
| 1691 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1692 | * |
||
| 1693 | 20 | * @param mixed $array |
|
| 1694 | * @param mixed $min |
||
| 1695 | 20 | * @param mixed $max |
|
| 1696 | * @param string $message |
||
| 1697 | 20 | * |
|
| 1698 | 8 | * @throws InvalidArgumentException |
|
| 1699 | 8 | */ |
|
| 1700 | 8 | public static function countBetween($array, $min, $max, $message = '') |
|
| 1713 | 24 | ||
| 1714 | /** |
||
| 1715 | 24 | * @param mixed $array |
|
| 1716 | 20 | * @param string $message |
|
| 1717 | 20 | * |
|
| 1718 | * @throws InvalidArgumentException |
||
| 1719 | */ |
||
| 1720 | 4 | public static function isList($array, $message = '') |
|
| 1728 | 16 | ||
| 1729 | /** |
||
| 1730 | * @param mixed $array |
||
| 1731 | 16 | * @param string $message |
|
| 1732 | 16 | * |
|
| 1733 | * @throws InvalidArgumentException |
||
| 1734 | 12 | */ |
|
| 1735 | 16 | public static function isMap($array, $message = '') |
|
| 1749 | 56 | ||
| 1750 | /** |
||
| 1751 | 56 | * @param mixed $value |
|
| 1752 | * @param string $message |
||
| 1753 | * |
||
| 1754 | * @throws InvalidArgumentException |
||
| 1755 | 56 | */ |
|
| 1756 | 4 | public static function uuid($value, $message = '') |
|
| 1773 | |||
| 1774 | 24 | /** |
|
| 1775 | * @param Closure $expression |
||
| 1776 | 24 | * @param string|object $class |
|
| 1777 | * @param string $message |
||
| 1778 | 24 | * |
|
| 1779 | * @throws InvalidArgumentException |
||
| 1780 | */ |
||
| 1781 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 1807 | 532 | ||
| 1808 | 436 | /** |
|
| 1809 | 436 | * @throws BadMethodCallException |
|
| 1810 | */ |
||
| 1811 | public static function __callStatic($name, $arguments) |
||
| 1839 | |||
| 1840 | 645 | /** |
|
| 1841 | 11 | * @param mixed $value |
|
| 1842 | * |
||
| 1843 | * @return string |
||
| 1844 | 636 | */ |
|
| 1845 | 15 | protected static function valueToString($value) |
|
| 1881 | |||
| 1882 | 276 | /** |
|
| 1883 | * @param mixed $value |
||
| 1884 | * |
||
| 1885 | 168 | * @return string |
|
| 1886 | */ |
||
| 1887 | 168 | protected static function typeToString($value) |
|
| 1891 | 168 | ||
| 1892 | protected static function strlen($value) |
||
| 1904 | |||
| 1905 | 976 | /** |
|
| 1906 | * @param string $message |
||
| 1907 | * |
||
| 1908 | * @throws InvalidArgumentException |
||
| 1909 | */ |
||
| 1910 | protected static function reportInvalidArgument($message) |
||
| 1914 | |||
| 1915 | private function __construct() |
||
| 1918 | } |
||
| 1919 |