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 | |||
93 | /** @var bool */ |
||
94 | protected $nullOr = false; |
||
95 | |||
96 | /** @var bool */ |
||
97 | protected $emptyOr = false; |
||
98 | |||
99 | /** @var mixed */ |
||
100 | protected $value = null; |
||
101 | |||
102 | /** @var bool */ |
||
103 | protected $all = false; |
||
104 | |||
105 | /** @var null|string */ |
||
106 | protected $propertyPath = null; |
||
107 | /** |
||
108 | * Exception to throw when an assertion failed. |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $exceptionClass = 'Terah\Assert\AssertionFailedException'; |
||
113 | |||
114 | /** |
||
115 | * @param mixed $value |
||
116 | */ |
||
117 | public function __construct($value) |
||
121 | |||
122 | /** |
||
123 | * @param $value |
||
124 | * @return Assert |
||
125 | */ |
||
126 | public static function that($value) |
||
130 | |||
131 | /** |
||
132 | * @param mixed $value |
||
133 | * @return Assert |
||
134 | */ |
||
135 | public function reset($value) |
||
139 | |||
140 | /** |
||
141 | * @param mixed $value |
||
142 | * @return Assert |
||
143 | */ |
||
144 | public function value($value) |
||
149 | |||
150 | /** |
||
151 | * @param bool $nullOr |
||
152 | * @return Assert |
||
153 | */ |
||
154 | public function nullOr($nullOr = true) |
||
159 | |||
160 | /** |
||
161 | * @param bool $emptyOr |
||
162 | * @return Assert |
||
163 | */ |
||
164 | public function emptyOr($emptyOr = true) |
||
169 | |||
170 | /** |
||
171 | * @param bool $all |
||
172 | * @return Assert |
||
173 | */ |
||
174 | public function all($all = true) |
||
179 | |||
180 | /** |
||
181 | * Helper method that handles building the assertion failure exceptions. |
||
182 | * They are returned from this method so that the stack trace still shows |
||
183 | * the assertions method. |
||
184 | * |
||
185 | * @param string $message |
||
186 | * @param int $code |
||
187 | * @param string $propertyPath |
||
188 | * @param array $constraints |
||
189 | * @return AssertionFailedException |
||
190 | */ |
||
191 | protected function createException($message, $code, $propertyPath, array $constraints = []) |
||
197 | |||
198 | /** |
||
199 | * @param $exceptionClass |
||
200 | * @return Assert |
||
201 | */ |
||
202 | public function setExceptionClass($exceptionClass) |
||
207 | |||
208 | /** |
||
209 | * @param string $name |
||
210 | * @return Assert |
||
211 | */ |
||
212 | public function name($name) |
||
217 | |||
218 | /** |
||
219 | * Assert that two values are equal (using == ). |
||
220 | * |
||
221 | * @param mixed $value2 |
||
222 | * @param string|null $message |
||
223 | * @param string|null $propertyPath |
||
224 | * @return Assert |
||
225 | * @throws AssertionFailedException |
||
226 | */ |
||
227 | public function eq($value2, $message = null, $propertyPath = null) |
||
244 | |||
245 | /** |
||
246 | * |
||
247 | * @param mixed $value2 |
||
248 | * @param string|null $message |
||
249 | * @param string|null $propertyPath |
||
250 | * @return Assert |
||
251 | * @throws AssertionFailedException |
||
252 | */ |
||
253 | View Code Duplication | public function greaterThan($value2, $message = null, $propertyPath = null) |
|
270 | |||
271 | /** |
||
272 | * |
||
273 | * @param mixed $value2 |
||
274 | * @param string|null $message |
||
275 | * @param string|null $propertyPath |
||
276 | * @return Assert |
||
277 | * @throws AssertionFailedException |
||
278 | */ |
||
279 | View Code Duplication | public function greaterThanOrEq($value2, $message = null, $propertyPath = null) |
|
296 | |||
297 | /** |
||
298 | * |
||
299 | * @param mixed $value2 |
||
300 | * @param string|null $message |
||
301 | * @param string|null $propertyPath |
||
302 | * @return Assert |
||
303 | * @throws AssertionFailedException |
||
304 | */ |
||
305 | View Code Duplication | public function lessThan($value2, $message = null, $propertyPath = null) |
|
322 | |||
323 | /** |
||
324 | * |
||
325 | * @param mixed $value2 |
||
326 | * @param string|null $message |
||
327 | * @param string|null $propertyPath |
||
328 | * @return Assert |
||
329 | * @throws AssertionFailedException |
||
330 | */ |
||
331 | View Code Duplication | public function lessThanOrEq($value2, $message = null, $propertyPath = null) |
|
348 | |||
349 | /** |
||
350 | * Assert that two values are the same (using ===). |
||
351 | * |
||
352 | * @param mixed $value2 |
||
353 | * @param string|null $message |
||
354 | * @param string|null $propertyPath |
||
355 | * @return Assert |
||
356 | * @throws AssertionFailedException |
||
357 | */ |
||
358 | public function same($value2, $message = null, $propertyPath = null) |
||
375 | |||
376 | /** |
||
377 | * Assert that two values are not equal (using == ). |
||
378 | * |
||
379 | * @param mixed $value2 |
||
380 | * @param string|null $message |
||
381 | * @param string|null $propertyPath |
||
382 | * @return Assert |
||
383 | * @throws AssertionFailedException |
||
384 | */ |
||
385 | public function notEq($value2, $message = null, $propertyPath = null) |
||
402 | |||
403 | /** |
||
404 | * @param string|null $message |
||
405 | * @param string|null $propertyPath |
||
406 | * |
||
407 | * @return $this |
||
408 | * @throws AssertionFailedException |
||
409 | */ |
||
410 | public function isCallable($message = null, $propertyPath = null) |
||
426 | |||
427 | /** |
||
428 | * Assert that two values are not the same (using === ). |
||
429 | * |
||
430 | * @param mixed $value2 |
||
431 | * @param string|null $message |
||
432 | * @param string|null $propertyPath |
||
433 | * @return Assert |
||
434 | * @throws AssertionFailedException |
||
435 | */ |
||
436 | public function notSame($value2, $message = null, $propertyPath = null) |
||
453 | |||
454 | /** |
||
455 | * @param string|null $message |
||
456 | * @param string|null $propertyPath |
||
457 | * @return Assert |
||
458 | * @throws AssertionFailedException |
||
459 | */ |
||
460 | public function id($message = null, $propertyPath = null) |
||
465 | |||
466 | /** |
||
467 | * @param string|null $message |
||
468 | * @param string|null $propertyPath |
||
469 | * @return Assert |
||
470 | * @throws AssertionFailedException |
||
471 | */ |
||
472 | public function flag($message = null, $propertyPath = null) |
||
477 | |||
478 | /** |
||
479 | * @param string|null $message |
||
480 | * @param string|null $propertyPath |
||
481 | * @return Assert |
||
482 | * @throws AssertionFailedException |
||
483 | */ |
||
484 | public function status($message = null, $propertyPath = null) |
||
489 | |||
490 | /** |
||
491 | * @param string|null $message |
||
492 | * @param string|null $propertyPath |
||
493 | * @return Assert |
||
494 | */ |
||
495 | public function nullOrId($message = null, $propertyPath = null) |
||
499 | |||
500 | /** |
||
501 | * @param string|null $message |
||
502 | * @param string|null $propertyPath |
||
503 | * @return Assert |
||
504 | */ |
||
505 | public function allIds($message = null, $propertyPath = null) |
||
509 | |||
510 | /** |
||
511 | * @param string|null $message |
||
512 | * @param string|null $propertyPath |
||
513 | * @return Assert |
||
514 | * @throws AssertionFailedException |
||
515 | */ |
||
516 | public function int($message = null, $propertyPath = null) |
||
520 | |||
521 | /** |
||
522 | * Assert that value is a php integer. |
||
523 | * |
||
524 | * @param string|null $message |
||
525 | * @param string|null $propertyPath |
||
526 | * @return Assert |
||
527 | * @throws AssertionFailedException |
||
528 | */ |
||
529 | public function integer($message = null, $propertyPath = null) |
||
545 | |||
546 | /** |
||
547 | * Assert that value is a php float. |
||
548 | * |
||
549 | * @param string|null $message |
||
550 | * @param string|null $propertyPath |
||
551 | * @return Assert |
||
552 | * @throws AssertionFailedException |
||
553 | */ |
||
554 | public function float($message = null, $propertyPath = null) |
||
570 | |||
571 | /** |
||
572 | * Validates if an integer or integerish is a digit. |
||
573 | * |
||
574 | * @param string|null $message |
||
575 | * @param string|null $propertyPath |
||
576 | * @return Assert |
||
577 | * @throws AssertionFailedException |
||
578 | */ |
||
579 | public function digit($message = null, $propertyPath = null) |
||
595 | |||
596 | /** |
||
597 | * Validates if an string is a date . |
||
598 | * |
||
599 | * @param string|null $message |
||
600 | * @param string|null $propertyPath |
||
601 | * @return Assert |
||
602 | * @throws AssertionFailedException |
||
603 | */ |
||
604 | public function date($message = null, $propertyPath = null) |
||
621 | |||
622 | /** |
||
623 | * Assert that value is a php integer'ish. |
||
624 | * |
||
625 | * @param string|null $message |
||
626 | * @param string|null $propertyPath |
||
627 | * @return Assert |
||
628 | * @throws AssertionFailedException |
||
629 | */ |
||
630 | public function integerish($message = null, $propertyPath = null) |
||
646 | |||
647 | /** |
||
648 | * Assert that value is php boolean |
||
649 | * |
||
650 | * @param string|null $message |
||
651 | * @param string|null $propertyPath |
||
652 | * @return Assert |
||
653 | * @throws AssertionFailedException |
||
654 | */ |
||
655 | public function boolean($message = null, $propertyPath = null) |
||
671 | |||
672 | /** |
||
673 | * Assert that value is a PHP scalar |
||
674 | * |
||
675 | * @param string|null $message |
||
676 | * @param string|null $propertyPath |
||
677 | * @return Assert |
||
678 | * @throws AssertionFailedException |
||
679 | */ |
||
680 | public function scalar($message = null, $propertyPath = null) |
||
696 | |||
697 | /** |
||
698 | * Assert that value is not empty |
||
699 | * |
||
700 | * @param string|null $message |
||
701 | * @param string|null $propertyPath |
||
702 | * @return Assert |
||
703 | * @throws AssertionFailedException |
||
704 | */ |
||
705 | View Code Duplication | public function notEmpty($message = null, $propertyPath = null) |
|
721 | |||
722 | /** |
||
723 | * Assert that value is empty |
||
724 | * |
||
725 | * @param string|null $message |
||
726 | * @param string|null $propertyPath |
||
727 | * @return Assert |
||
728 | * @throws AssertionFailedException |
||
729 | */ |
||
730 | public function noContent($message = null, $propertyPath = null) |
||
746 | |||
747 | /** |
||
748 | * Assert that value is not null |
||
749 | * |
||
750 | * @param string|null $message |
||
751 | * @param string|null $propertyPath |
||
752 | * @return Assert |
||
753 | * @throws AssertionFailedException |
||
754 | */ |
||
755 | public function notNull($message = null, $propertyPath = null) |
||
771 | |||
772 | /** |
||
773 | * Assert that value is a string |
||
774 | * |
||
775 | * @param string|null $message |
||
776 | * @param string|null $propertyPath |
||
777 | * @return Assert |
||
778 | * @throws AssertionFailedException |
||
779 | */ |
||
780 | public function string($message = null, $propertyPath = null) |
||
797 | |||
798 | /** |
||
799 | * Assert that value matches a regex |
||
800 | * |
||
801 | * @param string $pattern |
||
802 | * @param string|null $message |
||
803 | * @param string|null $propertyPath |
||
804 | * @return Assert |
||
805 | * @throws AssertionFailedException |
||
806 | */ |
||
807 | public function regex($pattern, $message = null, $propertyPath = null) |
||
824 | |||
825 | /** |
||
826 | * @param string $pattern |
||
827 | * @param string|null $message |
||
828 | * @param string|null $propertyPath |
||
829 | * @return $this |
||
830 | * @throws AssertionFailedException |
||
831 | */ |
||
832 | public function notRegex($pattern, $message = null, $propertyPath = null) |
||
849 | |||
850 | /** |
||
851 | * Assert that string has a given length. |
||
852 | * |
||
853 | * @param int $length |
||
854 | * @param string|null $message |
||
855 | * @param string|null $propertyPath |
||
856 | * @param string $encoding |
||
857 | * @return Assert |
||
858 | * @throws AssertionFailedException |
||
859 | */ |
||
860 | public function length($length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
880 | |||
881 | /** |
||
882 | * Assert that a string is at least $minLength chars long. |
||
883 | * |
||
884 | * @param int $minLength |
||
885 | * @param string|null $message |
||
886 | * @param string|null $propertyPath |
||
887 | * @param string $encoding |
||
888 | * @return Assert |
||
889 | * @throws AssertionFailedException |
||
890 | */ |
||
891 | public function minLength($minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
912 | |||
913 | /** |
||
914 | * Assert that string value is not longer than $maxLength chars. |
||
915 | * |
||
916 | * @param integer $maxLength |
||
917 | * @param string|null $message |
||
918 | * @param string|null $propertyPath |
||
919 | * @param string $encoding |
||
920 | * @return Assert |
||
921 | * @throws AssertionFailedException |
||
922 | */ |
||
923 | public function maxLength($maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
943 | |||
944 | /** |
||
945 | * Assert that string length is between min,max lengths. |
||
946 | * |
||
947 | * @param integer $minLength |
||
948 | * @param integer $maxLength |
||
949 | * @param string|null $message |
||
950 | * @param string|null $propertyPath |
||
951 | * @param string $encoding |
||
952 | * @return Assert |
||
953 | * @throws AssertionFailedException |
||
954 | */ |
||
955 | public function betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
987 | |||
988 | /** |
||
989 | * Assert that string starts with a sequence of chars. |
||
990 | * |
||
991 | * @param string $needle |
||
992 | * @param string|null $message |
||
993 | * @param string|null $propertyPath |
||
994 | * @param string $encoding |
||
995 | * @return Assert |
||
996 | * @throws AssertionFailedException |
||
997 | */ |
||
998 | View Code Duplication | public function startsWith($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
|
1017 | |||
1018 | /** |
||
1019 | * Assert that string ends with a sequence of chars. |
||
1020 | * |
||
1021 | * @param string $needle |
||
1022 | * @param string|null $message |
||
1023 | * @param string|null $propertyPath |
||
1024 | * @param string $encoding |
||
1025 | * @return Assert |
||
1026 | * @throws AssertionFailedException |
||
1027 | */ |
||
1028 | public function endsWith($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
1048 | |||
1049 | /** |
||
1050 | * Assert that string contains a sequence of chars. |
||
1051 | * |
||
1052 | * @param string $needle |
||
1053 | * @param string|null $message |
||
1054 | * @param string|null $propertyPath |
||
1055 | * @param string $encoding |
||
1056 | * @return Assert |
||
1057 | * @throws AssertionFailedException |
||
1058 | */ |
||
1059 | View Code Duplication | public function contains($needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
|
1078 | |||
1079 | /** |
||
1080 | * Assert that value is in array of choices. |
||
1081 | * |
||
1082 | * @param array $choices |
||
1083 | * @param string|null $message |
||
1084 | * @param string|null $propertyPath |
||
1085 | * @return Assert |
||
1086 | * @throws AssertionFailedException |
||
1087 | */ |
||
1088 | public function choice(array $choices, $message = null, $propertyPath = null) |
||
1105 | |||
1106 | /** |
||
1107 | * Alias of {@see choice()} |
||
1108 | * |
||
1109 | * @throws AssertionFailedException |
||
1110 | * |
||
1111 | * @param array $choices |
||
1112 | * @param string|null $message |
||
1113 | * @param string|null $propertyPath |
||
1114 | * @return $this |
||
1115 | */ |
||
1116 | public function inArray(array $choices, $message = null, $propertyPath = null) |
||
1125 | |||
1126 | /** |
||
1127 | * Assert that value is numeric. |
||
1128 | * |
||
1129 | * @param string|null $message |
||
1130 | * @param string|null $propertyPath |
||
1131 | * @return Assert |
||
1132 | * @throws AssertionFailedException |
||
1133 | */ |
||
1134 | public function numeric($message = null, $propertyPath = null) |
||
1150 | |||
1151 | /** |
||
1152 | * @param string|null $message |
||
1153 | * @param string|null $propertyPath |
||
1154 | * @return Assert |
||
1155 | * @throws AssertionFailedException |
||
1156 | */ |
||
1157 | public function nonEmptyArray($message = null, $propertyPath = null) |
||
1162 | |||
1163 | /** |
||
1164 | * @param string|null $message |
||
1165 | * @param string|null $propertyPath |
||
1166 | * @return Assert |
||
1167 | * @throws AssertionFailedException |
||
1168 | */ |
||
1169 | public function nonEmptyInt($message = null, $propertyPath = null) |
||
1174 | |||
1175 | /** |
||
1176 | * @param string|null $message |
||
1177 | * @param string|null $propertyPath |
||
1178 | * @return Assert |
||
1179 | * @throws AssertionFailedException |
||
1180 | */ |
||
1181 | public function nonEmptyString($message = null, $propertyPath = null) |
||
1186 | |||
1187 | /** |
||
1188 | * Assert that value is an array. |
||
1189 | * |
||
1190 | * @param string|null $message |
||
1191 | * @param string|null $propertyPath |
||
1192 | * @return Assert |
||
1193 | * @throws AssertionFailedException |
||
1194 | */ |
||
1195 | public function isArray($message = null, $propertyPath = null) |
||
1211 | |||
1212 | /** |
||
1213 | * Assert that value is an array or a traversable object. |
||
1214 | * |
||
1215 | * @param string|null $message |
||
1216 | * @param string|null $propertyPath |
||
1217 | * @return Assert |
||
1218 | * @throws AssertionFailedException |
||
1219 | */ |
||
1220 | public function isTraversable($message = null, $propertyPath = null) |
||
1236 | |||
1237 | /** |
||
1238 | * Assert that value is an array or an array-accessible object. |
||
1239 | * |
||
1240 | * @param string|null $message |
||
1241 | * @param string|null $propertyPath |
||
1242 | * @return Assert |
||
1243 | * @throws AssertionFailedException |
||
1244 | */ |
||
1245 | public function isArrayAccessible($message = null, $propertyPath = null) |
||
1261 | |||
1262 | /** |
||
1263 | * Assert that key exists in an array |
||
1264 | * |
||
1265 | * @param string|integer $key |
||
1266 | * @param string|null $message |
||
1267 | * @param string|null $propertyPath |
||
1268 | * @return Assert |
||
1269 | * @throws AssertionFailedException |
||
1270 | */ |
||
1271 | public function keyExists($key, $message = null, $propertyPath = null) |
||
1288 | |||
1289 | /** |
||
1290 | * Assert that keys exist in array |
||
1291 | * |
||
1292 | * @param array $keys |
||
1293 | * @param string|null $message |
||
1294 | * @param string|null $propertyPath |
||
1295 | * @return Assert |
||
1296 | * @throws AssertionFailedException |
||
1297 | */ |
||
1298 | View Code Duplication | public function keysExist($keys, $message = null, $propertyPath = null) |
|
1319 | |||
1320 | /** |
||
1321 | * Assert that property exists in array |
||
1322 | * |
||
1323 | * @param string|integer $key |
||
1324 | * @param string|null $message |
||
1325 | * @param string|null $propertyPath |
||
1326 | * @return Assert |
||
1327 | * @throws AssertionFailedException |
||
1328 | */ |
||
1329 | View Code Duplication | public function propertyExists($key, $message = null, $propertyPath = null) |
|
1347 | |||
1348 | /** |
||
1349 | * Assert that properties exists in array |
||
1350 | * |
||
1351 | * @param array $keys |
||
1352 | * @param string|null $message |
||
1353 | * @param string|null $propertyPath |
||
1354 | * @return Assert |
||
1355 | * @throws AssertionFailedException |
||
1356 | */ |
||
1357 | View Code Duplication | public function propertiesExist(array $keys, $message = null, $propertyPath = null) |
|
1379 | |||
1380 | /** |
||
1381 | * Assert that string is valid utf8 |
||
1382 | * |
||
1383 | * @param string|null $message |
||
1384 | * @param string|null $propertyPath |
||
1385 | * @return Assert |
||
1386 | * @throws AssertionFailedException |
||
1387 | */ |
||
1388 | public function utf8($message = null, $propertyPath = null) |
||
1406 | |||
1407 | |||
1408 | /** |
||
1409 | * Assert that string is valid utf8 |
||
1410 | * |
||
1411 | * @param string|null $message |
||
1412 | * @param string|null $propertyPath |
||
1413 | * @return Assert |
||
1414 | * @throws AssertionFailedException |
||
1415 | */ |
||
1416 | View Code Duplication | public function ascii($message = null, $propertyPath = null) |
|
1434 | |||
1435 | /** |
||
1436 | * Assert that key exists in an array/array-accessible object using isset() |
||
1437 | * |
||
1438 | * @param string|integer $key |
||
1439 | * @param string|null $message |
||
1440 | * @param string|null $propertyPath |
||
1441 | * @return Assert |
||
1442 | * @throws AssertionFailedException |
||
1443 | */ |
||
1444 | public function keyIsset($key, $message = null, $propertyPath = null) |
||
1461 | |||
1462 | /** |
||
1463 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
1464 | * |
||
1465 | * @param string|integer $key |
||
1466 | * @param string|null $message |
||
1467 | * @param string|null $propertyPath |
||
1468 | * @return Assert |
||
1469 | * @throws AssertionFailedException |
||
1470 | */ |
||
1471 | public function notEmptyKey($key, $message = null, $propertyPath = null) |
||
1481 | |||
1482 | /** |
||
1483 | * Assert that value is not blank |
||
1484 | * |
||
1485 | * @param string|null $message |
||
1486 | * @param string|null $propertyPath |
||
1487 | * @return Assert |
||
1488 | * @throws AssertionFailedException |
||
1489 | */ |
||
1490 | View Code Duplication | public function notBlank($message = null, $propertyPath = null) |
|
1506 | |||
1507 | /** |
||
1508 | * Assert that value is instance of given class-name. |
||
1509 | * |
||
1510 | * @param string $className |
||
1511 | * @param string|null $message |
||
1512 | * @param string|null $propertyPath |
||
1513 | * @return Assert |
||
1514 | * @throws AssertionFailedException |
||
1515 | */ |
||
1516 | public function isInstanceOf($className, $message = null, $propertyPath = null) |
||
1533 | |||
1534 | /** |
||
1535 | * Assert that value is not instance of given class-name. |
||
1536 | * |
||
1537 | * @param string $className |
||
1538 | * @param string|null $message |
||
1539 | * @param string|null $propertyPath |
||
1540 | * @return Assert |
||
1541 | * @throws AssertionFailedException |
||
1542 | */ |
||
1543 | public function notIsInstanceOf($className, $message = null, $propertyPath = null) |
||
1560 | |||
1561 | /** |
||
1562 | * Assert that value is subclass of given class-name. |
||
1563 | * |
||
1564 | * @param string $className |
||
1565 | * @param string|null $message |
||
1566 | * @param string|null $propertyPath |
||
1567 | * @return Assert |
||
1568 | * @throws AssertionFailedException |
||
1569 | */ |
||
1570 | public function subclassOf($className, $message = null, $propertyPath = null) |
||
1587 | |||
1588 | /** |
||
1589 | * Assert that value is in range of numbers. |
||
1590 | * |
||
1591 | * @param integer $minValue |
||
1592 | * @param integer $maxValue |
||
1593 | * @param string|null $message |
||
1594 | * @param string|null $propertyPath |
||
1595 | * @return Assert |
||
1596 | * @throws AssertionFailedException |
||
1597 | */ |
||
1598 | public function range($minValue, $maxValue, $message = null, $propertyPath = null) |
||
1620 | |||
1621 | /** |
||
1622 | * Assert that a value is at least as big as a given limit |
||
1623 | * |
||
1624 | * @param mixed $minValue |
||
1625 | * @param string|null $message |
||
1626 | * @param string|null $propertyPath |
||
1627 | * @return Assert |
||
1628 | * @throws AssertionFailedException |
||
1629 | */ |
||
1630 | public function min($minValue, $message = null, $propertyPath = null) |
||
1648 | |||
1649 | /** |
||
1650 | * Assert that a number is smaller as a given limit |
||
1651 | * |
||
1652 | * @param mixed $maxValue |
||
1653 | * @param string|null $message |
||
1654 | * @param string|null $propertyPath |
||
1655 | * @return Assert |
||
1656 | * @throws AssertionFailedException |
||
1657 | */ |
||
1658 | public function max($maxValue, $message = null, $propertyPath = null) |
||
1676 | |||
1677 | /** |
||
1678 | * Assert that a file exists |
||
1679 | * |
||
1680 | * @param string|null $message |
||
1681 | * @param string|null $propertyPath |
||
1682 | * @return Assert |
||
1683 | * @throws AssertionFailedException |
||
1684 | */ |
||
1685 | public function file($message = null, $propertyPath = null) |
||
1703 | |||
1704 | /** |
||
1705 | * @param string|null $message |
||
1706 | * @param string|null $propertyPath |
||
1707 | * @return $this |
||
1708 | * @throws AssertionFailedException |
||
1709 | */ |
||
1710 | public function fileExists($message = null, $propertyPath = null) |
||
1728 | |||
1729 | /** |
||
1730 | * Assert that a directory exists |
||
1731 | * |
||
1732 | * @param string|null $message |
||
1733 | * @param string|null $propertyPath |
||
1734 | * @return Assert |
||
1735 | * @throws AssertionFailedException |
||
1736 | */ |
||
1737 | View Code Duplication | public function directory($message = null, $propertyPath = null) |
|
1754 | |||
1755 | /** |
||
1756 | * Assert that the value is something readable |
||
1757 | * |
||
1758 | * @param string|null $message |
||
1759 | * @param string|null $propertyPath |
||
1760 | * @return Assert |
||
1761 | * @throws AssertionFailedException |
||
1762 | */ |
||
1763 | View Code Duplication | public function readable($message = null, $propertyPath = null) |
|
1780 | |||
1781 | /** |
||
1782 | * Assert that the value is something writeable |
||
1783 | * |
||
1784 | * @param string|null $message |
||
1785 | * @param string|null $propertyPath |
||
1786 | * @return Assert |
||
1787 | * @throws AssertionFailedException |
||
1788 | */ |
||
1789 | View Code Duplication | public function writeable($message = null, $propertyPath = null) |
|
1806 | |||
1807 | /** |
||
1808 | * Assert that value is an email adress (using |
||
1809 | * input_filter/FILTER_VALIDATE_EMAIL). |
||
1810 | * |
||
1811 | * @param string|null $message |
||
1812 | * @param string|null $propertyPath |
||
1813 | * @return Assert |
||
1814 | * @throws AssertionFailedException |
||
1815 | */ |
||
1816 | public function email($message = null, $propertyPath = null) |
||
1846 | |||
1847 | /** |
||
1848 | * @param null $message |
||
1849 | * @param null $propertyPath |
||
1850 | * @return Assert |
||
1851 | * @throws AssertionFailedException |
||
1852 | */ |
||
1853 | public function emailPrefix($message = null, $propertyPath = null) |
||
1858 | |||
1859 | /** |
||
1860 | * Assert that value is an URL. |
||
1861 | * |
||
1862 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1863 | * |
||
1864 | * @param string|null $message |
||
1865 | * @param string|null $propertyPath |
||
1866 | * @return Assert |
||
1867 | * @throws AssertionFailedException |
||
1868 | * |
||
1869 | * |
||
1870 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1871 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1872 | */ |
||
1873 | public function url($message = null, $propertyPath = null) |
||
1906 | |||
1907 | /** |
||
1908 | * Assert that value is domain name. |
||
1909 | * |
||
1910 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1911 | * |
||
1912 | * @param string|null $message |
||
1913 | * @param string|null $propertyPath |
||
1914 | * @return Assert |
||
1915 | * @throws AssertionFailedException |
||
1916 | * |
||
1917 | */ |
||
1918 | public function domainName($message = null, $propertyPath = null) |
||
1936 | |||
1937 | /** |
||
1938 | * Assert that value is alphanumeric. |
||
1939 | * |
||
1940 | * @param string|null $message |
||
1941 | * @param string|null $propertyPath |
||
1942 | * @return Assert |
||
1943 | * @throws AssertionFailedException |
||
1944 | */ |
||
1945 | View Code Duplication | public function alnum($message = null, $propertyPath = null) |
|
1966 | |||
1967 | /** |
||
1968 | * Assert that the value is boolean True. |
||
1969 | * |
||
1970 | * @param string|null $message |
||
1971 | * @param string|null $propertyPath |
||
1972 | * @return Assert |
||
1973 | * @throws AssertionFailedException |
||
1974 | */ |
||
1975 | public function true($message = null, $propertyPath = null) |
||
1991 | |||
1992 | /** |
||
1993 | * Assert that the value is boolean True. |
||
1994 | * |
||
1995 | * @param string|null $message |
||
1996 | * @param string|null $propertyPath |
||
1997 | * @return Assert |
||
1998 | * @throws AssertionFailedException |
||
1999 | */ |
||
2000 | public function truthy($message = null, $propertyPath = null) |
||
2016 | |||
2017 | /** |
||
2018 | * Assert that the value is boolean False. |
||
2019 | * |
||
2020 | * @param string|null $message |
||
2021 | * @param string|null $propertyPath |
||
2022 | * @return Assert |
||
2023 | * @throws AssertionFailedException |
||
2024 | */ |
||
2025 | public function false($message = null, $propertyPath = null) |
||
2041 | |||
2042 | /** |
||
2043 | * Assert that the value is not boolean False. |
||
2044 | * |
||
2045 | * @param string|null $message |
||
2046 | * @param string|null $propertyPath |
||
2047 | * @return Assert |
||
2048 | * @throws AssertionFailedException |
||
2049 | */ |
||
2050 | public function notFalse($message = null, $propertyPath = null) |
||
2066 | |||
2067 | /** |
||
2068 | * Assert that the class exists. |
||
2069 | * |
||
2070 | * @param string|null $message |
||
2071 | * @param string|null $propertyPath |
||
2072 | * @return Assert |
||
2073 | * @throws AssertionFailedException |
||
2074 | */ |
||
2075 | public function classExists($message = null, $propertyPath = null) |
||
2091 | |||
2092 | /** |
||
2093 | * Assert that the class implements the interface |
||
2094 | * |
||
2095 | * @param string $interfaceName |
||
2096 | * @param string|null $message |
||
2097 | * @param string|null $propertyPath |
||
2098 | * @return Assert |
||
2099 | * @throws AssertionFailedException |
||
2100 | */ |
||
2101 | public function implementsInterface($interfaceName, $message = null, $propertyPath = null) |
||
2119 | |||
2120 | /** |
||
2121 | * Assert that the given string is a valid json string. |
||
2122 | * |
||
2123 | * NOTICE: |
||
2124 | * Since this does a json_decode to determine its validity |
||
2125 | * you probably should consider, when using the variable |
||
2126 | * content afterwards, just to decode and check for yourself instead |
||
2127 | * of using this assertion. |
||
2128 | * |
||
2129 | * @param string|null $message |
||
2130 | * @param string|null $propertyPath |
||
2131 | * @return Assert |
||
2132 | * @throws AssertionFailedException |
||
2133 | */ |
||
2134 | public function isJsonString($message = null, $propertyPath = null) |
||
2150 | |||
2151 | /** |
||
2152 | * Assert that the given string is a valid UUID |
||
2153 | * |
||
2154 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
2155 | * |
||
2156 | * @param string|null $message |
||
2157 | * @param string|null $propertyPath |
||
2158 | * @return Assert |
||
2159 | * @throws AssertionFailedException |
||
2160 | */ |
||
2161 | public function uuid($message = null, $propertyPath = null) |
||
2182 | |||
2183 | /** |
||
2184 | * Assert that the count of countable is equal to count. |
||
2185 | * |
||
2186 | * @param int $count |
||
2187 | * @param string $message |
||
2188 | * @param string $propertyPath |
||
2189 | * @return Assert |
||
2190 | * @throws AssertionFailedException |
||
2191 | */ |
||
2192 | public function count($count, $message = null, $propertyPath = null) |
||
2209 | |||
2210 | /** |
||
2211 | * @param $func |
||
2212 | * @param $args |
||
2213 | * @return bool |
||
2214 | * @throws AssertionFailedException |
||
2215 | */ |
||
2216 | protected function doAllOrNullOr($func, $args) |
||
2237 | |||
2238 | /** |
||
2239 | * Determines if the values array has every choice as key and that this choice has content. |
||
2240 | * |
||
2241 | * @param array $choices |
||
2242 | * @param string|null $message |
||
2243 | * @param string|null $propertyPath |
||
2244 | * @return $this |
||
2245 | */ |
||
2246 | public function choicesNotEmpty(array $choices, $message = null, $propertyPath = null) |
||
2259 | |||
2260 | /** |
||
2261 | * Determines that the named method is defined in the provided object. |
||
2262 | * |
||
2263 | * @param mixed $object |
||
2264 | * @param string|null $message |
||
2265 | * @param string|null $propertyPath |
||
2266 | * @returns Assert |
||
2267 | * @throws |
||
2268 | */ |
||
2269 | View Code Duplication | public function methodExists($object, $message = null, $propertyPath = null) |
|
2286 | |||
2287 | /** |
||
2288 | * Determines that the provided value is an object. |
||
2289 | * |
||
2290 | * @param string|null $message |
||
2291 | * @param string|null $propertyPath |
||
2292 | * @return $this |
||
2293 | * @throws AssertionFailedException |
||
2294 | */ |
||
2295 | public function isObject($message = null, $propertyPath = null) |
||
2311 | |||
2312 | /** |
||
2313 | * Make a string version of a value. |
||
2314 | * |
||
2315 | * @param $value |
||
2316 | * @return string |
||
2317 | */ |
||
2318 | private function stringify($value) |
||
2351 | } |
||
2352 | |||
2353 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.