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 |
||
| 24 | class Assert |
||
| 25 | { |
||
| 26 | const INVALID_FLOAT = 9; |
||
| 27 | const INVALID_INTEGER = 10; |
||
| 28 | const INVALID_DIGIT = 11; |
||
| 29 | const INVALID_INTEGERISH = 12; |
||
| 30 | const INVALID_BOOLEAN = 13; |
||
| 31 | const VALUE_EMPTY = 14; |
||
| 32 | const VALUE_NULL = 15; |
||
| 33 | const INVALID_STRING = 16; |
||
| 34 | const INVALID_REGEX = 17; |
||
| 35 | const INVALID_MIN_LENGTH = 18; |
||
| 36 | const INVALID_MAX_LENGTH = 19; |
||
| 37 | const INVALID_STRING_START = 20; |
||
| 38 | const INVALID_STRING_CONTAINS = 21; |
||
| 39 | const INVALID_CHOICE = 22; |
||
| 40 | const INVALID_NUMERIC = 23; |
||
| 41 | const INVALID_ARRAY = 24; |
||
| 42 | const INVALID_KEY_EXISTS = 26; |
||
| 43 | const INVALID_NOT_BLANK = 27; |
||
| 44 | const INVALID_INSTANCE_OF = 28; |
||
| 45 | const INVALID_SUBCLASS_OF = 29; |
||
| 46 | const INVALID_RANGE = 30; |
||
| 47 | const INVALID_ALNUM = 31; |
||
| 48 | const INVALID_TRUE = 32; |
||
| 49 | const INVALID_EQ = 33; |
||
| 50 | const INVALID_SAME = 34; |
||
| 51 | const INVALID_MIN = 35; |
||
| 52 | const INVALID_MAX = 36; |
||
| 53 | const INVALID_LENGTH = 37; |
||
| 54 | const INVALID_FALSE = 38; |
||
| 55 | const INVALID_STRING_END = 39; |
||
| 56 | const INVALID_UUID = 40; |
||
| 57 | const INVALID_COUNT = 41; |
||
| 58 | const INVALID_NOT_EQ = 42; |
||
| 59 | const INVALID_NOT_SAME = 43; |
||
| 60 | const INVALID_TRAVERSABLE = 44; |
||
| 61 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 62 | const INVALID_KEY_ISSET = 46; |
||
| 63 | const INVALID_DIRECTORY = 101; |
||
| 64 | const INVALID_FILE = 102; |
||
| 65 | const INVALID_READABLE = 103; |
||
| 66 | const INVALID_WRITEABLE = 104; |
||
| 67 | const INVALID_CLASS = 105; |
||
| 68 | const INVALID_EMAIL = 201; |
||
| 69 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 70 | const INVALID_URL = 203; |
||
| 71 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 72 | const VALUE_NOT_EMPTY = 205; |
||
| 73 | const INVALID_JSON_STRING = 206; |
||
| 74 | const INVALID_OBJECT = 207; |
||
| 75 | const INVALID_METHOD = 208; |
||
| 76 | const INVALID_SCALAR = 209; |
||
| 77 | const INVALID_DATE = 210; |
||
| 78 | const INVALID_CALLABLE = 211; |
||
| 79 | const INVALID_KEYS_EXIST = 300; |
||
| 80 | const INVALID_PROPERTY_EXISTS = 301; |
||
| 81 | const INVALID_PROPERTIES_EXIST = 302; |
||
| 82 | const INVALID_UTF8 = 303; |
||
| 83 | const INVALID_DOMAIN_NAME = 304; |
||
| 84 | const INVALID_NOT_FALSE = 305; |
||
| 85 | const INVALID_FILE_OR_DIR = 306; |
||
| 86 | const INVALID_ASCII = 307; |
||
| 87 | const INVALID_NOT_REGEX = 308; |
||
| 88 | const INVALID_GREATER_THAN = 309; |
||
| 89 | const INVALID_LESS_THAN = 310; |
||
| 90 | const INVALID_GREATER_THAN_OR_EQ = 311; |
||
| 91 | const INVALID_LESS_THAN_OR_EQ = 312; |
||
| 92 | const INVALID_IP_ADDRESS = 313; |
||
| 93 | |||
| 94 | const EMERGENCY = 'emergency'; |
||
| 95 | const ALERT = 'alert'; |
||
| 96 | const CRITICAL = 'critical'; |
||
| 97 | const ERROR = 'error'; |
||
| 98 | const WARNING = 'warning'; |
||
| 99 | const NOTICE = 'notice'; |
||
| 100 | const INFO = 'info'; |
||
| 101 | const DEBUG = 'debug'; |
||
| 102 | |||
| 103 | /** @var bool */ |
||
| 104 | protected $nullOr = false; |
||
| 105 | |||
| 106 | /** @var bool */ |
||
| 107 | protected $emptyOr = false; |
||
| 108 | |||
| 109 | /** @var mixed */ |
||
| 110 | protected $value = null; |
||
| 111 | |||
| 112 | /** @var bool */ |
||
| 113 | protected $all = false; |
||
| 114 | |||
| 115 | /** @var null|string */ |
||
| 116 | protected $propertyPath = null; |
||
| 117 | |||
| 118 | /** @var null|string */ |
||
| 119 | protected $level = 'critical'; |
||
| 120 | |||
| 121 | /** @var int */ |
||
| 122 | protected $overrideCode = null; |
||
| 123 | |||
| 124 | /** @var string */ |
||
| 125 | protected $overrideError = ''; |
||
| 126 | /** |
||
| 127 | * Exception to throw when an assertion failed. |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $exceptionClass = 'Terah\Assert\AssertionFailedException'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param mixed $value |
||
| 135 | */ |
||
| 136 | public function __construct($value) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $value |
||
| 143 | * @return Assert |
||
| 144 | */ |
||
| 145 | public static function that($value) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param mixed $value |
||
| 152 | * @return Assert |
||
| 153 | */ |
||
| 154 | public function reset($value) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param mixed $value |
||
| 161 | * @return Assert |
||
| 162 | */ |
||
| 163 | public function value($value) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param bool $nullOr |
||
| 171 | * @return Assert |
||
| 172 | */ |
||
| 173 | public function nullOr($nullOr = true) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param bool $emptyOr |
||
| 181 | * @return Assert |
||
| 182 | */ |
||
| 183 | public function emptyOr($emptyOr = true) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param bool $all |
||
| 191 | * @return Assert |
||
| 192 | */ |
||
| 193 | public function all($all = true) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Helper method that handles building the assertion failure exceptions. |
||
| 201 | * They are returned from this method so that the stack trace still shows |
||
| 202 | * the assertions method. |
||
| 203 | * |
||
| 204 | * @param string $message |
||
| 205 | * @param int $code |
||
| 206 | * @param string $propertyPath |
||
| 207 | * @param array $constraints |
||
| 208 | * @param string $level |
||
| 209 | * @return AssertionFailedException |
||
| 210 | */ |
||
| 211 | protected function createException($message, $code, $propertyPath, array $constraints = [], $level=null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $exceptionClass |
||
| 222 | * @return Assert |
||
| 223 | */ |
||
| 224 | public function setExceptionClass($exceptionClass) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param int $code |
||
| 232 | * @return Assert |
||
| 233 | */ |
||
| 234 | public function code($code) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param int $level |
||
| 242 | * @return Assert |
||
| 243 | */ |
||
| 244 | public function level($level) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $error |
||
| 252 | * @return Assert |
||
| 253 | */ |
||
| 254 | public function error($error) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $name |
||
| 262 | * @return Assert |
||
| 263 | */ |
||
| 264 | public function name($name) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Assert that two values are equal (using == ). |
||
| 272 | * |
||
| 273 | * @param mixed $value2 |
||
| 274 | * @param string|null $message |
||
| 275 | * @param string|null $propertyPath |
||
| 276 | * @return Assert |
||
| 277 | * @throws AssertionFailedException |
||
| 278 | */ |
||
| 279 | public function eq($value2, $message = null, $propertyPath = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * |
||
| 300 | * @param mixed $value2 |
||
| 301 | * @param string|null $message |
||
| 302 | * @param string|null $propertyPath |
||
| 303 | * @return Assert |
||
| 304 | * @throws AssertionFailedException |
||
| 305 | */ |
||
| 306 | public function greaterThan($value2, $message = null, $propertyPath = null) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * |
||
| 327 | * @param mixed $value2 |
||
| 328 | * @param string|null $message |
||
| 329 | * @param string|null $propertyPath |
||
| 330 | * @return Assert |
||
| 331 | * @throws AssertionFailedException |
||
| 332 | */ |
||
| 333 | public function greaterThanOrEq($value2, $message = null, $propertyPath = null) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * |
||
| 354 | * @param mixed $value2 |
||
| 355 | * @param string|null $message |
||
| 356 | * @param string|null $propertyPath |
||
| 357 | * @return Assert |
||
| 358 | * @throws AssertionFailedException |
||
| 359 | */ |
||
| 360 | public function lessThan($value2, $message = null, $propertyPath = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * |
||
| 381 | * @param mixed $value2 |
||
| 382 | * @param string|null $message |
||
| 383 | * @param string|null $propertyPath |
||
| 384 | * @return Assert |
||
| 385 | * @throws AssertionFailedException |
||
| 386 | */ |
||
| 387 | public function lessThanOrEq($value2, $message = null, $propertyPath = null) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Assert that two values are the same (using ===). |
||
| 408 | * |
||
| 409 | * @param mixed $value2 |
||
| 410 | * @param string|null $message |
||
| 411 | * @param string|null $propertyPath |
||
| 412 | * @return Assert |
||
| 413 | * @throws AssertionFailedException |
||
| 414 | */ |
||
| 415 | public function same($value2, $message = null, $propertyPath = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Assert that two values are not equal (using == ). |
||
| 436 | * |
||
| 437 | * @param mixed $value2 |
||
| 438 | * @param string|null $message |
||
| 439 | * @param string|null $propertyPath |
||
| 440 | * @return Assert |
||
| 441 | * @throws AssertionFailedException |
||
| 442 | */ |
||
| 443 | public function notEq($value2, $message = null, $propertyPath = null) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string|null $message |
||
| 464 | * @param string|null $propertyPath |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | * @throws AssertionFailedException |
||
| 468 | */ |
||
| 469 | public function isCallable($message = null, $propertyPath = null) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Assert that two values are not the same (using === ). |
||
| 489 | * |
||
| 490 | * @param mixed $value2 |
||
| 491 | * @param string|null $message |
||
| 492 | * @param string|null $propertyPath |
||
| 493 | * @return Assert |
||
| 494 | * @throws AssertionFailedException |
||
| 495 | */ |
||
| 496 | public function notSame($value2, $message = null, $propertyPath = null) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param string|null $message |
||
| 517 | * @param string|null $propertyPath |
||
| 518 | * @return Assert |
||
| 519 | * @throws AssertionFailedException |
||
| 520 | */ |
||
| 521 | public function id($message = null, $propertyPath = null) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string|null $message |
||
| 530 | * @param string|null $propertyPath |
||
| 531 | * @return Assert |
||
| 532 | * @throws AssertionFailedException |
||
| 533 | */ |
||
| 534 | public function flag($message = null, $propertyPath = null) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param string|null $message |
||
| 543 | * @param string|null $propertyPath |
||
| 544 | * @return Assert |
||
| 545 | * @throws AssertionFailedException |
||
| 546 | */ |
||
| 547 | public function status($message = null, $propertyPath = null) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param string|null $message |
||
| 556 | * @param string|null $propertyPath |
||
| 557 | * @return Assert |
||
| 558 | */ |
||
| 559 | public function nullOrId($message = null, $propertyPath = null) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @param string|null $message |
||
| 566 | * @param string|null $propertyPath |
||
| 567 | * @return Assert |
||
| 568 | */ |
||
| 569 | public function allIds($message = null, $propertyPath = null) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param string|null $message |
||
| 576 | * @param string|null $propertyPath |
||
| 577 | * @return Assert |
||
| 578 | * @throws AssertionFailedException |
||
| 579 | */ |
||
| 580 | public function int($message = null, $propertyPath = null) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Assert that value is a php integer. |
||
| 587 | * |
||
| 588 | * @param string|null $message |
||
| 589 | * @param string|null $propertyPath |
||
| 590 | * @return Assert |
||
| 591 | * @throws AssertionFailedException |
||
| 592 | */ |
||
| 593 | public function integer($message = null, $propertyPath = null) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Assert that value is a php float. |
||
| 613 | * |
||
| 614 | * @param string|null $message |
||
| 615 | * @param string|null $propertyPath |
||
| 616 | * @return Assert |
||
| 617 | * @throws AssertionFailedException |
||
| 618 | */ |
||
| 619 | public function float($message = null, $propertyPath = null) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Validates if an integer or integerish is a digit. |
||
| 639 | * |
||
| 640 | * @param string|null $message |
||
| 641 | * @param string|null $propertyPath |
||
| 642 | * @return Assert |
||
| 643 | * @throws AssertionFailedException |
||
| 644 | */ |
||
| 645 | public function digit($message = null, $propertyPath = null) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Validates if an string is a date . |
||
| 665 | * |
||
| 666 | * @param string|null $message |
||
| 667 | * @param string|null $propertyPath |
||
| 668 | * @return Assert |
||
| 669 | * @throws AssertionFailedException |
||
| 670 | */ |
||
| 671 | public function date($message = null, $propertyPath = null) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Assert that value is a php integer'ish. |
||
| 692 | * |
||
| 693 | * @param string|null $message |
||
| 694 | * @param string|null $propertyPath |
||
| 695 | * @return Assert |
||
| 696 | * @throws AssertionFailedException |
||
| 697 | */ |
||
| 698 | public function integerish($message = null, $propertyPath = null) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Assert that value is php boolean |
||
| 718 | * |
||
| 719 | * @param string|null $message |
||
| 720 | * @param string|null $propertyPath |
||
| 721 | * @return Assert |
||
| 722 | * @throws AssertionFailedException |
||
| 723 | */ |
||
| 724 | public function boolean($message = null, $propertyPath = null) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Assert that value is a PHP scalar |
||
| 744 | * |
||
| 745 | * @param string|null $message |
||
| 746 | * @param string|null $propertyPath |
||
| 747 | * @return Assert |
||
| 748 | * @throws AssertionFailedException |
||
| 749 | */ |
||
| 750 | public function scalar($message = null, $propertyPath = null) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Assert that value is not empty |
||
| 770 | * |
||
| 771 | * @param string|null $message |
||
| 772 | * @param string|null $propertyPath |
||
| 773 | * @return Assert |
||
| 774 | * @throws AssertionFailedException |
||
| 775 | */ |
||
| 776 | View Code Duplication | public function notEmpty($message = null, $propertyPath = null) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Assert that value is empty |
||
| 796 | * |
||
| 797 | * @param string|null $message |
||
| 798 | * @param string|null $propertyPath |
||
| 799 | * @return Assert |
||
| 800 | * @throws AssertionFailedException |
||
| 801 | */ |
||
| 802 | public function noContent($message = null, $propertyPath = null) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Assert that value is not null |
||
| 822 | * |
||
| 823 | * @param string|null $message |
||
| 824 | * @param string|null $propertyPath |
||
| 825 | * @return Assert |
||
| 826 | * @throws AssertionFailedException |
||
| 827 | */ |
||
| 828 | public function notNull($message = null, $propertyPath = null) |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Assert that value is a string |
||
| 848 | * |
||
| 849 | * @param string|null $message |
||
| 850 | * @param string|null $propertyPath |
||
| 851 | * @return Assert |
||
| 852 | * @throws AssertionFailedException |
||
| 853 | */ |
||
| 854 | public function string($message = null, $propertyPath = null) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Assert that value matches a regex |
||
| 875 | * |
||
| 876 | * @param string $pattern |
||
| 877 | * @param string|null $message |
||
| 878 | * @param string|null $propertyPath |
||
| 879 | * @return Assert |
||
| 880 | * @throws AssertionFailedException |
||
| 881 | */ |
||
| 882 | public function regex($pattern, $message=null, $propertyPath=null) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @param string $message |
||
| 903 | * @param string $propertyPath |
||
| 904 | * @return $this |
||
| 905 | * @throws AssertionFailedException |
||
| 906 | */ |
||
| 907 | public function ipAddress($message = null, $propertyPath = null) |
||
| 926 | |||
| 927 | /** |
||
| 928 | * @param string $pattern |
||
| 929 | * @param string|null $message |
||
| 930 | * @param string|null $propertyPath |
||
| 931 | * @return $this |
||
| 932 | * @throws AssertionFailedException |
||
| 933 | */ |
||
| 934 | public function notRegex($pattern, $message = null, $propertyPath = null) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Assert that string has a given length. |
||
| 955 | * |
||
| 956 | * @param int $length |
||
| 957 | * @param string|null $message |
||
| 958 | * @param string|null $propertyPath |
||
| 959 | * @param string $encoding |
||
| 960 | * @return Assert |
||
| 961 | * @throws AssertionFailedException |
||
| 962 | */ |
||
| 963 | public function length($length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Assert that a string is at least $minLength chars long. |
||
| 987 | * |
||
| 988 | * @param int $minLength |
||
| 989 | * @param string|null $message |
||
| 990 | * @param string|null $propertyPath |
||
| 991 | * @param string $encoding |
||
| 992 | * @return Assert |
||
| 993 | * @throws AssertionFailedException |
||
| 994 | */ |
||
| 995 | public function minLength($minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Assert that string value is not longer than $maxLength chars. |
||
| 1020 | * |
||
| 1021 | * @param integer $maxLength |
||
| 1022 | * @param string|null $message |
||
| 1023 | * @param string|null $propertyPath |
||
| 1024 | * @param string $encoding |
||
| 1025 | * @return Assert |
||
| 1026 | * @throws AssertionFailedException |
||
| 1027 | */ |
||
| 1028 | public function maxLength($maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Assert that string length is between min,max lengths. |
||
| 1052 | * |
||
| 1053 | * @param integer $minLength |
||
| 1054 | * @param integer $maxLength |
||
| 1055 | * @param string|null $message |
||
| 1056 | * @param string|null $propertyPath |
||
| 1057 | * @param string $encoding |
||
| 1058 | * @return Assert |
||
| 1059 | * @throws AssertionFailedException |
||
| 1060 | */ |
||
| 1061 | public function betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Assert that string starts with a sequence of chars. |
||
| 1098 | * |
||
| 1099 | * @param string $needle |
||
| 1100 | * @param string|null $message |
||
| 1101 | * @param string|null $propertyPath |
||
| 1102 | * @param string $encoding |
||
| 1103 | * @return Assert |
||
| 1104 | * @throws AssertionFailedException |
||
| 1105 | */ |
||
| 1106 | View Code Duplication | public function startsWith($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
|
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Assert that string ends with a sequence of chars. |
||
| 1129 | * |
||
| 1130 | * @param string $needle |
||
| 1131 | * @param string|null $message |
||
| 1132 | * @param string|null $propertyPath |
||
| 1133 | * @param string $encoding |
||
| 1134 | * @return Assert |
||
| 1135 | * @throws AssertionFailedException |
||
| 1136 | */ |
||
| 1137 | public function endsWith($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * Assert that string contains a sequence of chars. |
||
| 1161 | * |
||
| 1162 | * @param string $needle |
||
| 1163 | * @param string|null $message |
||
| 1164 | * @param string|null $propertyPath |
||
| 1165 | * @param string $encoding |
||
| 1166 | * @return Assert |
||
| 1167 | * @throws AssertionFailedException |
||
| 1168 | */ |
||
| 1169 | View Code Duplication | public function contains($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Assert that value is in array of choices. |
||
| 1192 | * |
||
| 1193 | * @param array $choices |
||
| 1194 | * @param string|null $message |
||
| 1195 | * @param string|null $propertyPath |
||
| 1196 | * @return Assert |
||
| 1197 | * @throws AssertionFailedException |
||
| 1198 | */ |
||
| 1199 | public function choice(array $choices, $message = null, $propertyPath = null) |
||
| 1217 | |||
| 1218 | /** |
||
| 1219 | * Alias of {@see choice()} |
||
| 1220 | * |
||
| 1221 | * @throws AssertionFailedException |
||
| 1222 | * |
||
| 1223 | * @param array $choices |
||
| 1224 | * @param string|null $message |
||
| 1225 | * @param string|null $propertyPath |
||
| 1226 | * @return $this |
||
| 1227 | */ |
||
| 1228 | public function inArray(array $choices, $message = null, $propertyPath = null) |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * Assert that value is numeric. |
||
| 1240 | * |
||
| 1241 | * @param string|null $message |
||
| 1242 | * @param string|null $propertyPath |
||
| 1243 | * @return Assert |
||
| 1244 | * @throws AssertionFailedException |
||
| 1245 | */ |
||
| 1246 | public function numeric($message = null, $propertyPath = null) |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * @param string|null $message |
||
| 1266 | * @param string|null $propertyPath |
||
| 1267 | * @return Assert |
||
| 1268 | * @throws AssertionFailedException |
||
| 1269 | */ |
||
| 1270 | public function nonEmptyArray($message = null, $propertyPath = null) |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * @param string|null $message |
||
| 1278 | * @param string|null $propertyPath |
||
| 1279 | * @return Assert |
||
| 1280 | * @throws AssertionFailedException |
||
| 1281 | */ |
||
| 1282 | public function nonEmptyInt($message = null, $propertyPath = null) |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * @param string|null $message |
||
| 1290 | * @param string|null $propertyPath |
||
| 1291 | * @return Assert |
||
| 1292 | * @throws AssertionFailedException |
||
| 1293 | */ |
||
| 1294 | public function nonEmptyString($message = null, $propertyPath = null) |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Assert that value is an array. |
||
| 1302 | * |
||
| 1303 | * @param string|null $message |
||
| 1304 | * @param string|null $propertyPath |
||
| 1305 | * @return Assert |
||
| 1306 | * @throws AssertionFailedException |
||
| 1307 | */ |
||
| 1308 | public function isArray($message = null, $propertyPath = null) |
||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Assert that value is an array or a traversable object. |
||
| 1328 | * |
||
| 1329 | * @param string|null $message |
||
| 1330 | * @param string|null $propertyPath |
||
| 1331 | * @return Assert |
||
| 1332 | * @throws AssertionFailedException |
||
| 1333 | */ |
||
| 1334 | public function isTraversable($message = null, $propertyPath = null) |
||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Assert that value is an array or an array-accessible object. |
||
| 1354 | * |
||
| 1355 | * @param string|null $message |
||
| 1356 | * @param string|null $propertyPath |
||
| 1357 | * @return Assert |
||
| 1358 | * @throws AssertionFailedException |
||
| 1359 | */ |
||
| 1360 | public function isArrayAccessible($message = null, $propertyPath = null) |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Assert that key exists in an array |
||
| 1380 | * |
||
| 1381 | * @param string|integer $key |
||
| 1382 | * @param string|null $message |
||
| 1383 | * @param string|null $propertyPath |
||
| 1384 | * @return Assert |
||
| 1385 | * @throws AssertionFailedException |
||
| 1386 | */ |
||
| 1387 | public function keyExists($key, $message = null, $propertyPath = null) |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Assert that keys exist in array |
||
| 1408 | * |
||
| 1409 | * @param array $keys |
||
| 1410 | * @param string|null $message |
||
| 1411 | * @param string|null $propertyPath |
||
| 1412 | * @return Assert |
||
| 1413 | * @throws AssertionFailedException |
||
| 1414 | */ |
||
| 1415 | public function keysExist($keys, $message = null, $propertyPath = null) |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Assert that property exists in array |
||
| 1439 | * |
||
| 1440 | * @param string|integer $key |
||
| 1441 | * @param string|null $message |
||
| 1442 | * @param string|null $propertyPath |
||
| 1443 | * @return Assert |
||
| 1444 | * @throws AssertionFailedException |
||
| 1445 | */ |
||
| 1446 | public function propertyExists($key, $message = null, $propertyPath = null) |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Assert that properties exists in array |
||
| 1467 | * |
||
| 1468 | * @param array $keys |
||
| 1469 | * @param string|null $message |
||
| 1470 | * @param string|null $propertyPath |
||
| 1471 | * @return Assert |
||
| 1472 | * @throws AssertionFailedException |
||
| 1473 | */ |
||
| 1474 | public function propertiesExist(array $keys, $message = null, $propertyPath = null) |
||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Assert that string is valid utf8 |
||
| 1499 | * |
||
| 1500 | * @param string|null $message |
||
| 1501 | * @param string|null $propertyPath |
||
| 1502 | * @return Assert |
||
| 1503 | * @throws AssertionFailedException |
||
| 1504 | */ |
||
| 1505 | View Code Duplication | public function utf8($message = null, $propertyPath = null) |
|
| 1523 | |||
| 1524 | |||
| 1525 | /** |
||
| 1526 | * Assert that string is valid utf8 |
||
| 1527 | * |
||
| 1528 | * @param string|null $message |
||
| 1529 | * @param string|null $propertyPath |
||
| 1530 | * @return Assert |
||
| 1531 | * @throws AssertionFailedException |
||
| 1532 | */ |
||
| 1533 | View Code Duplication | public function ascii($message = null, $propertyPath = null) |
|
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 1554 | * |
||
| 1555 | * @param string|integer $key |
||
| 1556 | * @param string|null $message |
||
| 1557 | * @param string|null $propertyPath |
||
| 1558 | * @return Assert |
||
| 1559 | * @throws AssertionFailedException |
||
| 1560 | */ |
||
| 1561 | public function keyIsset($key, $message = null, $propertyPath = null) |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
| 1582 | * |
||
| 1583 | * @param string|integer $key |
||
| 1584 | * @param string|null $message |
||
| 1585 | * @param string|null $propertyPath |
||
| 1586 | * @return Assert |
||
| 1587 | * @throws AssertionFailedException |
||
| 1588 | */ |
||
| 1589 | public function notEmptyKey($key, $message = null, $propertyPath = null) |
||
| 1599 | |||
| 1600 | /** |
||
| 1601 | * Assert that value is not blank |
||
| 1602 | * |
||
| 1603 | * @param string|null $message |
||
| 1604 | * @param string|null $propertyPath |
||
| 1605 | * @return Assert |
||
| 1606 | * @throws AssertionFailedException |
||
| 1607 | */ |
||
| 1608 | View Code Duplication | public function notBlank($message = null, $propertyPath = null) |
|
| 1625 | |||
| 1626 | /** |
||
| 1627 | * Assert that value is instance of given class-name. |
||
| 1628 | * |
||
| 1629 | * @param string $className |
||
| 1630 | * @param string|null $message |
||
| 1631 | * @param string|null $propertyPath |
||
| 1632 | * @return Assert |
||
| 1633 | * @throws AssertionFailedException |
||
| 1634 | */ |
||
| 1635 | public function isInstanceOf($className, $message = null, $propertyPath = null) |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Assert that value is not instance of given class-name. |
||
| 1656 | * |
||
| 1657 | * @param string $className |
||
| 1658 | * @param string|null $message |
||
| 1659 | * @param string|null $propertyPath |
||
| 1660 | * @return Assert |
||
| 1661 | * @throws AssertionFailedException |
||
| 1662 | */ |
||
| 1663 | public function notIsInstanceOf($className, $message = null, $propertyPath = null) |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Assert that value is subclass of given class-name. |
||
| 1684 | * |
||
| 1685 | * @param string $className |
||
| 1686 | * @param string|null $message |
||
| 1687 | * @param string|null $propertyPath |
||
| 1688 | * @return Assert |
||
| 1689 | * @throws AssertionFailedException |
||
| 1690 | */ |
||
| 1691 | public function subclassOf($className, $message = null, $propertyPath = null) |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * Assert that value is in range of numbers. |
||
| 1712 | * |
||
| 1713 | * @param integer $minValue |
||
| 1714 | * @param integer $maxValue |
||
| 1715 | * @param string|null $message |
||
| 1716 | * @param string|null $propertyPath |
||
| 1717 | * @return Assert |
||
| 1718 | * @throws AssertionFailedException |
||
| 1719 | */ |
||
| 1720 | public function range($minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1743 | |||
| 1744 | /** |
||
| 1745 | * Assert that a value is at least as big as a given limit |
||
| 1746 | * |
||
| 1747 | * @param mixed $minValue |
||
| 1748 | * @param string|null $message |
||
| 1749 | * @param string|null $propertyPath |
||
| 1750 | * @return Assert |
||
| 1751 | * @throws AssertionFailedException |
||
| 1752 | */ |
||
| 1753 | public function min($minValue, $message = null, $propertyPath = null) |
||
| 1772 | |||
| 1773 | /** |
||
| 1774 | * Assert that a number is smaller as a given limit |
||
| 1775 | * |
||
| 1776 | * @param mixed $maxValue |
||
| 1777 | * @param string|null $message |
||
| 1778 | * @param string|null $propertyPath |
||
| 1779 | * @return Assert |
||
| 1780 | * @throws AssertionFailedException |
||
| 1781 | */ |
||
| 1782 | public function max($maxValue, $message = null, $propertyPath = null) |
||
| 1801 | |||
| 1802 | /** |
||
| 1803 | * Assert that a file exists |
||
| 1804 | * |
||
| 1805 | * @param string|null $message |
||
| 1806 | * @param string|null $propertyPath |
||
| 1807 | * @return Assert |
||
| 1808 | * @throws AssertionFailedException |
||
| 1809 | */ |
||
| 1810 | public function file($message = null, $propertyPath = null) |
||
| 1829 | |||
| 1830 | /** |
||
| 1831 | * @param string|null $message |
||
| 1832 | * @param string|null $propertyPath |
||
| 1833 | * @return $this |
||
| 1834 | * @throws AssertionFailedException |
||
| 1835 | */ |
||
| 1836 | public function fileExists($message = null, $propertyPath = null) |
||
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Assert that a directory exists |
||
| 1858 | * |
||
| 1859 | * @param string|null $message |
||
| 1860 | * @param string|null $propertyPath |
||
| 1861 | * @return Assert |
||
| 1862 | * @throws AssertionFailedException |
||
| 1863 | */ |
||
| 1864 | View Code Duplication | public function directory($message = null, $propertyPath = null) |
|
| 1882 | |||
| 1883 | /** |
||
| 1884 | * Assert that the value is something readable |
||
| 1885 | * |
||
| 1886 | * @param string|null $message |
||
| 1887 | * @param string|null $propertyPath |
||
| 1888 | * @return Assert |
||
| 1889 | * @throws AssertionFailedException |
||
| 1890 | */ |
||
| 1891 | View Code Duplication | public function readable($message = null, $propertyPath = null) |
|
| 1909 | |||
| 1910 | /** |
||
| 1911 | * Assert that the value is something writeable |
||
| 1912 | * |
||
| 1913 | * @param string|null $message |
||
| 1914 | * @param string|null $propertyPath |
||
| 1915 | * @return Assert |
||
| 1916 | * @throws AssertionFailedException |
||
| 1917 | */ |
||
| 1918 | View Code Duplication | public function writeable($message = null, $propertyPath = null) |
|
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Assert that value is an email adress (using |
||
| 1939 | * input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1940 | * |
||
| 1941 | * @param string|null $message |
||
| 1942 | * @param string|null $propertyPath |
||
| 1943 | * @return Assert |
||
| 1944 | * @throws AssertionFailedException |
||
| 1945 | */ |
||
| 1946 | public function email($message = null, $propertyPath = null) |
||
| 1978 | |||
| 1979 | /** |
||
| 1980 | * @param null $message |
||
| 1981 | * @param null $propertyPath |
||
| 1982 | * @return Assert |
||
| 1983 | * @throws AssertionFailedException |
||
| 1984 | */ |
||
| 1985 | public function emailPrefix($message = null, $propertyPath = null) |
||
| 1990 | |||
| 1991 | /** |
||
| 1992 | * Assert that value is an URL. |
||
| 1993 | * |
||
| 1994 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1995 | * |
||
| 1996 | * @param string|null $message |
||
| 1997 | * @param string|null $propertyPath |
||
| 1998 | * @return Assert |
||
| 1999 | * @throws AssertionFailedException |
||
| 2000 | * |
||
| 2001 | * |
||
| 2002 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 2003 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 2004 | */ |
||
| 2005 | public function url($message = null, $propertyPath = null) |
||
| 2039 | |||
| 2040 | /** |
||
| 2041 | * Assert that value is domain name. |
||
| 2042 | * |
||
| 2043 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 2044 | * |
||
| 2045 | * @param string|null $message |
||
| 2046 | * @param string|null $propertyPath |
||
| 2047 | * @return Assert |
||
| 2048 | * @throws AssertionFailedException |
||
| 2049 | * |
||
| 2050 | */ |
||
| 2051 | public function domainName($message = null, $propertyPath = null) |
||
| 2070 | |||
| 2071 | /** |
||
| 2072 | * Assert that value is alphanumeric. |
||
| 2073 | * |
||
| 2074 | * @param string|null $message |
||
| 2075 | * @param string|null $propertyPath |
||
| 2076 | * @return Assert |
||
| 2077 | * @throws AssertionFailedException |
||
| 2078 | */ |
||
| 2079 | View Code Duplication | public function alnum($message = null, $propertyPath = null) |
|
| 2101 | |||
| 2102 | /** |
||
| 2103 | * Assert that the value is boolean True. |
||
| 2104 | * |
||
| 2105 | * @param string|null $message |
||
| 2106 | * @param string|null $propertyPath |
||
| 2107 | * @return Assert |
||
| 2108 | * @throws AssertionFailedException |
||
| 2109 | */ |
||
| 2110 | public function true($message = null, $propertyPath = null) |
||
| 2127 | |||
| 2128 | /** |
||
| 2129 | * Assert that the value is boolean True. |
||
| 2130 | * |
||
| 2131 | * @param string|null $message |
||
| 2132 | * @param string|null $propertyPath |
||
| 2133 | * @return Assert |
||
| 2134 | * @throws AssertionFailedException |
||
| 2135 | */ |
||
| 2136 | public function truthy($message = null, $propertyPath = null) |
||
| 2153 | |||
| 2154 | /** |
||
| 2155 | * Assert that the value is boolean False. |
||
| 2156 | * |
||
| 2157 | * @param string|null $message |
||
| 2158 | * @param string|null $propertyPath |
||
| 2159 | * @return Assert |
||
| 2160 | * @throws AssertionFailedException |
||
| 2161 | */ |
||
| 2162 | public function false($message = null, $propertyPath = null) |
||
| 2179 | |||
| 2180 | /** |
||
| 2181 | * Assert that the value is not boolean False. |
||
| 2182 | * |
||
| 2183 | * @param string|null $message |
||
| 2184 | * @param string|null $propertyPath |
||
| 2185 | * @return Assert |
||
| 2186 | * @throws AssertionFailedException |
||
| 2187 | */ |
||
| 2188 | public function notFalse($message = null, $propertyPath = null) |
||
| 2205 | |||
| 2206 | /** |
||
| 2207 | * Assert that the class exists. |
||
| 2208 | * |
||
| 2209 | * @param string|null $message |
||
| 2210 | * @param string|null $propertyPath |
||
| 2211 | * @return Assert |
||
| 2212 | * @throws AssertionFailedException |
||
| 2213 | */ |
||
| 2214 | public function classExists($message = null, $propertyPath = null) |
||
| 2231 | |||
| 2232 | /** |
||
| 2233 | * Assert that the class implements the interface |
||
| 2234 | * |
||
| 2235 | * @param string $interfaceName |
||
| 2236 | * @param string|null $message |
||
| 2237 | * @param string|null $propertyPath |
||
| 2238 | * @return Assert |
||
| 2239 | * @throws AssertionFailedException |
||
| 2240 | */ |
||
| 2241 | public function implementsInterface($interfaceName, $message = null, $propertyPath = null) |
||
| 2260 | |||
| 2261 | /** |
||
| 2262 | * Assert that the given string is a valid json string. |
||
| 2263 | * |
||
| 2264 | * NOTICE: |
||
| 2265 | * Since this does a json_decode to determine its validity |
||
| 2266 | * you probably should consider, when using the variable |
||
| 2267 | * content afterwards, just to decode and check for yourself instead |
||
| 2268 | * of using this assertion. |
||
| 2269 | * |
||
| 2270 | * @param string|null $message |
||
| 2271 | * @param string|null $propertyPath |
||
| 2272 | * @return Assert |
||
| 2273 | * @throws AssertionFailedException |
||
| 2274 | */ |
||
| 2275 | public function isJsonString($message = null, $propertyPath = null) |
||
| 2292 | |||
| 2293 | /** |
||
| 2294 | * Assert that the given string is a valid UUID |
||
| 2295 | * |
||
| 2296 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 2297 | * |
||
| 2298 | * @param string|null $message |
||
| 2299 | * @param string|null $propertyPath |
||
| 2300 | * @return Assert |
||
| 2301 | * @throws AssertionFailedException |
||
| 2302 | */ |
||
| 2303 | public function uuid($message = null, $propertyPath = null) |
||
| 2325 | |||
| 2326 | /** |
||
| 2327 | * Assert that the count of countable is equal to count. |
||
| 2328 | * |
||
| 2329 | * @param int $count |
||
| 2330 | * @param string $message |
||
| 2331 | * @param string $propertyPath |
||
| 2332 | * @return Assert |
||
| 2333 | * @throws AssertionFailedException |
||
| 2334 | */ |
||
| 2335 | public function count($count, $message = null, $propertyPath = null) |
||
| 2353 | |||
| 2354 | /** |
||
| 2355 | * @param $func |
||
| 2356 | * @param $args |
||
| 2357 | * @return bool |
||
| 2358 | * @throws AssertionFailedException |
||
| 2359 | */ |
||
| 2360 | protected function doAllOrNullOr($func, $args) |
||
| 2381 | |||
| 2382 | /** |
||
| 2383 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 2384 | * |
||
| 2385 | * @param array $choices |
||
| 2386 | * @param string|null $message |
||
| 2387 | * @param string|null $propertyPath |
||
| 2388 | * @return $this |
||
| 2389 | */ |
||
| 2390 | public function choicesNotEmpty(array $choices, $message = null, $propertyPath = null) |
||
| 2403 | |||
| 2404 | /** |
||
| 2405 | * Determines that the named method is defined in the provided object. |
||
| 2406 | * |
||
| 2407 | * @param mixed $object |
||
| 2408 | * @param string|null $message |
||
| 2409 | * @param string|null $propertyPath |
||
| 2410 | * @returns Assert |
||
| 2411 | * @throws |
||
| 2412 | */ |
||
| 2413 | public function methodExists($object, $message = null, $propertyPath = null) |
||
| 2431 | |||
| 2432 | /** |
||
| 2433 | * Determines that the provided value is an object. |
||
| 2434 | * |
||
| 2435 | * @param string|null $message |
||
| 2436 | * @param string|null $propertyPath |
||
| 2437 | * @return $this |
||
| 2438 | * @throws AssertionFailedException |
||
| 2439 | */ |
||
| 2440 | public function isObject($message = null, $propertyPath = null) |
||
| 2457 | |||
| 2458 | /** |
||
| 2459 | * Make a string version of a value. |
||
| 2460 | * |
||
| 2461 | * @param $value |
||
| 2462 | * @return string |
||
| 2463 | */ |
||
| 2464 | private function stringify($value) |
||
| 2497 | } |
||
| 2498 | |||
| 2499 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..