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 |
||
| 195 | class Assert |
||
|
|
|||
| 196 | { |
||
| 197 | 158 | public static function string($value, $message = '') |
|
| 198 | { |
||
| 199 | 158 | if (!is_string($value)) { |
|
| 200 | 26 | static::reportInvalidArgument(sprintf( |
|
| 201 | 26 | $message ?: 'Expected a string. Got: %s', |
|
| 202 | 26 | static::typeToString($value) |
|
| 203 | )); |
||
| 204 | } |
||
| 205 | 132 | } |
|
| 206 | |||
| 207 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 212 | |||
| 213 | 17 | public static function integer($value, $message = '') |
|
| 222 | |||
| 223 | 16 | public static function integerish($value, $message = '') |
|
| 232 | |||
| 233 | 16 | public static function float($value, $message = '') |
|
| 242 | |||
| 243 | 20 | public static function numeric($value, $message = '') |
|
| 252 | |||
| 253 | 24 | public static function natural($value, $message = '') |
|
| 262 | |||
| 263 | 16 | public static function boolean($value, $message = '') |
|
| 272 | |||
| 273 | 23 | public static function scalar($value, $message = '') |
|
| 282 | |||
| 283 | 23 | public static function object($value, $message = '') |
|
| 292 | |||
| 293 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 294 | { |
||
| 295 | 16 | if (!is_resource($value)) { |
|
| 296 | 4 | static::reportInvalidArgument(sprintf( |
|
| 297 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
| 298 | 4 | static::typeToString($value) |
|
| 299 | )); |
||
| 300 | } |
||
| 301 | |||
| 302 | 12 | if ($type && $type !== get_resource_type($value)) { |
|
| 303 | 4 | static::reportInvalidArgument(sprintf( |
|
| 304 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
| 305 | 4 | static::typeToString($value), |
|
| 306 | $type |
||
| 307 | )); |
||
| 308 | } |
||
| 309 | 8 | } |
|
| 310 | |||
| 311 | 20 | public static function isCallable($value, $message = '') |
|
| 320 | |||
| 321 | 20 | public static function isArray($value, $message = '') |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @deprecated |
||
| 333 | */ |
||
| 334 | public static function isTraversable($value, $message = '') |
||
| 335 | { |
||
| 336 | @trigger_error( |
||
| 337 | sprintf( |
||
| 338 | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', |
||
| 339 | __METHOD__ |
||
| 340 | ), |
||
| 341 | E_USER_DEPRECATED |
||
| 342 | ); |
||
| 343 | |||
| 344 | if (!is_array($value) && !($value instanceof Traversable)) { |
||
| 345 | static::reportInvalidArgument(sprintf( |
||
| 346 | $message ?: 'Expected a traversable. Got: %s', |
||
| 347 | static::typeToString($value) |
||
| 348 | )); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 353 | { |
||
| 354 | 20 | if (!is_array($value) && !($value instanceof ArrayAccess)) { |
|
| 355 | 8 | static::reportInvalidArgument(sprintf( |
|
| 356 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
| 357 | 8 | static::typeToString($value) |
|
| 358 | )); |
||
| 359 | } |
||
| 360 | 12 | } |
|
| 361 | |||
| 362 | 24 | View Code Duplication | public static function isCountable($value, $message = '') |
| 363 | { |
||
| 364 | 24 | if (!is_array($value) && !($value instanceof Countable)) { |
|
| 365 | 12 | static::reportInvalidArgument(sprintf( |
|
| 366 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
| 367 | 12 | static::typeToString($value) |
|
| 368 | )); |
||
| 369 | } |
||
| 370 | 12 | } |
|
| 371 | |||
| 372 | 746 | View Code Duplication | public static function isIterable($value, $message = '') |
| 373 | { |
||
| 374 | 746 | if (!is_array($value) && !($value instanceof Traversable)) { |
|
| 375 | 8 | static::reportInvalidArgument(sprintf( |
|
| 376 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
| 377 | 8 | static::typeToString($value) |
|
| 378 | )); |
||
| 379 | } |
||
| 380 | 742 | } |
|
| 381 | |||
| 382 | 16 | public static function isInstanceOf($value, $class, $message = '') |
|
| 383 | { |
||
| 384 | 16 | if (!($value instanceof $class)) { |
|
| 385 | 12 | static::reportInvalidArgument(sprintf( |
|
| 386 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
| 387 | 12 | static::typeToString($value), |
|
| 388 | $class |
||
| 389 | )); |
||
| 390 | } |
||
| 391 | 4 | } |
|
| 392 | |||
| 393 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 394 | { |
||
| 395 | 16 | if ($value instanceof $class) { |
|
| 396 | 4 | static::reportInvalidArgument(sprintf( |
|
| 397 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
| 398 | 4 | static::typeToString($value), |
|
| 399 | $class |
||
| 400 | )); |
||
| 401 | } |
||
| 402 | 12 | } |
|
| 403 | |||
| 404 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 418 | |||
| 419 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
| 420 | { |
||
| 421 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 422 | |||
| 423 | 16 | if (!is_a($value, $class, is_string($value))) { |
|
| 424 | 12 | static::reportInvalidArgument(sprintf( |
|
| 425 | 12 | $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', |
|
| 426 | 12 | static::typeToString($value), |
|
| 427 | $class |
||
| 428 | )); |
||
| 429 | } |
||
| 430 | 4 | } |
|
| 431 | |||
| 432 | 20 | View Code Duplication | public static function notAOf($value, $class, $message = '') |
| 444 | |||
| 445 | 24 | public static function isAOfAny($value, array $classes, $message = '') |
|
| 446 | { |
||
| 447 | 24 | foreach ($classes as $class) { |
|
| 448 | 24 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 449 | |||
| 450 | 20 | if (is_a($value, $class, is_string($value))) { |
|
| 451 | 8 | return; |
|
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | 12 | static::reportInvalidArgument(sprintf( |
|
| 456 | 12 | $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
| 457 | 12 | static::typeToString($value), |
|
| 458 | 12 | implode(', ', array_map(array('static', 'valueToString'), $classes)) |
|
| 459 | )); |
||
| 460 | } |
||
| 461 | |||
| 462 | 23 | public static function isEmpty($value, $message = '') |
|
| 471 | |||
| 472 | 23 | public static function notEmpty($value, $message = '') |
|
| 481 | |||
| 482 | 11 | public static function null($value, $message = '') |
|
| 491 | |||
| 492 | 11 | public static function notNull($value, $message = '') |
|
| 500 | |||
| 501 | 15 | public static function true($value, $message = '') |
|
| 502 | { |
||
| 503 | 15 | if (true !== $value) { |
|
| 504 | 11 | static::reportInvalidArgument(sprintf( |
|
| 505 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
| 506 | 11 | static::valueToString($value) |
|
| 507 | )); |
||
| 508 | } |
||
| 509 | 4 | } |
|
| 510 | |||
| 511 | 19 | public static function false($value, $message = '') |
|
| 512 | { |
||
| 513 | 19 | if (false !== $value) { |
|
| 514 | 15 | static::reportInvalidArgument(sprintf( |
|
| 515 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
| 516 | 15 | static::valueToString($value) |
|
| 517 | )); |
||
| 518 | } |
||
| 519 | 4 | } |
|
| 520 | |||
| 521 | 47 | View Code Duplication | public static function ip($value, $message = '') |
| 530 | |||
| 531 | 47 | View Code Duplication | public static function ipv4($value, $message = '') |
| 540 | |||
| 541 | 47 | View Code Duplication | public static function ipv6($value, $message = '') |
| 550 | |||
| 551 | 12 | public static function uniqueValues(array $values, $message = '') |
|
| 552 | { |
||
| 553 | 12 | $allValues = count($values); |
|
| 554 | 12 | $uniqueValues = count(array_unique($values)); |
|
| 555 | |||
| 556 | 12 | if ($allValues !== $uniqueValues) { |
|
| 557 | 8 | $difference = $allValues - $uniqueValues; |
|
| 558 | |||
| 559 | 8 | static::reportInvalidArgument(sprintf( |
|
| 560 | 8 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
|
| 561 | $difference, |
||
| 562 | 8 | (1 === $difference ? 'is' : 'are') |
|
| 563 | )); |
||
| 564 | } |
||
| 565 | 4 | } |
|
| 566 | |||
| 567 | 33 | public static function eq($value, $expect, $message = '') |
|
| 568 | { |
||
| 569 | 33 | if ($expect != $value) { |
|
| 570 | 17 | static::reportInvalidArgument(sprintf( |
|
| 571 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 572 | 17 | static::valueToString($value), |
|
| 573 | 17 | static::valueToString($expect) |
|
| 574 | )); |
||
| 575 | } |
||
| 576 | 16 | } |
|
| 577 | |||
| 578 | 28 | public static function notEq($value, $expect, $message = '') |
|
| 579 | { |
||
| 580 | 28 | if ($expect == $value) { |
|
| 581 | 16 | static::reportInvalidArgument(sprintf( |
|
| 582 | 16 | $message ?: 'Expected a different value than %s.', |
|
| 583 | 16 | static::valueToString($expect) |
|
| 584 | )); |
||
| 585 | } |
||
| 586 | 12 | } |
|
| 587 | |||
| 588 | 16 | public static function same($value, $expect, $message = '') |
|
| 589 | { |
||
| 590 | 16 | if ($expect !== $value) { |
|
| 591 | 12 | static::reportInvalidArgument(sprintf( |
|
| 592 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 593 | 12 | static::valueToString($value), |
|
| 594 | 12 | static::valueToString($expect) |
|
| 595 | )); |
||
| 596 | } |
||
| 597 | 4 | } |
|
| 598 | |||
| 599 | 16 | public static function notSame($value, $expect, $message = '') |
|
| 600 | { |
||
| 601 | 16 | if ($expect === $value) { |
|
| 602 | 4 | static::reportInvalidArgument(sprintf( |
|
| 603 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
| 604 | 4 | static::valueToString($expect) |
|
| 605 | )); |
||
| 606 | } |
||
| 607 | 12 | } |
|
| 608 | |||
| 609 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 610 | { |
||
| 611 | 8 | if ($value <= $limit) { |
|
| 612 | 4 | static::reportInvalidArgument(sprintf( |
|
| 613 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 614 | 4 | static::valueToString($value), |
|
| 615 | 4 | static::valueToString($limit) |
|
| 616 | )); |
||
| 617 | } |
||
| 618 | 4 | } |
|
| 619 | |||
| 620 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 621 | { |
||
| 622 | 12 | if ($value < $limit) { |
|
| 623 | 4 | static::reportInvalidArgument(sprintf( |
|
| 624 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
| 625 | 4 | static::valueToString($value), |
|
| 626 | 4 | static::valueToString($limit) |
|
| 627 | )); |
||
| 628 | } |
||
| 629 | 8 | } |
|
| 630 | |||
| 631 | 8 | public static function lessThan($value, $limit, $message = '') |
|
| 632 | { |
||
| 633 | 8 | if ($value >= $limit) { |
|
| 634 | 4 | static::reportInvalidArgument(sprintf( |
|
| 635 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
| 636 | 4 | static::valueToString($value), |
|
| 637 | 4 | static::valueToString($limit) |
|
| 638 | )); |
||
| 639 | } |
||
| 640 | 4 | } |
|
| 641 | |||
| 642 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 643 | { |
||
| 644 | 12 | if ($value > $limit) { |
|
| 645 | 4 | static::reportInvalidArgument(sprintf( |
|
| 646 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
| 647 | 4 | static::valueToString($value), |
|
| 648 | 4 | static::valueToString($limit) |
|
| 649 | )); |
||
| 650 | } |
||
| 651 | 8 | } |
|
| 652 | |||
| 653 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 654 | { |
||
| 655 | 16 | if ($value < $min || $value > $max) { |
|
| 656 | 8 | static::reportInvalidArgument(sprintf( |
|
| 657 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 658 | 8 | static::valueToString($value), |
|
| 659 | 8 | static::valueToString($min), |
|
| 660 | 8 | static::valueToString($max) |
|
| 661 | )); |
||
| 662 | } |
||
| 663 | 8 | } |
|
| 664 | |||
| 665 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 666 | { |
||
| 667 | 8 | if (!in_array($value, $values, true)) { |
|
| 668 | 4 | static::reportInvalidArgument(sprintf( |
|
| 669 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
| 670 | 4 | static::valueToString($value), |
|
| 671 | 4 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
|
| 672 | )); |
||
| 673 | } |
||
| 674 | 4 | } |
|
| 675 | |||
| 676 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 686 | |||
| 687 | 20 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 697 | |||
| 698 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 707 | |||
| 708 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 709 | { |
||
| 710 | 12 | if (0 !== strpos($value, $prefix)) { |
|
| 711 | 8 | static::reportInvalidArgument(sprintf( |
|
| 712 | 8 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
| 713 | 8 | static::valueToString($value), |
|
| 714 | 8 | static::valueToString($prefix) |
|
| 715 | )); |
||
| 716 | } |
||
| 717 | 4 | } |
|
| 718 | |||
| 719 | 12 | public static function startsWithLetter($value, $message = '') |
|
| 720 | { |
||
| 721 | 12 | $valid = isset($value[0]); |
|
| 722 | |||
| 723 | 12 | if ($valid) { |
|
| 724 | 8 | $locale = setlocale(LC_CTYPE, 0); |
|
| 725 | 8 | setlocale(LC_CTYPE, 'C'); |
|
| 726 | 8 | $valid = ctype_alpha($value[0]); |
|
| 727 | 8 | setlocale(LC_CTYPE, $locale); |
|
| 728 | } |
||
| 729 | |||
| 730 | 12 | if (!$valid) { |
|
| 731 | 8 | static::reportInvalidArgument(sprintf( |
|
| 732 | 8 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
| 733 | 8 | static::valueToString($value) |
|
| 734 | )); |
||
| 735 | } |
||
| 736 | 4 | } |
|
| 737 | |||
| 738 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 739 | { |
||
| 740 | 12 | if ($suffix !== substr($value, -static::strlen($suffix))) { |
|
| 741 | 8 | static::reportInvalidArgument(sprintf( |
|
| 742 | 8 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
| 743 | 8 | static::valueToString($value), |
|
| 744 | 8 | static::valueToString($suffix) |
|
| 745 | )); |
||
| 746 | } |
||
| 747 | 4 | } |
|
| 748 | |||
| 749 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 758 | |||
| 759 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
| 760 | { |
||
| 761 | 12 | if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 762 | 4 | static::reportInvalidArgument(sprintf( |
|
| 763 | 4 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
|
| 764 | 4 | static::valueToString($value), |
|
| 765 | 4 | static::valueToString($pattern), |
|
| 766 | 4 | $matches[0][1] |
|
| 767 | )); |
||
| 768 | } |
||
| 769 | 8 | } |
|
| 770 | |||
| 771 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
| 772 | { |
||
| 773 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
| 774 | 12 | setlocale(LC_CTYPE, 'C'); |
|
| 775 | 12 | $valid = !ctype_alpha($value); |
|
| 776 | 12 | setlocale(LC_CTYPE, $locale); |
|
| 777 | |||
| 778 | 12 | if ($valid) { |
|
| 779 | 8 | static::reportInvalidArgument(sprintf( |
|
| 780 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
| 781 | 8 | static::valueToString($value) |
|
| 782 | )); |
||
| 783 | } |
||
| 784 | 4 | } |
|
| 785 | |||
| 786 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 787 | { |
||
| 788 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
| 789 | 12 | setlocale(LC_CTYPE, 'C'); |
|
| 790 | 12 | $valid = !ctype_digit($value); |
|
| 791 | 12 | setlocale(LC_CTYPE, $locale); |
|
| 792 | |||
| 793 | 12 | if ($valid) { |
|
| 794 | 8 | static::reportInvalidArgument(sprintf( |
|
| 795 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
| 796 | 8 | static::valueToString($value) |
|
| 797 | )); |
||
| 798 | } |
||
| 799 | 4 | } |
|
| 800 | |||
| 801 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 802 | { |
||
| 803 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
| 804 | 12 | setlocale(LC_CTYPE, 'C'); |
|
| 805 | 12 | $valid = !ctype_alnum($value); |
|
| 806 | 12 | setlocale(LC_CTYPE, $locale); |
|
| 807 | |||
| 808 | 12 | if ($valid) { |
|
| 809 | 8 | static::reportInvalidArgument(sprintf( |
|
| 810 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
| 811 | 8 | static::valueToString($value) |
|
| 812 | )); |
||
| 813 | } |
||
| 814 | 4 | } |
|
| 815 | |||
| 816 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 817 | { |
||
| 818 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
| 819 | 16 | setlocale(LC_CTYPE, 'C'); |
|
| 820 | 16 | $valid = !ctype_lower($value); |
|
| 821 | 16 | setlocale(LC_CTYPE, $locale); |
|
| 822 | |||
| 823 | 16 | if ($valid) { |
|
| 824 | 12 | static::reportInvalidArgument(sprintf( |
|
| 825 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
| 826 | 12 | static::valueToString($value) |
|
| 827 | )); |
||
| 828 | } |
||
| 829 | 4 | } |
|
| 830 | |||
| 831 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 832 | { |
||
| 833 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
| 834 | 16 | setlocale(LC_CTYPE, 'C'); |
|
| 835 | 16 | $valid = !ctype_upper($value); |
|
| 836 | 16 | setlocale(LC_CTYPE, $locale); |
|
| 837 | |||
| 838 | 16 | if ($valid) { |
|
| 839 | 12 | static::reportInvalidArgument(sprintf( |
|
| 840 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
| 841 | 12 | static::valueToString($value) |
|
| 842 | )); |
||
| 843 | } |
||
| 844 | 4 | } |
|
| 845 | |||
| 846 | 24 | public static function length($value, $length, $message = '') |
|
| 856 | |||
| 857 | 24 | public static function minLength($value, $min, $message = '') |
|
| 867 | |||
| 868 | 24 | public static function maxLength($value, $max, $message = '') |
|
| 878 | |||
| 879 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 880 | { |
||
| 881 | 40 | $length = static::strlen($value); |
|
| 882 | |||
| 883 | 40 | if ($length < $min || $length > $max) { |
|
| 884 | 16 | static::reportInvalidArgument(sprintf( |
|
| 885 | 16 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
| 886 | 16 | static::valueToString($value), |
|
| 887 | $min, |
||
| 888 | $max |
||
| 889 | )); |
||
| 890 | } |
||
| 891 | 24 | } |
|
| 892 | |||
| 893 | 36 | public static function fileExists($value, $message = '') |
|
| 894 | { |
||
| 895 | 36 | static::string($value); |
|
| 896 | |||
| 897 | 36 | if (!file_exists($value)) { |
|
| 898 | 12 | static::reportInvalidArgument(sprintf( |
|
| 899 | 12 | $message ?: 'The file %s does not exist.', |
|
| 900 | 12 | static::valueToString($value) |
|
| 901 | )); |
||
| 902 | } |
||
| 903 | 24 | } |
|
| 904 | |||
| 905 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 916 | |||
| 917 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 918 | { |
||
| 919 | 12 | static::fileExists($value, $message); |
|
| 920 | |||
| 921 | 8 | if (!is_dir($value)) { |
|
| 922 | 4 | static::reportInvalidArgument(sprintf( |
|
| 923 | 4 | $message ?: 'The path %s is no directory.', |
|
| 924 | 4 | static::valueToString($value) |
|
| 925 | )); |
||
| 926 | } |
||
| 927 | 4 | } |
|
| 928 | |||
| 929 | public static function readable($value, $message = '') |
||
| 930 | { |
||
| 931 | if (!is_readable($value)) { |
||
| 932 | static::reportInvalidArgument(sprintf( |
||
| 933 | $message ?: 'The path %s is not readable.', |
||
| 934 | static::valueToString($value) |
||
| 935 | )); |
||
| 936 | } |
||
| 937 | } |
||
| 938 | |||
| 939 | public static function writable($value, $message = '') |
||
| 940 | { |
||
| 941 | if (!is_writable($value)) { |
||
| 942 | static::reportInvalidArgument(sprintf( |
||
| 943 | $message ?: 'The path %s is not writable.', |
||
| 944 | static::valueToString($value) |
||
| 945 | )); |
||
| 946 | } |
||
| 947 | } |
||
| 948 | |||
| 949 | 8 | public static function classExists($value, $message = '') |
|
| 958 | |||
| 959 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 960 | { |
||
| 961 | 8 | if (!is_subclass_of($value, $class)) { |
|
| 962 | 4 | static::reportInvalidArgument(sprintf( |
|
| 963 | 4 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
| 964 | 4 | static::valueToString($value), |
|
| 965 | 4 | static::valueToString($class) |
|
| 966 | )); |
||
| 967 | } |
||
| 968 | 4 | } |
|
| 969 | |||
| 970 | 8 | public static function interfaceExists($value, $message = '') |
|
| 979 | |||
| 980 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 981 | { |
||
| 982 | 8 | if (!in_array($interface, class_implements($value))) { |
|
| 983 | 4 | static::reportInvalidArgument(sprintf( |
|
| 984 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
| 985 | 4 | static::valueToString($value), |
|
| 986 | 4 | static::valueToString($interface) |
|
| 987 | )); |
||
| 988 | } |
||
| 989 | 4 | } |
|
| 990 | |||
| 991 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 992 | { |
||
| 993 | 12 | if (!property_exists($classOrObject, $property)) { |
|
| 994 | 4 | static::reportInvalidArgument(sprintf( |
|
| 995 | 4 | $message ?: 'Expected the property %s to exist.', |
|
| 996 | 4 | static::valueToString($property) |
|
| 997 | )); |
||
| 998 | } |
||
| 999 | 8 | } |
|
| 1000 | |||
| 1001 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 1002 | { |
||
| 1003 | 12 | if (property_exists($classOrObject, $property)) { |
|
| 1004 | 8 | static::reportInvalidArgument(sprintf( |
|
| 1005 | 8 | $message ?: 'Expected the property %s to not exist.', |
|
| 1006 | 8 | static::valueToString($property) |
|
| 1007 | )); |
||
| 1008 | } |
||
| 1009 | 4 | } |
|
| 1010 | |||
| 1011 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 1012 | { |
||
| 1013 | 27 | if (!method_exists($classOrObject, $method)) { |
|
| 1014 | 19 | static::reportInvalidArgument(sprintf( |
|
| 1015 | 19 | $message ?: 'Expected the method %s to exist.', |
|
| 1016 | 19 | static::valueToString($method) |
|
| 1017 | )); |
||
| 1018 | } |
||
| 1019 | 8 | } |
|
| 1020 | |||
| 1021 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 1022 | { |
||
| 1023 | 27 | if (method_exists($classOrObject, $method)) { |
|
| 1024 | 8 | static::reportInvalidArgument(sprintf( |
|
| 1025 | 8 | $message ?: 'Expected the method %s to not exist.', |
|
| 1026 | 8 | static::valueToString($method) |
|
| 1027 | )); |
||
| 1028 | } |
||
| 1029 | 19 | } |
|
| 1030 | |||
| 1031 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 1040 | |||
| 1041 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 1042 | { |
||
| 1043 | 12 | if (isset($array[$key]) || array_key_exists($key, $array)) { |
|
| 1044 | 8 | static::reportInvalidArgument(sprintf( |
|
| 1045 | 8 | $message ?: 'Expected the key %s to not exist.', |
|
| 1046 | 8 | static::valueToString($key) |
|
| 1047 | )); |
||
| 1048 | } |
||
| 1049 | 4 | } |
|
| 1050 | |||
| 1051 | 8 | public static function count($array, $number, $message = '') |
|
| 1052 | { |
||
| 1053 | 8 | static::eq( |
|
| 1054 | 8 | count($array), |
|
| 1055 | $number, |
||
| 1056 | 8 | $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
|
| 1057 | ); |
||
| 1058 | 4 | } |
|
| 1059 | |||
| 1060 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 1061 | { |
||
| 1062 | 12 | if (count($array) < $min) { |
|
| 1063 | 4 | static::reportInvalidArgument(sprintf( |
|
| 1064 | 4 | $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
|
| 1065 | 4 | count($array), |
|
| 1066 | $min |
||
| 1067 | )); |
||
| 1068 | } |
||
| 1069 | 8 | } |
|
| 1070 | |||
| 1071 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 1072 | { |
||
| 1073 | 12 | if (count($array) > $max) { |
|
| 1074 | 4 | static::reportInvalidArgument(sprintf( |
|
| 1075 | 4 | $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
|
| 1076 | 4 | count($array), |
|
| 1077 | $max |
||
| 1078 | )); |
||
| 1079 | } |
||
| 1080 | 8 | } |
|
| 1081 | |||
| 1082 | 12 | public static function countBetween($array, $min, $max, $message = '') |
|
| 1083 | { |
||
| 1084 | 12 | $count = count($array); |
|
| 1085 | |||
| 1086 | 12 | if ($count < $min || $count > $max) { |
|
| 1095 | |||
| 1096 | 16 | public static function isList($array, $message = '') |
|
| 1104 | |||
| 1105 | 16 | public static function isMap($array, $message = '') |
|
| 1119 | |||
| 1120 | 48 | public static function uuid($value, $message = '') |
|
| 1137 | |||
| 1138 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 1164 | |||
| 1165 | 1185 | public static function __callStatic($name, $arguments) |
|
| 1193 | |||
| 1194 | 542 | protected static function valueToString($value) |
|
| 1230 | |||
| 1231 | 209 | protected static function typeToString($value) |
|
| 1235 | |||
| 1236 | 124 | protected static function strlen($value) |
|
| 1248 | |||
| 1249 | 786 | protected static function reportInvalidArgument($message) |
|
| 1253 | |||
| 1254 | private function __construct() |
||
| 1257 | } |
||
| 1258 |