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 |
||
| 219 | class Assert |
||
|
|
|||
| 220 | { |
||
| 221 | /** |
||
| 222 | * @psalm-pure |
||
| 223 | * @psalm-assert string $value |
||
| 224 | * |
||
| 225 | * @param mixed $value |
||
| 226 | * @param string $message |
||
| 227 | * |
||
| 228 | * @throws InvalidArgumentException |
||
| 229 | */ |
||
| 230 | 241 | public static function string($value, $message = '') |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @psalm-pure |
||
| 242 | * @psalm-assert string $value |
||
| 243 | * @psalm-assert !empty $value |
||
| 244 | * |
||
| 245 | * @param mixed $value |
||
| 246 | * @param string $message |
||
| 247 | * |
||
| 248 | * @throws InvalidArgumentException |
||
| 249 | */ |
||
| 250 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @psalm-pure |
||
| 258 | * @psalm-assert int $value |
||
| 259 | * |
||
| 260 | * @param mixed $value |
||
| 261 | * @param string $message |
||
| 262 | * |
||
| 263 | * @throws InvalidArgumentException |
||
| 264 | */ |
||
| 265 | 17 | public static function integer($value, $message = '') |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @psalm-pure |
||
| 277 | * @psalm-assert numeric $value |
||
| 278 | * |
||
| 279 | * @param mixed $value |
||
| 280 | * @param string $message |
||
| 281 | * |
||
| 282 | * @throws InvalidArgumentException |
||
| 283 | */ |
||
| 284 | 16 | public static function integerish($value, $message = '') |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @psalm-pure |
||
| 296 | * @psalm-assert float $value |
||
| 297 | * |
||
| 298 | * @param mixed $value |
||
| 299 | * @param string $message |
||
| 300 | * |
||
| 301 | * @throws InvalidArgumentException |
||
| 302 | */ |
||
| 303 | 16 | public static function float($value, $message = '') |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @psalm-pure |
||
| 315 | * @psalm-assert numeric $value |
||
| 316 | * |
||
| 317 | * @param mixed $value |
||
| 318 | * @param string $message |
||
| 319 | * |
||
| 320 | * @throws InvalidArgumentException |
||
| 321 | */ |
||
| 322 | 20 | public static function numeric($value, $message = '') |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @psalm-pure |
||
| 334 | * @psalm-assert int $value |
||
| 335 | * |
||
| 336 | * @param mixed $value |
||
| 337 | * @param string $message |
||
| 338 | * |
||
| 339 | * @throws InvalidArgumentException |
||
| 340 | */ |
||
| 341 | 24 | public static function natural($value, $message = '') |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @psalm-pure |
||
| 353 | * @psalm-assert bool $value |
||
| 354 | * |
||
| 355 | * @param mixed $value |
||
| 356 | * @param string $message |
||
| 357 | * |
||
| 358 | * @throws InvalidArgumentException |
||
| 359 | */ |
||
| 360 | 16 | public static function boolean($value, $message = '') |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @psalm-pure |
||
| 372 | * @psalm-assert scalar $value |
||
| 373 | * |
||
| 374 | * @param mixed $value |
||
| 375 | * @param string $message |
||
| 376 | * |
||
| 377 | * @throws InvalidArgumentException |
||
| 378 | */ |
||
| 379 | 23 | public static function scalar($value, $message = '') |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @psalm-pure |
||
| 391 | * @psalm-assert object $value |
||
| 392 | * |
||
| 393 | * @param mixed $value |
||
| 394 | * @param string $message |
||
| 395 | * |
||
| 396 | * @throws InvalidArgumentException |
||
| 397 | */ |
||
| 398 | 23 | public static function object($value, $message = '') |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @psalm-pure |
||
| 410 | * @psalm-assert resource $value |
||
| 411 | * |
||
| 412 | * @param mixed $value |
||
| 413 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
| 414 | * @param string $message |
||
| 415 | * |
||
| 416 | * @throws InvalidArgumentException |
||
| 417 | */ |
||
| 418 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @psalm-pure |
||
| 438 | * @psalm-assert callable $value |
||
| 439 | * |
||
| 440 | * @param mixed $value |
||
| 441 | * @param string $message |
||
| 442 | * |
||
| 443 | * @throws InvalidArgumentException |
||
| 444 | */ |
||
| 445 | 20 | public static function isCallable($value, $message = '') |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @psalm-pure |
||
| 457 | * @psalm-assert array $value |
||
| 458 | * |
||
| 459 | * @param mixed $value |
||
| 460 | * @param string $message |
||
| 461 | * |
||
| 462 | * @throws InvalidArgumentException |
||
| 463 | */ |
||
| 464 | 20 | public static function isArray($value, $message = '') |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @psalm-pure |
||
| 476 | * @psalm-assert iterable $value |
||
| 477 | * |
||
| 478 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
| 479 | * |
||
| 480 | * @param mixed $value |
||
| 481 | * @param string $message |
||
| 482 | * |
||
| 483 | * @throws InvalidArgumentException |
||
| 484 | */ |
||
| 485 | 20 | public static function isTraversable($value, $message = '') |
|
| 502 | |||
| 503 | /** |
||
| 504 | * @psalm-pure |
||
| 505 | * @psalm-assert array|ArrayAccess $value |
||
| 506 | * |
||
| 507 | * @param mixed $value |
||
| 508 | * @param string $message |
||
| 509 | * |
||
| 510 | * @throws InvalidArgumentException |
||
| 511 | */ |
||
| 512 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 521 | |||
| 522 | /** |
||
| 523 | * @psalm-pure |
||
| 524 | * @psalm-assert countable $value |
||
| 525 | * |
||
| 526 | * @param mixed $value |
||
| 527 | * @param string $message |
||
| 528 | * |
||
| 529 | * @throws InvalidArgumentException |
||
| 530 | */ |
||
| 531 | 28 | public static function isCountable($value, $message = '') |
|
| 545 | |||
| 546 | /** |
||
| 547 | * @psalm-pure |
||
| 548 | * @psalm-assert iterable $value |
||
| 549 | * |
||
| 550 | * @param mixed $value |
||
| 551 | * @param string $message |
||
| 552 | * |
||
| 553 | * @throws InvalidArgumentException |
||
| 554 | */ |
||
| 555 | 1074 | View Code Duplication | public static function isIterable($value, $message = '') |
| 564 | |||
| 565 | /** |
||
| 566 | * @psalm-pure |
||
| 567 | * @psalm-template ExpectedType of object |
||
| 568 | * @psalm-param class-string<ExpectedType> $class |
||
| 569 | * @psalm-assert ExpectedType $value |
||
| 570 | * |
||
| 571 | * @param mixed $value |
||
| 572 | * @param string|object $class |
||
| 573 | * @param string $message |
||
| 574 | * |
||
| 575 | * @throws InvalidArgumentException |
||
| 576 | */ |
||
| 577 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
| 587 | |||
| 588 | /** |
||
| 589 | * @psalm-pure |
||
| 590 | * @psalm-template ExpectedType of object |
||
| 591 | * @psalm-param class-string<ExpectedType> $class |
||
| 592 | * @psalm-assert !ExpectedType $value |
||
| 593 | * |
||
| 594 | * @param mixed $value |
||
| 595 | * @param string|object $class |
||
| 596 | * @param string $message |
||
| 597 | * |
||
| 598 | * @throws InvalidArgumentException |
||
| 599 | */ |
||
| 600 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 610 | |||
| 611 | /** |
||
| 612 | * @psalm-pure |
||
| 613 | * @psalm-param array<class-string> $classes |
||
| 614 | * |
||
| 615 | * @param mixed $value |
||
| 616 | * @param array<object|string> $classes |
||
| 617 | * @param string $message |
||
| 618 | * |
||
| 619 | * @throws InvalidArgumentException |
||
| 620 | */ |
||
| 621 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 635 | |||
| 636 | /** |
||
| 637 | * @psalm-pure |
||
| 638 | * @psalm-template ExpectedType of object |
||
| 639 | * @psalm-param class-string<ExpectedType> $class |
||
| 640 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
||
| 641 | * |
||
| 642 | * @param object|string $value |
||
| 643 | * @param string $class |
||
| 644 | * @param string $message |
||
| 645 | * |
||
| 646 | * @throws InvalidArgumentException |
||
| 647 | */ |
||
| 648 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
| 660 | |||
| 661 | /** |
||
| 662 | * @psalm-pure |
||
| 663 | * @psalm-template UnexpectedType of object |
||
| 664 | * @psalm-param class-string<UnexpectedType> $class |
||
| 665 | * @psalm-assert !UnexpectedType $value |
||
| 666 | * @psalm-assert !class-string<UnexpectedType> $value |
||
| 667 | * |
||
| 668 | * @param object|string $value |
||
| 669 | * @param string $class |
||
| 670 | * @param string $message |
||
| 671 | * |
||
| 672 | * @throws InvalidArgumentException |
||
| 673 | */ |
||
| 674 | 20 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
| 686 | |||
| 687 | /** |
||
| 688 | * @psalm-pure |
||
| 689 | * @psalm-param array<class-string> $classes |
||
| 690 | * |
||
| 691 | * @param object|string $value |
||
| 692 | * @param string[] $classes |
||
| 693 | * @param string $message |
||
| 694 | * |
||
| 695 | * @throws InvalidArgumentException |
||
| 696 | */ |
||
| 697 | 24 | public static function isAnyOf($value, array $classes, $message = '') |
|
| 713 | |||
| 714 | /** |
||
| 715 | * @psalm-pure |
||
| 716 | * @psalm-assert empty $value |
||
| 717 | * |
||
| 718 | * @param mixed $value |
||
| 719 | * @param string $message |
||
| 720 | * |
||
| 721 | * @throws InvalidArgumentException |
||
| 722 | */ |
||
| 723 | 23 | public static function isEmpty($value, $message = '') |
|
| 732 | |||
| 733 | /** |
||
| 734 | * @psalm-pure |
||
| 735 | * @psalm-assert !empty $value |
||
| 736 | * |
||
| 737 | * @param mixed $value |
||
| 738 | * @param string $message |
||
| 739 | * |
||
| 740 | * @throws InvalidArgumentException |
||
| 741 | */ |
||
| 742 | 55 | public static function notEmpty($value, $message = '') |
|
| 751 | |||
| 752 | /** |
||
| 753 | * @psalm-pure |
||
| 754 | * @psalm-assert null $value |
||
| 755 | * |
||
| 756 | * @param mixed $value |
||
| 757 | * @param string $message |
||
| 758 | * |
||
| 759 | * @throws InvalidArgumentException |
||
| 760 | */ |
||
| 761 | 11 | public static function null($value, $message = '') |
|
| 770 | |||
| 771 | /** |
||
| 772 | * @psalm-pure |
||
| 773 | * @psalm-assert !null $value |
||
| 774 | * |
||
| 775 | * @param mixed $value |
||
| 776 | * @param string $message |
||
| 777 | * |
||
| 778 | * @throws InvalidArgumentException |
||
| 779 | */ |
||
| 780 | 11 | public static function notNull($value, $message = '') |
|
| 788 | |||
| 789 | /** |
||
| 790 | * @psalm-pure |
||
| 791 | * @psalm-assert true $value |
||
| 792 | * |
||
| 793 | * @param mixed $value |
||
| 794 | * @param string $message |
||
| 795 | * |
||
| 796 | * @throws InvalidArgumentException |
||
| 797 | */ |
||
| 798 | 15 | public static function true($value, $message = '') |
|
| 807 | |||
| 808 | /** |
||
| 809 | * @psalm-pure |
||
| 810 | * @psalm-assert false $value |
||
| 811 | * |
||
| 812 | * @param mixed $value |
||
| 813 | * @param string $message |
||
| 814 | * |
||
| 815 | * @throws InvalidArgumentException |
||
| 816 | */ |
||
| 817 | 19 | public static function false($value, $message = '') |
|
| 826 | |||
| 827 | /** |
||
| 828 | * @psalm-pure |
||
| 829 | * @psalm-assert !false $value |
||
| 830 | * |
||
| 831 | * @param mixed $value |
||
| 832 | * @param string $message |
||
| 833 | * |
||
| 834 | * @throws InvalidArgumentException |
||
| 835 | */ |
||
| 836 | 19 | public static function notFalse($value, $message = '') |
|
| 837 | { |
||
| 838 | 19 | if (false === $value) { |
|
| 839 | 4 | static::reportInvalidArgument( |
|
| 840 | 4 | $message ?: 'Expected a value other than false.' |
|
| 841 | ); |
||
| 842 | } |
||
| 843 | 15 | } |
|
| 844 | |||
| 845 | /** |
||
| 846 | * @param mixed $value |
||
| 847 | * @param string $message |
||
| 848 | * |
||
| 849 | * @throws InvalidArgumentException |
||
| 850 | */ |
||
| 851 | 51 | View Code Duplication | public static function ip($value, $message = '') |
| 860 | |||
| 861 | /** |
||
| 862 | * @param mixed $value |
||
| 863 | * @param string $message |
||
| 864 | * |
||
| 865 | * @throws InvalidArgumentException |
||
| 866 | */ |
||
| 867 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
| 868 | { |
||
| 869 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
| 870 | 35 | static::reportInvalidArgument(\sprintf( |
|
| 871 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
| 872 | 35 | static::valueToString($value) |
|
| 873 | )); |
||
| 874 | } |
||
| 875 | 16 | } |
|
| 876 | |||
| 877 | /** |
||
| 878 | * @param mixed $value |
||
| 879 | * @param string $message |
||
| 880 | * |
||
| 881 | * @throws InvalidArgumentException |
||
| 882 | */ |
||
| 883 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
| 884 | { |
||
| 885 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
| 886 | 31 | static::reportInvalidArgument(\sprintf( |
|
| 887 | 31 | $message ?: 'Expected a value to be an IPv6. Got: %s', |
|
| 888 | 31 | static::valueToString($value) |
|
| 889 | )); |
||
| 890 | } |
||
| 891 | 20 | } |
|
| 892 | |||
| 893 | /** |
||
| 894 | * @psalm-pure |
||
| 895 | * |
||
| 896 | * @param string $value |
||
| 897 | * @param string $message |
||
| 898 | * |
||
| 899 | * @throws InvalidArgumentException |
||
| 900 | */ |
||
| 901 | 60 | View Code Duplication | public static function url($value, $message = '') |
| 902 | { |
||
| 903 | 60 | if (false === \filter_var($value, FILTER_VALIDATE_URL)) { |
|
| 904 | 28 | static::reportInvalidArgument(\sprintf( |
|
| 905 | 28 | $message ?: 'Expected a value to be a valid URL. Got %s', |
|
| 906 | 28 | static::valueToString($value) |
|
| 907 | )); |
||
| 908 | } |
||
| 909 | 32 | } |
|
| 910 | |||
| 911 | /** |
||
| 912 | * @param mixed $value |
||
| 913 | * @param string $message |
||
| 914 | * |
||
| 915 | * @throws InvalidArgumentException |
||
| 916 | */ |
||
| 917 | 20 | View Code Duplication | public static function email($value, $message = '') |
| 918 | { |
||
| 919 | 20 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 920 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 921 | 12 | $message ?: 'Expected a value to be a valid e-mail address. Got: %s', |
|
| 922 | 12 | static::valueToString($value) |
|
| 923 | )); |
||
| 924 | } |
||
| 925 | 8 | } |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
| 929 | * |
||
| 930 | * @param array $values |
||
| 931 | * @param string $message |
||
| 932 | * |
||
| 933 | * @throws InvalidArgumentException |
||
| 934 | */ |
||
| 935 | 12 | public static function uniqueValues(array $values, $message = '') |
|
| 936 | { |
||
| 937 | 12 | $allValues = \count($values); |
|
| 938 | 12 | $uniqueValues = \count(\array_unique($values)); |
|
| 939 | |||
| 940 | 12 | if ($allValues !== $uniqueValues) { |
|
| 941 | 8 | $difference = $allValues - $uniqueValues; |
|
| 942 | |||
| 943 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 944 | 8 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
|
| 945 | 8 | $difference, |
|
| 946 | 8 | (1 === $difference ? 'is' : 'are') |
|
| 947 | )); |
||
| 948 | } |
||
| 949 | 4 | } |
|
| 950 | |||
| 951 | /** |
||
| 952 | * @param mixed $value |
||
| 953 | * @param mixed $expect |
||
| 954 | * @param string $message |
||
| 955 | * |
||
| 956 | * @throws InvalidArgumentException |
||
| 957 | */ |
||
| 958 | 33 | public static function eq($value, $expect, $message = '') |
|
| 959 | { |
||
| 960 | 33 | if ($expect != $value) { |
|
| 961 | 17 | static::reportInvalidArgument(\sprintf( |
|
| 962 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 963 | 17 | static::valueToString($value), |
|
| 964 | 17 | static::valueToString($expect) |
|
| 965 | )); |
||
| 966 | } |
||
| 967 | 16 | } |
|
| 968 | |||
| 969 | /** |
||
| 970 | * @param mixed $value |
||
| 971 | * @param mixed $expect |
||
| 972 | * @param string $message |
||
| 973 | * |
||
| 974 | * @throws InvalidArgumentException |
||
| 975 | */ |
||
| 976 | 28 | public static function notEq($value, $expect, $message = '') |
|
| 977 | { |
||
| 978 | 28 | if ($expect == $value) { |
|
| 979 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 980 | 16 | $message ?: 'Expected a different value than %s.', |
|
| 981 | 16 | static::valueToString($expect) |
|
| 982 | )); |
||
| 983 | } |
||
| 984 | 12 | } |
|
| 985 | |||
| 986 | /** |
||
| 987 | * @psalm-pure |
||
| 988 | * |
||
| 989 | * @param mixed $value |
||
| 990 | * @param mixed $expect |
||
| 991 | * @param string $message |
||
| 992 | * |
||
| 993 | * @throws InvalidArgumentException |
||
| 994 | */ |
||
| 995 | 16 | public static function same($value, $expect, $message = '') |
|
| 996 | { |
||
| 997 | 16 | if ($expect !== $value) { |
|
| 998 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 999 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 1000 | 12 | static::valueToString($value), |
|
| 1001 | 12 | static::valueToString($expect) |
|
| 1002 | )); |
||
| 1003 | } |
||
| 1004 | 4 | } |
|
| 1005 | |||
| 1006 | /** |
||
| 1007 | * @psalm-pure |
||
| 1008 | * |
||
| 1009 | * @param mixed $value |
||
| 1010 | * @param mixed $expect |
||
| 1011 | * @param string $message |
||
| 1012 | * |
||
| 1013 | * @throws InvalidArgumentException |
||
| 1014 | */ |
||
| 1015 | 16 | public static function notSame($value, $expect, $message = '') |
|
| 1016 | { |
||
| 1017 | 16 | if ($expect === $value) { |
|
| 1018 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1019 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
| 1020 | 4 | static::valueToString($expect) |
|
| 1021 | )); |
||
| 1022 | } |
||
| 1023 | 12 | } |
|
| 1024 | |||
| 1025 | /** |
||
| 1026 | * @psalm-pure |
||
| 1027 | * |
||
| 1028 | * @param mixed $value |
||
| 1029 | * @param mixed $limit |
||
| 1030 | * @param string $message |
||
| 1031 | * |
||
| 1032 | * @throws InvalidArgumentException |
||
| 1033 | */ |
||
| 1034 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 1035 | { |
||
| 1036 | 8 | if ($value <= $limit) { |
|
| 1037 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1038 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 1039 | 4 | static::valueToString($value), |
|
| 1040 | 4 | static::valueToString($limit) |
|
| 1041 | )); |
||
| 1042 | } |
||
| 1043 | 4 | } |
|
| 1044 | |||
| 1045 | /** |
||
| 1046 | * @psalm-pure |
||
| 1047 | * |
||
| 1048 | * @param mixed $value |
||
| 1049 | * @param mixed $limit |
||
| 1050 | * @param string $message |
||
| 1051 | * |
||
| 1052 | * @throws InvalidArgumentException |
||
| 1053 | */ |
||
| 1054 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 1055 | { |
||
| 1056 | 12 | if ($value < $limit) { |
|
| 1057 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1058 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
| 1059 | 4 | static::valueToString($value), |
|
| 1060 | 4 | static::valueToString($limit) |
|
| 1061 | )); |
||
| 1062 | } |
||
| 1063 | 8 | } |
|
| 1064 | |||
| 1065 | /** |
||
| 1066 | * @psalm-pure |
||
| 1067 | * |
||
| 1068 | * @param mixed $value |
||
| 1069 | * @param mixed $limit |
||
| 1070 | * @param string $message |
||
| 1071 | * |
||
| 1072 | * @throws InvalidArgumentException |
||
| 1073 | */ |
||
| 1074 | 9 | public static function lessThan($value, $limit, $message = '') |
|
| 1075 | { |
||
| 1076 | 9 | if ($value >= $limit) { |
|
| 1077 | 5 | static::reportInvalidArgument(\sprintf( |
|
| 1078 | 5 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
| 1079 | 5 | static::valueToString($value), |
|
| 1080 | 5 | static::valueToString($limit) |
|
| 1081 | )); |
||
| 1082 | } |
||
| 1083 | 4 | } |
|
| 1084 | |||
| 1085 | /** |
||
| 1086 | * @psalm-pure |
||
| 1087 | * |
||
| 1088 | * @param mixed $value |
||
| 1089 | * @param mixed $limit |
||
| 1090 | * @param string $message |
||
| 1091 | * |
||
| 1092 | * @throws InvalidArgumentException |
||
| 1093 | */ |
||
| 1094 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 1095 | { |
||
| 1096 | 12 | if ($value > $limit) { |
|
| 1097 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1098 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
| 1099 | 4 | static::valueToString($value), |
|
| 1100 | 4 | static::valueToString($limit) |
|
| 1101 | )); |
||
| 1102 | } |
||
| 1103 | 8 | } |
|
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
| 1107 | * |
||
| 1108 | * @psalm-pure |
||
| 1109 | * |
||
| 1110 | * @param mixed $value |
||
| 1111 | * @param mixed $min |
||
| 1112 | * @param mixed $max |
||
| 1113 | * @param string $message |
||
| 1114 | * |
||
| 1115 | * @throws InvalidArgumentException |
||
| 1116 | */ |
||
| 1117 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 1118 | { |
||
| 1119 | 16 | if ($value < $min || $value > $max) { |
|
| 1120 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1121 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 1122 | 8 | static::valueToString($value), |
|
| 1123 | 8 | static::valueToString($min), |
|
| 1124 | 8 | static::valueToString($max) |
|
| 1125 | )); |
||
| 1126 | } |
||
| 1127 | 8 | } |
|
| 1128 | |||
| 1129 | /** |
||
| 1130 | * A more human-readable alias of Assert::inArray(). |
||
| 1131 | * |
||
| 1132 | * @psalm-pure |
||
| 1133 | * |
||
| 1134 | * @param mixed $value |
||
| 1135 | * @param array $values |
||
| 1136 | * @param string $message |
||
| 1137 | * |
||
| 1138 | * @throws InvalidArgumentException |
||
| 1139 | */ |
||
| 1140 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 1141 | { |
||
| 1142 | 8 | static::inArray($value, $values, $message); |
|
| 1143 | 4 | } |
|
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion. |
||
| 1147 | * |
||
| 1148 | * @psalm-pure |
||
| 1149 | * |
||
| 1150 | * @param mixed $value |
||
| 1151 | * @param array $values |
||
| 1152 | * @param string $message |
||
| 1153 | * |
||
| 1154 | * @throws InvalidArgumentException |
||
| 1155 | */ |
||
| 1156 | 16 | public static function inArray($value, array $values, $message = '') |
|
| 1157 | { |
||
| 1158 | 16 | if (!\in_array($value, $values, true)) { |
|
| 1159 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1160 | 8 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
| 1161 | 8 | static::valueToString($value), |
|
| 1162 | 8 | \implode(', ', \array_map(array('static', 'valueToString'), $values)) |
|
| 1163 | )); |
||
| 1164 | } |
||
| 1165 | 8 | } |
|
| 1166 | |||
| 1167 | /** |
||
| 1168 | * @psalm-pure |
||
| 1169 | * |
||
| 1170 | * @param string $value |
||
| 1171 | * @param string $subString |
||
| 1172 | * @param string $message |
||
| 1173 | * |
||
| 1174 | * @throws InvalidArgumentException |
||
| 1175 | */ |
||
| 1176 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 1177 | { |
||
| 1178 | 80 | if (false === \strpos($value, $subString)) { |
|
| 1179 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 1180 | 32 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
| 1181 | 32 | static::valueToString($value), |
|
| 1182 | 32 | static::valueToString($subString) |
|
| 1183 | )); |
||
| 1184 | } |
||
| 1185 | 48 | } |
|
| 1186 | |||
| 1187 | /** |
||
| 1188 | * @psalm-pure |
||
| 1189 | * |
||
| 1190 | * @param string $value |
||
| 1191 | * @param string $subString |
||
| 1192 | * @param string $message |
||
| 1193 | * |
||
| 1194 | * @throws InvalidArgumentException |
||
| 1195 | */ |
||
| 1196 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 1197 | { |
||
| 1198 | 80 | if (false !== \strpos($value, $subString)) { |
|
| 1199 | 48 | static::reportInvalidArgument(\sprintf( |
|
| 1200 | 48 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
|
| 1201 | 48 | static::valueToString($value), |
|
| 1202 | 48 | static::valueToString($subString) |
|
| 1203 | )); |
||
| 1204 | } |
||
| 1205 | 32 | } |
|
| 1206 | |||
| 1207 | /** |
||
| 1208 | * @psalm-pure |
||
| 1209 | * |
||
| 1210 | * @param string $value |
||
| 1211 | * @param string $message |
||
| 1212 | * |
||
| 1213 | * @throws InvalidArgumentException |
||
| 1214 | */ |
||
| 1215 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 1216 | { |
||
| 1217 | 40 | if (\preg_match('/^\s*$/', $value)) { |
|
| 1218 | 24 | static::reportInvalidArgument(\sprintf( |
|
| 1219 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
| 1220 | 24 | static::valueToString($value) |
|
| 1221 | )); |
||
| 1222 | } |
||
| 1223 | 16 | } |
|
| 1224 | |||
| 1225 | /** |
||
| 1226 | * @psalm-pure |
||
| 1227 | * |
||
| 1228 | * @param string $value |
||
| 1229 | * @param string $prefix |
||
| 1230 | * @param string $message |
||
| 1231 | * |
||
| 1232 | * @throws InvalidArgumentException |
||
| 1233 | */ |
||
| 1234 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 1235 | { |
||
| 1236 | 48 | if (0 !== \strpos($value, $prefix)) { |
|
| 1237 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 1238 | 32 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
| 1239 | 32 | static::valueToString($value), |
|
| 1240 | 32 | static::valueToString($prefix) |
|
| 1241 | )); |
||
| 1242 | } |
||
| 1243 | 16 | } |
|
| 1244 | |||
| 1245 | /** |
||
| 1246 | * @psalm-pure |
||
| 1247 | * |
||
| 1248 | * @param string $value |
||
| 1249 | * @param string $prefix |
||
| 1250 | * @param string $message |
||
| 1251 | * |
||
| 1252 | * @throws InvalidArgumentException |
||
| 1253 | */ |
||
| 1254 | 48 | View Code Duplication | public static function notStartsWith($value, $prefix, $message = '') |
| 1255 | { |
||
| 1256 | 48 | if (0 === \strpos($value, $prefix)) { |
|
| 1257 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 1258 | 16 | $message ?: 'Expected a value not to start with %2$s. Got: %s', |
|
| 1259 | 16 | static::valueToString($value), |
|
| 1260 | 16 | static::valueToString($prefix) |
|
| 1261 | )); |
||
| 1262 | } |
||
| 1263 | 32 | } |
|
| 1264 | |||
| 1265 | /** |
||
| 1266 | * @psalm-pure |
||
| 1267 | * |
||
| 1268 | * @param mixed $value |
||
| 1269 | * @param string $message |
||
| 1270 | * |
||
| 1271 | * @throws InvalidArgumentException |
||
| 1272 | */ |
||
| 1273 | 35 | public static function startsWithLetter($value, $message = '') |
|
| 1274 | { |
||
| 1275 | 35 | static::string($value); |
|
| 1276 | |||
| 1277 | 24 | $valid = isset($value[0]); |
|
| 1278 | |||
| 1279 | 24 | if ($valid) { |
|
| 1280 | 20 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1281 | 20 | \setlocale(LC_CTYPE, 'C'); |
|
| 1282 | 20 | $valid = \ctype_alpha($value[0]); |
|
| 1283 | 20 | \setlocale(LC_CTYPE, $locale); |
|
| 1284 | } |
||
| 1285 | |||
| 1286 | 24 | if (!$valid) { |
|
| 1287 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1288 | 12 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
| 1289 | 12 | static::valueToString($value) |
|
| 1290 | )); |
||
| 1291 | } |
||
| 1292 | 12 | } |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * @psalm-pure |
||
| 1296 | * |
||
| 1297 | * @param string $value |
||
| 1298 | * @param string $suffix |
||
| 1299 | * @param string $message |
||
| 1300 | * |
||
| 1301 | * @throws InvalidArgumentException |
||
| 1302 | */ |
||
| 1303 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 1304 | { |
||
| 1305 | 48 | if ($suffix !== \substr($value, -\strlen($suffix))) { |
|
| 1306 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 1307 | 32 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
| 1308 | 32 | static::valueToString($value), |
|
| 1309 | 32 | static::valueToString($suffix) |
|
| 1310 | )); |
||
| 1311 | } |
||
| 1312 | 16 | } |
|
| 1313 | |||
| 1314 | /** |
||
| 1315 | * @psalm-pure |
||
| 1316 | * |
||
| 1317 | * @param string $value |
||
| 1318 | * @param string $suffix |
||
| 1319 | * @param string $message |
||
| 1320 | * |
||
| 1321 | * @throws InvalidArgumentException |
||
| 1322 | */ |
||
| 1323 | 48 | View Code Duplication | public static function notEndsWith($value, $suffix, $message = '') |
| 1324 | { |
||
| 1325 | 48 | if ($suffix === \substr($value, -\strlen($suffix))) { |
|
| 1326 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 1327 | 16 | $message ?: 'Expected a value not to end with %2$s. Got: %s', |
|
| 1328 | 16 | static::valueToString($value), |
|
| 1329 | 16 | static::valueToString($suffix) |
|
| 1330 | )); |
||
| 1331 | } |
||
| 1332 | 32 | } |
|
| 1333 | |||
| 1334 | /** |
||
| 1335 | * @psalm-pure |
||
| 1336 | * |
||
| 1337 | * @param string $value |
||
| 1338 | * @param string $pattern |
||
| 1339 | * @param string $message |
||
| 1340 | * |
||
| 1341 | * @throws InvalidArgumentException |
||
| 1342 | */ |
||
| 1343 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 1344 | { |
||
| 1345 | 12 | if (!\preg_match($pattern, $value)) { |
|
| 1346 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1347 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
| 1348 | 8 | static::valueToString($value) |
|
| 1349 | )); |
||
| 1350 | } |
||
| 1351 | 4 | } |
|
| 1352 | |||
| 1353 | /** |
||
| 1354 | * @psalm-pure |
||
| 1355 | * |
||
| 1356 | * @param string $value |
||
| 1357 | * @param string $pattern |
||
| 1358 | * @param string $message |
||
| 1359 | * |
||
| 1360 | * @throws InvalidArgumentException |
||
| 1361 | */ |
||
| 1362 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
| 1363 | { |
||
| 1364 | 12 | if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 1365 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1366 | 4 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
|
| 1367 | 4 | static::valueToString($value), |
|
| 1368 | 4 | static::valueToString($pattern), |
|
| 1369 | 4 | $matches[0][1] |
|
| 1370 | )); |
||
| 1371 | } |
||
| 1372 | 8 | } |
|
| 1373 | |||
| 1374 | /** |
||
| 1375 | * @psalm-pure |
||
| 1376 | * |
||
| 1377 | * @param mixed $value |
||
| 1378 | * @param string $message |
||
| 1379 | * |
||
| 1380 | * @throws InvalidArgumentException |
||
| 1381 | */ |
||
| 1382 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
| 1383 | { |
||
| 1384 | 28 | static::string($value); |
|
| 1385 | |||
| 1386 | 28 | if (!\preg_match('/^\p{L}+$/u', $value)) { |
|
| 1387 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 1388 | 16 | $message ?: 'Expected a value to contain only Unicode letters. Got: %s', |
|
| 1389 | 16 | static::valueToString($value) |
|
| 1390 | )); |
||
| 1391 | } |
||
| 1392 | 12 | } |
|
| 1393 | |||
| 1394 | /** |
||
| 1395 | * @psalm-pure |
||
| 1396 | * |
||
| 1397 | * @param mixed $value |
||
| 1398 | * @param string $message |
||
| 1399 | * |
||
| 1400 | * @throws InvalidArgumentException |
||
| 1401 | */ |
||
| 1402 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
| 1403 | { |
||
| 1404 | 20 | static::string($value); |
|
| 1405 | |||
| 1406 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1407 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
| 1408 | 12 | $valid = !\ctype_alpha($value); |
|
| 1409 | 12 | \setlocale(LC_CTYPE, $locale); |
|
| 1410 | |||
| 1411 | 12 | if ($valid) { |
|
| 1412 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1413 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
| 1414 | 8 | static::valueToString($value) |
|
| 1415 | )); |
||
| 1416 | } |
||
| 1417 | 4 | } |
|
| 1418 | |||
| 1419 | /** |
||
| 1420 | * @psalm-pure |
||
| 1421 | * |
||
| 1422 | * @param string $value |
||
| 1423 | * @param string $message |
||
| 1424 | * |
||
| 1425 | * @throws InvalidArgumentException |
||
| 1426 | */ |
||
| 1427 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 1428 | { |
||
| 1429 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1430 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
| 1431 | 12 | $valid = !\ctype_digit($value); |
|
| 1432 | 12 | \setlocale(LC_CTYPE, $locale); |
|
| 1433 | |||
| 1434 | 12 | if ($valid) { |
|
| 1435 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1436 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
| 1437 | 8 | static::valueToString($value) |
|
| 1438 | )); |
||
| 1439 | } |
||
| 1440 | 4 | } |
|
| 1441 | |||
| 1442 | /** |
||
| 1443 | * @psalm-pure |
||
| 1444 | * |
||
| 1445 | * @param string $value |
||
| 1446 | * @param string $message |
||
| 1447 | * |
||
| 1448 | * @throws InvalidArgumentException |
||
| 1449 | */ |
||
| 1450 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 1451 | { |
||
| 1452 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1453 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
| 1454 | 12 | $valid = !\ctype_alnum($value); |
|
| 1455 | 12 | \setlocale(LC_CTYPE, $locale); |
|
| 1456 | |||
| 1457 | 12 | if ($valid) { |
|
| 1458 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1459 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
| 1460 | 8 | static::valueToString($value) |
|
| 1461 | )); |
||
| 1462 | } |
||
| 1463 | 4 | } |
|
| 1464 | |||
| 1465 | /** |
||
| 1466 | * @psalm-pure |
||
| 1467 | * @psalm-assert lowercase-string $value |
||
| 1468 | * |
||
| 1469 | * @param string $value |
||
| 1470 | * @param string $message |
||
| 1471 | * |
||
| 1472 | * @throws InvalidArgumentException |
||
| 1473 | */ |
||
| 1474 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 1475 | { |
||
| 1476 | 16 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1477 | 16 | \setlocale(LC_CTYPE, 'C'); |
|
| 1478 | 16 | $valid = !\ctype_lower($value); |
|
| 1479 | 16 | \setlocale(LC_CTYPE, $locale); |
|
| 1480 | |||
| 1481 | 16 | if ($valid) { |
|
| 1482 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1483 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
| 1484 | 12 | static::valueToString($value) |
|
| 1485 | )); |
||
| 1486 | } |
||
| 1487 | 4 | } |
|
| 1488 | |||
| 1489 | /** |
||
| 1490 | * @psalm-pure |
||
| 1491 | * @psalm-assert !lowercase-string $value |
||
| 1492 | * |
||
| 1493 | * @param string $value |
||
| 1494 | * @param string $message |
||
| 1495 | * |
||
| 1496 | * @throws InvalidArgumentException |
||
| 1497 | */ |
||
| 1498 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 1499 | { |
||
| 1500 | 16 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1501 | 16 | \setlocale(LC_CTYPE, 'C'); |
|
| 1502 | 16 | $valid = !\ctype_upper($value); |
|
| 1503 | 16 | \setlocale(LC_CTYPE, $locale); |
|
| 1504 | |||
| 1505 | 16 | if ($valid) { |
|
| 1506 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1507 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
| 1508 | 12 | static::valueToString($value) |
|
| 1509 | )); |
||
| 1510 | } |
||
| 1511 | 4 | } |
|
| 1512 | |||
| 1513 | /** |
||
| 1514 | * @psalm-pure |
||
| 1515 | * |
||
| 1516 | * @param string $value |
||
| 1517 | * @param int $length |
||
| 1518 | * @param string $message |
||
| 1519 | * |
||
| 1520 | * @throws InvalidArgumentException |
||
| 1521 | */ |
||
| 1522 | 36 | public static function length($value, $length, $message = '') |
|
| 1523 | { |
||
| 1524 | 36 | if ($length !== static::strlen($value)) { |
|
| 1525 | 24 | static::reportInvalidArgument(\sprintf( |
|
| 1526 | 24 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
| 1527 | 24 | static::valueToString($value), |
|
| 1528 | 24 | $length |
|
| 1529 | )); |
||
| 1530 | } |
||
| 1531 | 12 | } |
|
| 1532 | |||
| 1533 | /** |
||
| 1534 | * Inclusive min. |
||
| 1535 | * |
||
| 1536 | * @psalm-pure |
||
| 1537 | * |
||
| 1538 | * @param string $value |
||
| 1539 | * @param int|float $min |
||
| 1540 | * @param string $message |
||
| 1541 | * |
||
| 1542 | * @throws InvalidArgumentException |
||
| 1543 | */ |
||
| 1544 | 36 | public static function minLength($value, $min, $message = '') |
|
| 1545 | { |
||
| 1546 | 36 | if (static::strlen($value) < $min) { |
|
| 1547 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1548 | 12 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
| 1549 | 12 | static::valueToString($value), |
|
| 1550 | 12 | $min |
|
| 1551 | )); |
||
| 1552 | } |
||
| 1553 | 24 | } |
|
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Inclusive max. |
||
| 1557 | * |
||
| 1558 | * @psalm-pure |
||
| 1559 | * |
||
| 1560 | * @param string $value |
||
| 1561 | * @param int|float $max |
||
| 1562 | * @param string $message |
||
| 1563 | * |
||
| 1564 | * @throws InvalidArgumentException |
||
| 1565 | */ |
||
| 1566 | 36 | public static function maxLength($value, $max, $message = '') |
|
| 1567 | { |
||
| 1568 | 36 | if (static::strlen($value) > $max) { |
|
| 1569 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1570 | 12 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
| 1571 | 12 | static::valueToString($value), |
|
| 1572 | 12 | $max |
|
| 1573 | )); |
||
| 1574 | } |
||
| 1575 | 24 | } |
|
| 1576 | |||
| 1577 | /** |
||
| 1578 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
| 1579 | * |
||
| 1580 | * @psalm-pure |
||
| 1581 | * |
||
| 1582 | * @param string $value |
||
| 1583 | * @param int|float $min |
||
| 1584 | * @param int|float $max |
||
| 1585 | * @param string $message |
||
| 1586 | * |
||
| 1587 | * @throws InvalidArgumentException |
||
| 1588 | */ |
||
| 1589 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 1590 | { |
||
| 1591 | 60 | $length = static::strlen($value); |
|
| 1592 | |||
| 1593 | 60 | if ($length < $min || $length > $max) { |
|
| 1594 | 24 | static::reportInvalidArgument(\sprintf( |
|
| 1595 | 24 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
| 1596 | 24 | static::valueToString($value), |
|
| 1597 | 24 | $min, |
|
| 1598 | 24 | $max |
|
| 1599 | )); |
||
| 1600 | } |
||
| 1601 | 36 | } |
|
| 1602 | |||
| 1603 | /** |
||
| 1604 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
| 1605 | * |
||
| 1606 | * @param mixed $value |
||
| 1607 | * @param string $message |
||
| 1608 | * |
||
| 1609 | * @throws InvalidArgumentException |
||
| 1610 | */ |
||
| 1611 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
| 1612 | { |
||
| 1613 | 36 | static::string($value); |
|
| 1614 | |||
| 1615 | 36 | if (!\file_exists($value)) { |
|
| 1616 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1617 | 12 | $message ?: 'The file %s does not exist.', |
|
| 1618 | 12 | static::valueToString($value) |
|
| 1619 | )); |
||
| 1620 | } |
||
| 1621 | 24 | } |
|
| 1622 | |||
| 1623 | /** |
||
| 1624 | * @param mixed $value |
||
| 1625 | * @param string $message |
||
| 1626 | * |
||
| 1627 | * @throws InvalidArgumentException |
||
| 1628 | */ |
||
| 1629 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 1630 | { |
||
| 1631 | 12 | static::fileExists($value, $message); |
|
| 1632 | |||
| 1633 | 8 | if (!\is_file($value)) { |
|
| 1634 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1635 | 4 | $message ?: 'The path %s is not a file.', |
|
| 1636 | 4 | static::valueToString($value) |
|
| 1637 | )); |
||
| 1638 | } |
||
| 1639 | 4 | } |
|
| 1640 | |||
| 1641 | /** |
||
| 1642 | * @param mixed $value |
||
| 1643 | * @param string $message |
||
| 1644 | * |
||
| 1645 | * @throws InvalidArgumentException |
||
| 1646 | */ |
||
| 1647 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 1658 | |||
| 1659 | /** |
||
| 1660 | * @param string $value |
||
| 1661 | * @param string $message |
||
| 1662 | * |
||
| 1663 | * @throws InvalidArgumentException |
||
| 1664 | */ |
||
| 1665 | public static function readable($value, $message = '') |
||
| 1666 | { |
||
| 1667 | if (!\is_readable($value)) { |
||
| 1668 | static::reportInvalidArgument(\sprintf( |
||
| 1669 | $message ?: 'The path %s is not readable.', |
||
| 1670 | static::valueToString($value) |
||
| 1671 | )); |
||
| 1672 | } |
||
| 1673 | } |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * @param string $value |
||
| 1677 | * @param string $message |
||
| 1678 | * |
||
| 1679 | * @throws InvalidArgumentException |
||
| 1680 | */ |
||
| 1681 | public static function writable($value, $message = '') |
||
| 1682 | { |
||
| 1683 | if (!\is_writable($value)) { |
||
| 1684 | static::reportInvalidArgument(\sprintf( |
||
| 1685 | $message ?: 'The path %s is not writable.', |
||
| 1686 | static::valueToString($value) |
||
| 1687 | )); |
||
| 1688 | } |
||
| 1689 | } |
||
| 1690 | |||
| 1691 | /** |
||
| 1692 | * @psalm-assert class-string $value |
||
| 1693 | * |
||
| 1694 | * @param mixed $value |
||
| 1695 | * @param string $message |
||
| 1696 | * |
||
| 1697 | * @throws InvalidArgumentException |
||
| 1698 | */ |
||
| 1699 | 8 | public static function classExists($value, $message = '') |
|
| 1708 | |||
| 1709 | /** |
||
| 1710 | * @psalm-pure |
||
| 1711 | * @psalm-template ExpectedType of object |
||
| 1712 | * @psalm-param class-string<ExpectedType> $class |
||
| 1713 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
| 1714 | * |
||
| 1715 | * @param mixed $value |
||
| 1716 | * @param string|object $class |
||
| 1717 | * @param string $message |
||
| 1718 | * |
||
| 1719 | * @throws InvalidArgumentException |
||
| 1720 | */ |
||
| 1721 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 1731 | |||
| 1732 | /** |
||
| 1733 | * @param mixed $value |
||
| 1734 | * @param string $message |
||
| 1735 | * |
||
| 1736 | * @throws InvalidArgumentException |
||
| 1737 | */ |
||
| 1738 | 8 | public static function interfaceExists($value, $message = '') |
|
| 1747 | |||
| 1748 | /** |
||
| 1749 | * @psalm-pure |
||
| 1750 | * @psalm-template ExpectedType of object |
||
| 1751 | * @psalm-param class-string<ExpectedType> $interface |
||
| 1752 | * @psalm-assert class-string<ExpectedType> $value |
||
| 1753 | * |
||
| 1754 | * @param mixed $value |
||
| 1755 | * @param mixed $interface |
||
| 1756 | * @param string $message |
||
| 1757 | * |
||
| 1758 | * @throws InvalidArgumentException |
||
| 1759 | */ |
||
| 1760 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 1770 | |||
| 1771 | /** |
||
| 1772 | * @psalm-pure |
||
| 1773 | * @psalm-param class-string|object $classOrObject |
||
| 1774 | * |
||
| 1775 | * @param string|object $classOrObject |
||
| 1776 | * @param mixed $property |
||
| 1777 | * @param string $message |
||
| 1778 | * |
||
| 1779 | * @throws InvalidArgumentException |
||
| 1780 | */ |
||
| 1781 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 1790 | |||
| 1791 | /** |
||
| 1792 | * @psalm-pure |
||
| 1793 | * @psalm-param class-string|object $classOrObject |
||
| 1794 | * |
||
| 1795 | * @param string|object $classOrObject |
||
| 1796 | * @param mixed $property |
||
| 1797 | * @param string $message |
||
| 1798 | * |
||
| 1799 | * @throws InvalidArgumentException |
||
| 1800 | */ |
||
| 1801 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 1810 | |||
| 1811 | /** |
||
| 1812 | * @psalm-pure |
||
| 1813 | * @psalm-param class-string|object $classOrObject |
||
| 1814 | * |
||
| 1815 | * @param string|object $classOrObject |
||
| 1816 | * @param mixed $method |
||
| 1817 | * @param string $message |
||
| 1818 | * |
||
| 1819 | * @throws InvalidArgumentException |
||
| 1820 | */ |
||
| 1821 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 1830 | |||
| 1831 | /** |
||
| 1832 | * @psalm-pure |
||
| 1833 | * @psalm-param class-string|object $classOrObject |
||
| 1834 | * |
||
| 1835 | * @param string|object $classOrObject |
||
| 1836 | * @param mixed $method |
||
| 1837 | * @param string $message |
||
| 1838 | * |
||
| 1839 | * @throws InvalidArgumentException |
||
| 1840 | */ |
||
| 1841 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 1850 | |||
| 1851 | /** |
||
| 1852 | * @psalm-pure |
||
| 1853 | * |
||
| 1854 | * @param array $array |
||
| 1855 | * @param string|int $key |
||
| 1856 | * @param string $message |
||
| 1857 | * |
||
| 1858 | * @throws InvalidArgumentException |
||
| 1859 | */ |
||
| 1860 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 1869 | |||
| 1870 | /** |
||
| 1871 | * @psalm-pure |
||
| 1872 | * |
||
| 1873 | * @param array $array |
||
| 1874 | * @param string|int $key |
||
| 1875 | * @param string $message |
||
| 1876 | * |
||
| 1877 | * @throws InvalidArgumentException |
||
| 1878 | */ |
||
| 1879 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 1888 | |||
| 1889 | /** |
||
| 1890 | * Checks if a value is a valid array key (int or string). |
||
| 1891 | * |
||
| 1892 | * @psalm-pure |
||
| 1893 | * @psalm-assert array-key $value |
||
| 1894 | * |
||
| 1895 | * @param mixed $value |
||
| 1896 | * @param string $message |
||
| 1897 | * |
||
| 1898 | * @throws InvalidArgumentException |
||
| 1899 | */ |
||
| 1900 | 28 | public static function validArrayKey($value, $message = '') |
|
| 1909 | |||
| 1910 | /** |
||
| 1911 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1912 | * |
||
| 1913 | * @param Countable|array $array |
||
| 1914 | * @param int $number |
||
| 1915 | * @param string $message |
||
| 1916 | * |
||
| 1917 | * @throws InvalidArgumentException |
||
| 1918 | */ |
||
| 1919 | 8 | public static function count($array, $number, $message = '') |
|
| 1931 | |||
| 1932 | /** |
||
| 1933 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1934 | * |
||
| 1935 | * @param Countable|array $array |
||
| 1936 | * @param int|float $min |
||
| 1937 | * @param string $message |
||
| 1938 | * |
||
| 1939 | * @throws InvalidArgumentException |
||
| 1940 | */ |
||
| 1941 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 1951 | |||
| 1952 | /** |
||
| 1953 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1954 | * |
||
| 1955 | * @param Countable|array $array |
||
| 1956 | * @param int|float $max |
||
| 1957 | * @param string $message |
||
| 1958 | * |
||
| 1959 | * @throws InvalidArgumentException |
||
| 1960 | */ |
||
| 1961 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 1971 | |||
| 1972 | /** |
||
| 1973 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1974 | * |
||
| 1975 | * @param Countable|array $array |
||
| 1976 | * @param int|float $min |
||
| 1977 | * @param int|float $max |
||
| 1978 | * @param string $message |
||
| 1979 | * |
||
| 1980 | * @throws InvalidArgumentException |
||
| 1981 | */ |
||
| 1982 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
| 1995 | |||
| 1996 | /** |
||
| 1997 | * @psalm-pure |
||
| 1998 | * @psalm-assert list $array |
||
| 1999 | * |
||
| 2000 | * @param mixed $array |
||
| 2001 | * @param string $message |
||
| 2002 | * |
||
| 2003 | * @throws InvalidArgumentException |
||
| 2004 | */ |
||
| 2005 | 80 | View Code Duplication | public static function isList($array, $message = '') |
| 2013 | |||
| 2014 | /** |
||
| 2015 | * @psalm-pure |
||
| 2016 | * @psalm-assert list $array |
||
| 2017 | * @psalm-assert !empty $array |
||
| 2018 | * |
||
| 2019 | * @param mixed $array |
||
| 2020 | * @param string $message |
||
| 2021 | * |
||
| 2022 | * @throws InvalidArgumentException |
||
| 2023 | */ |
||
| 2024 | 40 | public static function isNonEmptyList($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 | * |
||
| 2036 | * @param mixed $array |
||
| 2037 | * @param string $message |
||
| 2038 | * |
||
| 2039 | * @throws InvalidArgumentException |
||
| 2040 | */ |
||
| 2041 | 32 | public static function isMap($array, $message = '') |
|
| 2052 | |||
| 2053 | /** |
||
| 2054 | * @psalm-pure |
||
| 2055 | * @psalm-template T |
||
| 2056 | * @psalm-param mixed|array<T> $array |
||
| 2057 | * @psalm-assert array<string, T> $array |
||
| 2058 | * @psalm-assert !empty $array |
||
| 2059 | * |
||
| 2060 | * @param mixed $array |
||
| 2061 | * @param string $message |
||
| 2062 | * |
||
| 2063 | * @throws InvalidArgumentException |
||
| 2064 | */ |
||
| 2065 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
| 2070 | |||
| 2071 | /** |
||
| 2072 | * @psalm-pure |
||
| 2073 | * |
||
| 2074 | * @param string $value |
||
| 2075 | * @param string $message |
||
| 2076 | * |
||
| 2077 | * @throws InvalidArgumentException |
||
| 2078 | */ |
||
| 2079 | 56 | public static function uuid($value, $message = '') |
|
| 2096 | |||
| 2097 | /** |
||
| 2098 | * @psalm-param class-string<Throwable> |
||
| 2099 | * |
||
| 2100 | * @param Closure $expression |
||
| 2101 | * @param string $class |
||
| 2102 | * @param string $message |
||
| 2103 | * |
||
| 2104 | * @throws InvalidArgumentException |
||
| 2105 | */ |
||
| 2106 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 2132 | |||
| 2133 | /** |
||
| 2134 | * @throws BadMethodCallException |
||
| 2135 | */ |
||
| 2136 | 1688 | public static function __callStatic($name, $arguments) |
|
| 2164 | |||
| 2165 | /** |
||
| 2166 | * @param mixed $value |
||
| 2167 | * |
||
| 2168 | * @return string |
||
| 2169 | */ |
||
| 2170 | 779 | protected static function valueToString($value) |
|
| 2210 | |||
| 2211 | /** |
||
| 2212 | * @param mixed $value |
||
| 2213 | * |
||
| 2214 | * @return string |
||
| 2215 | */ |
||
| 2216 | 251 | protected static function typeToString($value) |
|
| 2220 | |||
| 2221 | 168 | protected static function strlen($value) |
|
| 2233 | |||
| 2234 | /** |
||
| 2235 | * @param string $message |
||
| 2236 | * |
||
| 2237 | * @throws InvalidArgumentException |
||
| 2238 | * |
||
| 2239 | * @psalm-pure this method is not supposed to perform side-effects |
||
| 2240 | */ |
||
| 2241 | 1093 | protected static function reportInvalidArgument($message) |
|
| 2245 | |||
| 2246 | private function __construct() |
||
| 2249 | } |
||
| 2250 |