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 declare(strict_types=1); |
||
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 unsignedInt($message=null, $propertyPath=null) |
||
541 | |||
542 | /** |
||
543 | * @param string|null $message |
||
544 | * @param string|null $propertyPath |
||
545 | * @return Assert |
||
546 | * @throws AssertionFailedException |
||
547 | */ |
||
548 | public function flag($message = null, $propertyPath = null) |
||
554 | |||
555 | /** |
||
556 | * @param string|null $message |
||
557 | * @param string|null $propertyPath |
||
558 | * @return Assert |
||
559 | * @throws AssertionFailedException |
||
560 | */ |
||
561 | public function status($message = null, $propertyPath = null) |
||
567 | |||
568 | /** |
||
569 | * @param string|null $message |
||
570 | * @param string|null $propertyPath |
||
571 | * @return Assert |
||
572 | */ |
||
573 | public function nullOrId($message = null, $propertyPath = null) |
||
577 | |||
578 | /** |
||
579 | * @param string|null $message |
||
580 | * @param string|null $propertyPath |
||
581 | * @return Assert |
||
582 | */ |
||
583 | public function allIds($message = null, $propertyPath = null) |
||
587 | |||
588 | /** |
||
589 | * @param string|null $message |
||
590 | * @param string|null $propertyPath |
||
591 | * @return Assert |
||
592 | * @throws AssertionFailedException |
||
593 | */ |
||
594 | public function int($message = null, $propertyPath = null) |
||
598 | |||
599 | /** |
||
600 | * Assert that value is a php integer. |
||
601 | * |
||
602 | * @param string|null $message |
||
603 | * @param string|null $propertyPath |
||
604 | * @return Assert |
||
605 | * @throws AssertionFailedException |
||
606 | */ |
||
607 | public function integer($message = null, $propertyPath = null) |
||
624 | |||
625 | /** |
||
626 | * Assert that value is a php float. |
||
627 | * |
||
628 | * @param string|null $message |
||
629 | * @param string|null $propertyPath |
||
630 | * @return Assert |
||
631 | * @throws AssertionFailedException |
||
632 | */ |
||
633 | public function float($message = null, $propertyPath = null) |
||
650 | |||
651 | /** |
||
652 | * Validates if an integer or integerish is a digit. |
||
653 | * |
||
654 | * @param string|null $message |
||
655 | * @param string|null $propertyPath |
||
656 | * @return Assert |
||
657 | * @throws AssertionFailedException |
||
658 | */ |
||
659 | public function digit($message = null, $propertyPath = null) |
||
676 | |||
677 | /** |
||
678 | * Validates if an string is a date . |
||
679 | * |
||
680 | * @param string|null $message |
||
681 | * @param string|null $propertyPath |
||
682 | * @return Assert |
||
683 | * @throws AssertionFailedException |
||
684 | */ |
||
685 | public function date($message = null, $propertyPath = null) |
||
703 | |||
704 | /** |
||
705 | * Assert that value is a php integer'ish. |
||
706 | * |
||
707 | * @param string|null $message |
||
708 | * @param string|null $propertyPath |
||
709 | * @return Assert |
||
710 | * @throws AssertionFailedException |
||
711 | */ |
||
712 | public function integerish($message = null, $propertyPath = null) |
||
729 | |||
730 | /** |
||
731 | * Assert that value is php boolean |
||
732 | * |
||
733 | * @param string|null $message |
||
734 | * @param string|null $propertyPath |
||
735 | * @return Assert |
||
736 | * @throws AssertionFailedException |
||
737 | */ |
||
738 | public function boolean($message = null, $propertyPath = null) |
||
755 | |||
756 | /** |
||
757 | * Assert that value is a PHP scalar |
||
758 | * |
||
759 | * @param string|null $message |
||
760 | * @param string|null $propertyPath |
||
761 | * @return Assert |
||
762 | * @throws AssertionFailedException |
||
763 | */ |
||
764 | public function scalar($message = null, $propertyPath = null) |
||
781 | |||
782 | /** |
||
783 | * Assert that value is not empty |
||
784 | * |
||
785 | * @param string|null $message |
||
786 | * @param string|null $propertyPath |
||
787 | * @return Assert |
||
788 | * @throws AssertionFailedException |
||
789 | */ |
||
790 | View Code Duplication | public function notEmpty($message = null, $propertyPath = null) |
|
807 | |||
808 | /** |
||
809 | * Assert that value is empty |
||
810 | * |
||
811 | * @param string|null $message |
||
812 | * @param string|null $propertyPath |
||
813 | * @return Assert |
||
814 | * @throws AssertionFailedException |
||
815 | */ |
||
816 | public function noContent($message = null, $propertyPath = null) |
||
833 | |||
834 | /** |
||
835 | * Assert that value is not null |
||
836 | * |
||
837 | * @param string|null $message |
||
838 | * @param string|null $propertyPath |
||
839 | * @return Assert |
||
840 | * @throws AssertionFailedException |
||
841 | */ |
||
842 | public function notNull($message = null, $propertyPath = null) |
||
859 | |||
860 | /** |
||
861 | * Assert that value is a string |
||
862 | * |
||
863 | * @param string|null $message |
||
864 | * @param string|null $propertyPath |
||
865 | * @return Assert |
||
866 | * @throws AssertionFailedException |
||
867 | */ |
||
868 | public function string($message = null, $propertyPath = null) |
||
886 | |||
887 | /** |
||
888 | * Assert that value matches a regex |
||
889 | * |
||
890 | * @param string $pattern |
||
891 | * @param string|null $message |
||
892 | * @param string|null $propertyPath |
||
893 | * @return Assert |
||
894 | * @throws AssertionFailedException |
||
895 | */ |
||
896 | public function regex($pattern, $message=null, $propertyPath=null) |
||
914 | |||
915 | /** |
||
916 | * @param string $message |
||
917 | * @param string $propertyPath |
||
918 | * @return $this |
||
919 | * @throws AssertionFailedException |
||
920 | */ |
||
921 | public function ipAddress($message = null, $propertyPath = null) |
||
940 | |||
941 | /** |
||
942 | * @param string $pattern |
||
943 | * @param string|null $message |
||
944 | * @param string|null $propertyPath |
||
945 | * @return $this |
||
946 | * @throws AssertionFailedException |
||
947 | */ |
||
948 | public function notRegex($pattern, $message = null, $propertyPath = null) |
||
966 | |||
967 | /** |
||
968 | * Assert that string has a given length. |
||
969 | * |
||
970 | * @param int $length |
||
971 | * @param string|null $message |
||
972 | * @param string|null $propertyPath |
||
973 | * @param string $encoding |
||
974 | * @return Assert |
||
975 | * @throws AssertionFailedException |
||
976 | */ |
||
977 | public function length($length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
998 | |||
999 | /** |
||
1000 | * Assert that a string is at least $minLength chars long. |
||
1001 | * |
||
1002 | * @param int $minLength |
||
1003 | * @param string|null $message |
||
1004 | * @param string|null $propertyPath |
||
1005 | * @param string $encoding |
||
1006 | * @return Assert |
||
1007 | * @throws AssertionFailedException |
||
1008 | */ |
||
1009 | public function minLength($minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
1031 | |||
1032 | /** |
||
1033 | * Assert that string value is not longer than $maxLength chars. |
||
1034 | * |
||
1035 | * @param integer $maxLength |
||
1036 | * @param string|null $message |
||
1037 | * @param string|null $propertyPath |
||
1038 | * @param string $encoding |
||
1039 | * @return Assert |
||
1040 | * @throws AssertionFailedException |
||
1041 | */ |
||
1042 | public function maxLength($maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
1063 | |||
1064 | /** |
||
1065 | * Assert that string length is between min,max lengths. |
||
1066 | * |
||
1067 | * @param integer $minLength |
||
1068 | * @param integer $maxLength |
||
1069 | * @param string|null $message |
||
1070 | * @param string|null $propertyPath |
||
1071 | * @param string $encoding |
||
1072 | * @return Assert |
||
1073 | * @throws AssertionFailedException |
||
1074 | */ |
||
1075 | public function betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
1109 | |||
1110 | /** |
||
1111 | * Assert that string starts with a sequence of chars. |
||
1112 | * |
||
1113 | * @param string $needle |
||
1114 | * @param string|null $message |
||
1115 | * @param string|null $propertyPath |
||
1116 | * @param string $encoding |
||
1117 | * @return Assert |
||
1118 | * @throws AssertionFailedException |
||
1119 | */ |
||
1120 | View Code Duplication | public function startsWith($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
|
1140 | |||
1141 | /** |
||
1142 | * Assert that string ends with a sequence of chars. |
||
1143 | * |
||
1144 | * @param string $needle |
||
1145 | * @param string|null $message |
||
1146 | * @param string|null $propertyPath |
||
1147 | * @param string $encoding |
||
1148 | * @return Assert |
||
1149 | * @throws AssertionFailedException |
||
1150 | */ |
||
1151 | public function endsWith($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
1172 | |||
1173 | /** |
||
1174 | * Assert that string contains a sequence of chars. |
||
1175 | * |
||
1176 | * @param string $needle |
||
1177 | * @param string|null $message |
||
1178 | * @param string|null $propertyPath |
||
1179 | * @param string $encoding |
||
1180 | * @return Assert |
||
1181 | * @throws AssertionFailedException |
||
1182 | */ |
||
1183 | View Code Duplication | public function contains($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
|
1203 | |||
1204 | /** |
||
1205 | * Assert that value is in array of choices. |
||
1206 | * |
||
1207 | * @param array $choices |
||
1208 | * @param string|null $message |
||
1209 | * @param string|null $propertyPath |
||
1210 | * @return Assert |
||
1211 | * @throws AssertionFailedException |
||
1212 | */ |
||
1213 | public function choice(array $choices, $message = null, $propertyPath = null) |
||
1231 | |||
1232 | /** |
||
1233 | * Alias of {@see choice()} |
||
1234 | * |
||
1235 | * @throws AssertionFailedException |
||
1236 | * |
||
1237 | * @param array $choices |
||
1238 | * @param string|null $message |
||
1239 | * @param string|null $propertyPath |
||
1240 | * @return $this |
||
1241 | */ |
||
1242 | public function inArray(array $choices, $message = null, $propertyPath = null) |
||
1251 | |||
1252 | /** |
||
1253 | * Assert that value is numeric. |
||
1254 | * |
||
1255 | * @param string|null $message |
||
1256 | * @param string|null $propertyPath |
||
1257 | * @return Assert |
||
1258 | * @throws AssertionFailedException |
||
1259 | */ |
||
1260 | public function numeric($message = null, $propertyPath = null) |
||
1277 | |||
1278 | /** |
||
1279 | * @param string|null $message |
||
1280 | * @param string|null $propertyPath |
||
1281 | * @return Assert |
||
1282 | * @throws AssertionFailedException |
||
1283 | */ |
||
1284 | public function nonEmptyArray($message = null, $propertyPath = null) |
||
1289 | |||
1290 | /** |
||
1291 | * @param string|null $message |
||
1292 | * @param string|null $propertyPath |
||
1293 | * @return Assert |
||
1294 | * @throws AssertionFailedException |
||
1295 | */ |
||
1296 | public function nonEmptyInt($message = null, $propertyPath = null) |
||
1301 | |||
1302 | /** |
||
1303 | * @param string|null $message |
||
1304 | * @param string|null $propertyPath |
||
1305 | * @return Assert |
||
1306 | * @throws AssertionFailedException |
||
1307 | */ |
||
1308 | public function nonEmptyString($message = null, $propertyPath = null) |
||
1313 | |||
1314 | /** |
||
1315 | * Assert that value is an array. |
||
1316 | * |
||
1317 | * @param string|null $message |
||
1318 | * @param string|null $propertyPath |
||
1319 | * @return Assert |
||
1320 | * @throws AssertionFailedException |
||
1321 | */ |
||
1322 | public function isArray($message = null, $propertyPath = null) |
||
1339 | |||
1340 | /** |
||
1341 | * Assert that value is an array or a traversable object. |
||
1342 | * |
||
1343 | * @param string|null $message |
||
1344 | * @param string|null $propertyPath |
||
1345 | * @return Assert |
||
1346 | * @throws AssertionFailedException |
||
1347 | */ |
||
1348 | public function isTraversable($message = null, $propertyPath = null) |
||
1365 | |||
1366 | /** |
||
1367 | * Assert that value is an array or an array-accessible object. |
||
1368 | * |
||
1369 | * @param string|null $message |
||
1370 | * @param string|null $propertyPath |
||
1371 | * @return Assert |
||
1372 | * @throws AssertionFailedException |
||
1373 | */ |
||
1374 | public function isArrayAccessible($message = null, $propertyPath = null) |
||
1391 | |||
1392 | /** |
||
1393 | * Assert that key exists in an array |
||
1394 | * |
||
1395 | * @param string|integer $key |
||
1396 | * @param string|null $message |
||
1397 | * @param string|null $propertyPath |
||
1398 | * @return Assert |
||
1399 | * @throws AssertionFailedException |
||
1400 | */ |
||
1401 | public function keyExists($key, $message = null, $propertyPath = null) |
||
1419 | |||
1420 | /** |
||
1421 | * Assert that keys exist in array |
||
1422 | * |
||
1423 | * @param array $keys |
||
1424 | * @param string|null $message |
||
1425 | * @param string|null $propertyPath |
||
1426 | * @return Assert |
||
1427 | * @throws AssertionFailedException |
||
1428 | */ |
||
1429 | public function keysExist($keys, $message = null, $propertyPath = null) |
||
1450 | |||
1451 | /** |
||
1452 | * Assert that property exists in array |
||
1453 | * |
||
1454 | * @param string|integer $key |
||
1455 | * @param string|null $message |
||
1456 | * @param string|null $propertyPath |
||
1457 | * @return Assert |
||
1458 | * @throws AssertionFailedException |
||
1459 | */ |
||
1460 | public function propertyExists($key, $message = null, $propertyPath = null) |
||
1478 | |||
1479 | /** |
||
1480 | * Assert that properties exists in array |
||
1481 | * |
||
1482 | * @param array $keys |
||
1483 | * @param string|null $message |
||
1484 | * @param string|null $propertyPath |
||
1485 | * @return Assert |
||
1486 | * @throws AssertionFailedException |
||
1487 | */ |
||
1488 | public function propertiesExist(array $keys, $message = null, $propertyPath = null) |
||
1510 | |||
1511 | /** |
||
1512 | * Assert that string is valid utf8 |
||
1513 | * |
||
1514 | * @param string|null $message |
||
1515 | * @param string|null $propertyPath |
||
1516 | * @return Assert |
||
1517 | * @throws AssertionFailedException |
||
1518 | */ |
||
1519 | View Code Duplication | public function utf8($message = null, $propertyPath = null) |
|
1537 | |||
1538 | |||
1539 | /** |
||
1540 | * Assert that string is valid utf8 |
||
1541 | * |
||
1542 | * @param string|null $message |
||
1543 | * @param string|null $propertyPath |
||
1544 | * @return Assert |
||
1545 | * @throws AssertionFailedException |
||
1546 | */ |
||
1547 | View Code Duplication | public function ascii($message = null, $propertyPath = null) |
|
1565 | |||
1566 | /** |
||
1567 | * Assert that key exists in an array/array-accessible object using isset() |
||
1568 | * |
||
1569 | * @param string|integer $key |
||
1570 | * @param string|null $message |
||
1571 | * @param string|null $propertyPath |
||
1572 | * @return Assert |
||
1573 | * @throws AssertionFailedException |
||
1574 | */ |
||
1575 | public function keyIsset($key, $message = null, $propertyPath = null) |
||
1593 | |||
1594 | /** |
||
1595 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
1596 | * |
||
1597 | * @param string|integer $key |
||
1598 | * @param string|null $message |
||
1599 | * @param string|null $propertyPath |
||
1600 | * @return Assert |
||
1601 | * @throws AssertionFailedException |
||
1602 | */ |
||
1603 | public function notEmptyKey($key, $message = null, $propertyPath = null) |
||
1613 | |||
1614 | /** |
||
1615 | * Assert that value is not blank |
||
1616 | * |
||
1617 | * @param string|null $message |
||
1618 | * @param string|null $propertyPath |
||
1619 | * @return Assert |
||
1620 | * @throws AssertionFailedException |
||
1621 | */ |
||
1622 | View Code Duplication | public function notBlank($message = null, $propertyPath = null) |
|
1639 | |||
1640 | /** |
||
1641 | * Assert that value is instance of given class-name. |
||
1642 | * |
||
1643 | * @param string $className |
||
1644 | * @param string|null $message |
||
1645 | * @param string|null $propertyPath |
||
1646 | * @return Assert |
||
1647 | * @throws AssertionFailedException |
||
1648 | */ |
||
1649 | public function isInstanceOf($className, $message = null, $propertyPath = null) |
||
1667 | |||
1668 | /** |
||
1669 | * Assert that value is not instance of given class-name. |
||
1670 | * |
||
1671 | * @param string $className |
||
1672 | * @param string|null $message |
||
1673 | * @param string|null $propertyPath |
||
1674 | * @return Assert |
||
1675 | * @throws AssertionFailedException |
||
1676 | */ |
||
1677 | public function notIsInstanceOf($className, $message = null, $propertyPath = null) |
||
1695 | |||
1696 | /** |
||
1697 | * Assert that value is subclass of given class-name. |
||
1698 | * |
||
1699 | * @param string $className |
||
1700 | * @param string|null $message |
||
1701 | * @param string|null $propertyPath |
||
1702 | * @return Assert |
||
1703 | * @throws AssertionFailedException |
||
1704 | */ |
||
1705 | public function subclassOf($className, $message = null, $propertyPath = null) |
||
1723 | |||
1724 | /** |
||
1725 | * Assert that value is in range of numbers. |
||
1726 | * |
||
1727 | * @param integer $minValue |
||
1728 | * @param integer $maxValue |
||
1729 | * @param string|null $message |
||
1730 | * @param string|null $propertyPath |
||
1731 | * @return Assert |
||
1732 | * @throws AssertionFailedException |
||
1733 | */ |
||
1734 | public function range($minValue, $maxValue, $message = null, $propertyPath = null) |
||
1757 | |||
1758 | /** |
||
1759 | * Assert that a value is at least as big as a given limit |
||
1760 | * |
||
1761 | * @param mixed $minValue |
||
1762 | * @param string|null $message |
||
1763 | * @param string|null $propertyPath |
||
1764 | * @return Assert |
||
1765 | * @throws AssertionFailedException |
||
1766 | */ |
||
1767 | public function min($minValue, $message = null, $propertyPath = null) |
||
1786 | |||
1787 | /** |
||
1788 | * Assert that a number is smaller as a given limit |
||
1789 | * |
||
1790 | * @param mixed $maxValue |
||
1791 | * @param string|null $message |
||
1792 | * @param string|null $propertyPath |
||
1793 | * @return Assert |
||
1794 | * @throws AssertionFailedException |
||
1795 | */ |
||
1796 | public function max($maxValue, $message = null, $propertyPath = null) |
||
1815 | |||
1816 | /** |
||
1817 | * Assert that a file exists |
||
1818 | * |
||
1819 | * @param string|null $message |
||
1820 | * @param string|null $propertyPath |
||
1821 | * @return Assert |
||
1822 | * @throws AssertionFailedException |
||
1823 | */ |
||
1824 | public function file($message = null, $propertyPath = null) |
||
1843 | |||
1844 | /** |
||
1845 | * @param string|null $message |
||
1846 | * @param string|null $propertyPath |
||
1847 | * @return $this |
||
1848 | * @throws AssertionFailedException |
||
1849 | */ |
||
1850 | public function fileExists($message = null, $propertyPath = null) |
||
1869 | |||
1870 | /** |
||
1871 | * Assert that a directory exists |
||
1872 | * |
||
1873 | * @param string|null $message |
||
1874 | * @param string|null $propertyPath |
||
1875 | * @return Assert |
||
1876 | * @throws AssertionFailedException |
||
1877 | */ |
||
1878 | View Code Duplication | public function directory($message = null, $propertyPath = null) |
|
1896 | |||
1897 | /** |
||
1898 | * Assert that the value is something readable |
||
1899 | * |
||
1900 | * @param string|null $message |
||
1901 | * @param string|null $propertyPath |
||
1902 | * @return Assert |
||
1903 | * @throws AssertionFailedException |
||
1904 | */ |
||
1905 | View Code Duplication | public function readable($message = null, $propertyPath = null) |
|
1923 | |||
1924 | /** |
||
1925 | * Assert that the value is something writeable |
||
1926 | * |
||
1927 | * @param string|null $message |
||
1928 | * @param string|null $propertyPath |
||
1929 | * @return Assert |
||
1930 | * @throws AssertionFailedException |
||
1931 | */ |
||
1932 | View Code Duplication | public function writeable($message = null, $propertyPath = null) |
|
1950 | |||
1951 | /** |
||
1952 | * Assert that value is an email adress (using |
||
1953 | * input_filter/FILTER_VALIDATE_EMAIL). |
||
1954 | * |
||
1955 | * @param string|null $message |
||
1956 | * @param string|null $propertyPath |
||
1957 | * @return Assert |
||
1958 | * @throws AssertionFailedException |
||
1959 | */ |
||
1960 | public function email($message = null, $propertyPath = null) |
||
1992 | |||
1993 | /** |
||
1994 | * @param null $message |
||
1995 | * @param null $propertyPath |
||
1996 | * @return Assert |
||
1997 | * @throws AssertionFailedException |
||
1998 | */ |
||
1999 | public function emailPrefix($message = null, $propertyPath = null) |
||
2004 | |||
2005 | /** |
||
2006 | * Assert that value is an URL. |
||
2007 | * |
||
2008 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
2009 | * |
||
2010 | * @param string|null $message |
||
2011 | * @param string|null $propertyPath |
||
2012 | * @return Assert |
||
2013 | * @throws AssertionFailedException |
||
2014 | * |
||
2015 | * |
||
2016 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
2017 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
2018 | */ |
||
2019 | public function url($message = null, $propertyPath = null) |
||
2053 | |||
2054 | /** |
||
2055 | * Assert that value is domain name. |
||
2056 | * |
||
2057 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
2058 | * |
||
2059 | * @param string|null $message |
||
2060 | * @param string|null $propertyPath |
||
2061 | * @return Assert |
||
2062 | * @throws AssertionFailedException |
||
2063 | * |
||
2064 | */ |
||
2065 | public function domainName($message = null, $propertyPath = null) |
||
2084 | |||
2085 | /** |
||
2086 | * Assert that value is alphanumeric. |
||
2087 | * |
||
2088 | * @param string|null $message |
||
2089 | * @param string|null $propertyPath |
||
2090 | * @return Assert |
||
2091 | * @throws AssertionFailedException |
||
2092 | */ |
||
2093 | View Code Duplication | public function alnum($message = null, $propertyPath = null) |
|
2115 | |||
2116 | /** |
||
2117 | * Assert that the value is boolean True. |
||
2118 | * |
||
2119 | * @param string|null $message |
||
2120 | * @param string|null $propertyPath |
||
2121 | * @return Assert |
||
2122 | * @throws AssertionFailedException |
||
2123 | */ |
||
2124 | public function true($message = null, $propertyPath = null) |
||
2141 | |||
2142 | /** |
||
2143 | * Assert that the value is boolean True. |
||
2144 | * |
||
2145 | * @param string|null $message |
||
2146 | * @param string|null $propertyPath |
||
2147 | * @return Assert |
||
2148 | * @throws AssertionFailedException |
||
2149 | */ |
||
2150 | public function truthy($message = null, $propertyPath = null) |
||
2167 | |||
2168 | /** |
||
2169 | * Assert that the value is boolean False. |
||
2170 | * |
||
2171 | * @param string|null $message |
||
2172 | * @param string|null $propertyPath |
||
2173 | * @return Assert |
||
2174 | * @throws AssertionFailedException |
||
2175 | */ |
||
2176 | public function false($message = null, $propertyPath = null) |
||
2193 | |||
2194 | /** |
||
2195 | * Assert that the value is not boolean False. |
||
2196 | * |
||
2197 | * @param string|null $message |
||
2198 | * @param string|null $propertyPath |
||
2199 | * @return Assert |
||
2200 | * @throws AssertionFailedException |
||
2201 | */ |
||
2202 | public function notFalse($message = null, $propertyPath = null) |
||
2219 | |||
2220 | /** |
||
2221 | * Assert that the class exists. |
||
2222 | * |
||
2223 | * @param string|null $message |
||
2224 | * @param string|null $propertyPath |
||
2225 | * @return Assert |
||
2226 | * @throws AssertionFailedException |
||
2227 | */ |
||
2228 | public function classExists($message = null, $propertyPath = null) |
||
2245 | |||
2246 | /** |
||
2247 | * Assert that the class implements the interface |
||
2248 | * |
||
2249 | * @param string $interfaceName |
||
2250 | * @param string|null $message |
||
2251 | * @param string|null $propertyPath |
||
2252 | * @return Assert |
||
2253 | * @throws AssertionFailedException |
||
2254 | */ |
||
2255 | public function implementsInterface($interfaceName, $message = null, $propertyPath = null) |
||
2274 | |||
2275 | /** |
||
2276 | * Assert that the given string is a valid json string. |
||
2277 | * |
||
2278 | * NOTICE: |
||
2279 | * Since this does a json_decode to determine its validity |
||
2280 | * you probably should consider, when using the variable |
||
2281 | * content afterwards, just to decode and check for yourself instead |
||
2282 | * of using this assertion. |
||
2283 | * |
||
2284 | * @param string|null $message |
||
2285 | * @param string|null $propertyPath |
||
2286 | * @return Assert |
||
2287 | * @throws AssertionFailedException |
||
2288 | */ |
||
2289 | public function isJsonString($message = null, $propertyPath = null) |
||
2306 | |||
2307 | /** |
||
2308 | * Assert that the given string is a valid UUID |
||
2309 | * |
||
2310 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
2311 | * |
||
2312 | * @param string|null $message |
||
2313 | * @param string|null $propertyPath |
||
2314 | * @return Assert |
||
2315 | * @throws AssertionFailedException |
||
2316 | */ |
||
2317 | public function uuid($message = null, $propertyPath = null) |
||
2339 | |||
2340 | /** |
||
2341 | * Assert that the count of countable is equal to count. |
||
2342 | * |
||
2343 | * @param int $count |
||
2344 | * @param string $message |
||
2345 | * @param string $propertyPath |
||
2346 | * @return Assert |
||
2347 | * @throws AssertionFailedException |
||
2348 | */ |
||
2349 | public function count($count, $message = null, $propertyPath = null) |
||
2367 | |||
2368 | /** |
||
2369 | * @param $func |
||
2370 | * @param $args |
||
2371 | * @return bool |
||
2372 | * @throws AssertionFailedException |
||
2373 | */ |
||
2374 | protected function doAllOrNullOr($func, $args) |
||
2395 | |||
2396 | /** |
||
2397 | * Determines if the values array has every choice as key and that this choice has content. |
||
2398 | * |
||
2399 | * @param array $choices |
||
2400 | * @param string|null $message |
||
2401 | * @param string|null $propertyPath |
||
2402 | * @return $this |
||
2403 | */ |
||
2404 | public function choicesNotEmpty(array $choices, $message = null, $propertyPath = null) |
||
2417 | |||
2418 | /** |
||
2419 | * Determines that the named method is defined in the provided object. |
||
2420 | * |
||
2421 | * @param mixed $object |
||
2422 | * @param string|null $message |
||
2423 | * @param string|null $propertyPath |
||
2424 | * @returns Assert |
||
2425 | * @throws |
||
2426 | */ |
||
2427 | public function methodExists($object, $message = null, $propertyPath = null) |
||
2445 | |||
2446 | /** |
||
2447 | * Determines that the provided value is an object. |
||
2448 | * |
||
2449 | * @param string|null $message |
||
2450 | * @param string|null $propertyPath |
||
2451 | * @return $this |
||
2452 | * @throws AssertionFailedException |
||
2453 | */ |
||
2454 | public function isObject($message = null, $propertyPath = null) |
||
2471 | |||
2472 | /** |
||
2473 | * Make a string version of a value. |
||
2474 | * |
||
2475 | * @param $value |
||
2476 | * @return string |
||
2477 | */ |
||
2478 | private function stringify($value) |
||
2511 | } |
||
2512 | |||
2513 |
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..