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