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 = 1; |
||
27 | const INVALID_INTEGER = 2; |
||
28 | const INVALID_DIGIT = 3; |
||
29 | const INVALID_INTEGERISH = 4; |
||
30 | const INVALID_BOOLEAN = 5; |
||
31 | const VALUE_EMPTY = 6; |
||
32 | const VALUE_NULL = 7; |
||
33 | const INVALID_STRING = 8; |
||
34 | const INVALID_REGEX = 9; |
||
35 | const INVALID_MIN_LENGTH = 10; |
||
36 | const INVALID_MAX_LENGTH = 11; |
||
37 | const INVALID_STRING_START = 12; |
||
38 | const INVALID_STRING_CONTAINS = 13; |
||
39 | const INVALID_CHOICE = 14; |
||
40 | const INVALID_NUMERIC = 15; |
||
41 | const INVALID_ARRAY = 16; |
||
42 | const INVALID_KEY_EXISTS = 17; |
||
43 | const INVALID_NOT_BLANK = 18; |
||
44 | const INVALID_INSTANCE_OF = 19; |
||
45 | const INVALID_SUBCLASS_OF = 20; |
||
46 | const INVALID_RANGE = 21; |
||
47 | const INVALID_ALNUM = 22; |
||
48 | const INVALID_TRUE = 23; |
||
49 | const INVALID_EQ = 24; |
||
50 | const INVALID_SAME = 25; |
||
51 | const INVALID_MIN = 26; |
||
52 | const INVALID_MAX = 27; |
||
53 | const INVALID_LENGTH = 28; |
||
54 | const INVALID_FALSE = 29; |
||
55 | const INVALID_STRING_END = 30; |
||
56 | const INVALID_UUID = 31; |
||
57 | const INVALID_COUNT = 32; |
||
58 | const INVALID_NOT_EQ = 33; |
||
59 | const INVALID_NOT_SAME = 34; |
||
60 | const INVALID_TRAVERSABLE = 35; |
||
61 | const INVALID_ARRAY_ACCESSIBLE = 36; |
||
62 | const INVALID_KEY_ISSET = 37; |
||
63 | const INVALID_SAMACCOUNTNAME = 38; |
||
64 | const INVALID_USERPRINCIPALNAME = 39; |
||
65 | const INVALID_DIRECTORY = 40; |
||
66 | const INVALID_FILE = 41; |
||
67 | const INVALID_READABLE = 42; |
||
68 | const INVALID_WRITEABLE = 43; |
||
69 | const INVALID_CLASS = 44; |
||
70 | const INVALID_EMAIL = 45; |
||
71 | const INTERFACE_NOT_IMPLEMENTED = 46; |
||
72 | const INVALID_URL = 47; |
||
73 | const INVALID_NOT_INSTANCE_OF = 48; |
||
74 | const VALUE_NOT_EMPTY = 49; |
||
75 | const INVALID_JSON_STRING = 50; |
||
76 | const INVALID_OBJECT = 51; |
||
77 | const INVALID_METHOD = 52; |
||
78 | const INVALID_SCALAR = 53; |
||
79 | const INVALID_DATE = 54; |
||
80 | const INVALID_CALLABLE = 55; |
||
81 | const INVALID_KEYS_EXIST = 56; |
||
82 | const INVALID_PROPERTY_EXISTS = 57; |
||
83 | const INVALID_PROPERTIES_EXIST = 58; |
||
84 | const INVALID_UTF8 = 59; |
||
85 | const INVALID_DOMAIN_NAME = 60; |
||
86 | const INVALID_NOT_FALSE = 61; |
||
87 | const INVALID_FILE_OR_DIR = 62; |
||
88 | const INVALID_ASCII = 63; |
||
89 | const INVALID_NOT_REGEX = 64; |
||
90 | const INVALID_GREATER_THAN = 65; |
||
91 | const INVALID_LESS_THAN = 66; |
||
92 | const INVALID_GREATER_THAN_OR_EQ = 67; |
||
93 | const INVALID_LESS_THAN_OR_EQ = 68; |
||
94 | const INVALID_IP_ADDRESS = 69; |
||
95 | const INVALID_AUS_MOBILE = 70; |
||
96 | const INVALID_ISNI = 71; |
||
97 | |||
98 | const EMERGENCY = 'emergency'; |
||
99 | const ALERT = 'alert'; |
||
100 | const CRITICAL = 'critical'; |
||
101 | const ERROR = 'error'; |
||
102 | const WARNING = 'warning'; |
||
103 | const NOTICE = 'notice'; |
||
104 | const INFO = 'info'; |
||
105 | const DEBUG = 'debug'; |
||
106 | |||
107 | /** @var bool */ |
||
108 | protected $nullOr = false; |
||
109 | |||
110 | /** @var bool */ |
||
111 | protected $emptyOr = false; |
||
112 | |||
113 | /** @var mixed */ |
||
114 | protected $value = null; |
||
115 | |||
116 | /** @var bool */ |
||
117 | protected $all = false; |
||
118 | |||
119 | /** @var string */ |
||
120 | protected $fieldName = ''; |
||
121 | |||
122 | /** @var string */ |
||
123 | protected $propertyPath = ''; |
||
124 | |||
125 | /** @var string */ |
||
126 | protected $level = 'critical'; |
||
127 | |||
128 | /** @var int */ |
||
129 | protected $overrideCode = null; |
||
130 | |||
131 | /** @var string */ |
||
132 | protected $overrideError = ''; |
||
133 | |||
134 | /** |
||
135 | * Exception to throw when an assertion failed. |
||
136 | * |
||
137 | * @var string |
||
138 | */ |
||
139 | protected $exceptionClass = AssertionFailedException::class; |
||
140 | |||
141 | /** |
||
142 | * @param mixed $value |
||
143 | */ |
||
144 | public function __construct($value) |
||
145 | { |
||
146 | $this->value($value); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param \Closure[] $validators |
||
151 | * @return array |
||
152 | */ |
||
153 | public static function runValidators(array $validators) : array |
||
154 | { |
||
155 | $errors = []; |
||
156 | foreach ( $validators as $fieldName => $validator ) |
||
157 | { |
||
158 | try |
||
159 | { |
||
160 | $validator->__invoke(); |
||
161 | } |
||
162 | catch ( AssertionFailedException $e ) |
||
163 | { |
||
164 | $errors[$fieldName] = $e->getMessage(); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return $errors; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param $value |
||
173 | * @param string $fieldName |
||
174 | * @param int $code |
||
175 | * @param string $error |
||
176 | * @param string $level |
||
177 | * @return Assert |
||
178 | */ |
||
179 | View Code Duplication | public static function that($value, string $fieldName='', int $code=0, string $error='', string $level=Assert::WARNING) : Assert |
|
201 | |||
202 | /** |
||
203 | * @param mixed $value |
||
204 | * @return Assert |
||
205 | */ |
||
206 | public function reset($value) : Assert |
||
210 | |||
211 | /** |
||
212 | * @param mixed $value |
||
213 | * @return Assert |
||
214 | */ |
||
215 | public function value($value) : Assert |
||
221 | |||
222 | /** |
||
223 | * Allow value to pass assertion if it is null. |
||
224 | * |
||
225 | * @param bool $nullOr |
||
226 | * @return Assert |
||
227 | */ |
||
228 | public function nullOr(bool $nullOr = true) : Assert |
||
234 | |||
235 | /** |
||
236 | * Allow value to pass assertion if it is empty. |
||
237 | * |
||
238 | * @param bool $emptyOr |
||
239 | * @return Assert |
||
240 | */ |
||
241 | public function emptyOr(bool $emptyOr = true) : Assert |
||
247 | |||
248 | /** |
||
249 | * Assert all values in the value array. |
||
250 | * |
||
251 | * @param bool $all |
||
252 | * @return Assert |
||
253 | */ |
||
254 | public function all(bool $all = true) : Assert |
||
260 | |||
261 | /** |
||
262 | * Helper method that handles building the assertion failure exceptions. |
||
263 | * They are returned from this method so that the stack trace still shows |
||
264 | * the assertions method. |
||
265 | * |
||
266 | * @param string $message |
||
267 | * @param int $code |
||
268 | * @param string $fieldName |
||
269 | * @param array $constraints |
||
270 | * @param string $level |
||
271 | * @return AssertionFailedException |
||
272 | */ |
||
273 | protected function createException(string $message, int $code, string $fieldName, array $constraints=[], string $level='') : AssertionFailedException |
||
281 | |||
282 | /** |
||
283 | * @param string $exceptionClass |
||
284 | * @return Assert |
||
285 | */ |
||
286 | public function setExceptionClass(string $exceptionClass) : Assert |
||
292 | |||
293 | /** |
||
294 | * @param int $code |
||
295 | * @return Assert |
||
296 | */ |
||
297 | public function code(int $code) : Assert |
||
303 | |||
304 | /** |
||
305 | * @param string $fieldName |
||
306 | * @return Assert |
||
307 | */ |
||
308 | public function fieldName(string $fieldName) : Assert |
||
314 | |||
315 | /** |
||
316 | * @deprecated |
||
317 | * @param string $fieldName |
||
318 | * @return Assert |
||
319 | */ |
||
320 | public function name(string $fieldName) : Assert |
||
326 | |||
327 | /** |
||
328 | * @param string $level |
||
329 | * @return Assert |
||
330 | */ |
||
331 | public function level(string $level) : Assert |
||
337 | |||
338 | /** |
||
339 | * @param string $error |
||
340 | * @return Assert |
||
341 | */ |
||
342 | public function error(string $error) : Assert |
||
348 | |||
349 | /** |
||
350 | * User controlled way to define a sub-property causing |
||
351 | * the failure of a currently asserted objects. |
||
352 | * |
||
353 | * Useful to transport information about the nature of the error |
||
354 | * back to higher layers. |
||
355 | * |
||
356 | * @param string $propertyPath |
||
357 | * @return Assert |
||
358 | */ |
||
359 | public function propertyPath(string $propertyPath) : Assert |
||
365 | |||
366 | /** |
||
367 | * Assert that value is equal to a provided value (using == ). |
||
368 | * |
||
369 | * @param mixed $value2 |
||
370 | * @param string $message |
||
371 | * @param string $fieldName |
||
372 | * @return Assert |
||
373 | * @throws AssertionFailedException |
||
374 | */ |
||
375 | public function eq($value2, string $message='', string $fieldName='') : Assert |
||
395 | |||
396 | /** |
||
397 | * Assert that value is greater than a provided value (exclusive). |
||
398 | * |
||
399 | * @param mixed $value2 |
||
400 | * @param string $message |
||
401 | * @param string $fieldName |
||
402 | * @return Assert |
||
403 | * @throws AssertionFailedException |
||
404 | */ |
||
405 | public function greaterThan($value2, string $message='', string $fieldName='') : Assert |
||
425 | |||
426 | /** |
||
427 | * Assert that value is greater than or equal to a provided value (inclusive). |
||
428 | * |
||
429 | * @param mixed $value2 |
||
430 | * @param string $message |
||
431 | * @param string $fieldName |
||
432 | * @return Assert |
||
433 | * @throws AssertionFailedException |
||
434 | */ |
||
435 | public function greaterThanOrEq($value2, string $message='', string $fieldName='') : Assert |
||
455 | |||
456 | /** |
||
457 | * Assert that value is less than a provided value (exclusive). |
||
458 | * |
||
459 | * @param mixed $value2 |
||
460 | * @param string $message |
||
461 | * @param string $fieldName |
||
462 | * @return Assert |
||
463 | * @throws AssertionFailedException |
||
464 | */ |
||
465 | public function lessThan($value2, string $message='', string $fieldName='') : Assert |
||
485 | |||
486 | /** |
||
487 | * Assert that value is less than or equal to a provided value (inclusive). |
||
488 | * |
||
489 | * @param mixed $value2 |
||
490 | * @param string $message |
||
491 | * @param string $fieldName |
||
492 | * @return Assert |
||
493 | * @throws AssertionFailedException |
||
494 | */ |
||
495 | public function lessThanOrEq($value2, string $message='', string $fieldName='') : Assert |
||
515 | |||
516 | /** |
||
517 | * Assert that value is the same as a provided value (using === ). |
||
518 | * |
||
519 | * @param mixed $value2 |
||
520 | * @param string $message |
||
521 | * @param string $fieldName |
||
522 | * @return Assert |
||
523 | * @throws AssertionFailedException |
||
524 | */ |
||
525 | public function same($value2, string $message='', string $fieldName='') : Assert |
||
545 | |||
546 | /** |
||
547 | * Assert that value is not equal to a provided value (using == ). |
||
548 | * |
||
549 | * @param mixed $value2 |
||
550 | * @param string $message |
||
551 | * @param string $fieldName |
||
552 | * @return Assert |
||
553 | * @throws AssertionFailedException |
||
554 | */ |
||
555 | public function notEq($value2, string $message='', string $fieldName='') : Assert |
||
575 | |||
576 | /** |
||
577 | * Assert that value can be called as a function (using is_callable()). |
||
578 | * |
||
579 | * @param string $message |
||
580 | * @param string $fieldName |
||
581 | * @return Assert |
||
582 | * @throws AssertionFailedException |
||
583 | */ |
||
584 | public function isCallable(string $message='', string $fieldName='') : Assert |
||
603 | |||
604 | /** |
||
605 | * Assert that value is not the same as a provided value (using === ). |
||
606 | * |
||
607 | * @param mixed $value2 |
||
608 | * @param string $message |
||
609 | * @param string $fieldName |
||
610 | * @return Assert |
||
611 | * @throws AssertionFailedException |
||
612 | */ |
||
613 | public function notSame($value2, string $message='', string $fieldName='') : Assert |
||
633 | |||
634 | /** |
||
635 | * Assert that value is a valid ID (non-empty, non-zero, valid integer). |
||
636 | * |
||
637 | * @param string $message |
||
638 | * @param string $fieldName |
||
639 | * @return Assert |
||
640 | * @throws AssertionFailedException |
||
641 | */ |
||
642 | View Code Duplication | public function id(string $message='', string $fieldName='') : Assert |
|
649 | |||
650 | /** |
||
651 | * Assert that value is a unsigned int (non-empty valid integer, can be zero). |
||
652 | * @param string $message |
||
653 | * @param string $fieldName |
||
654 | * @return Assert |
||
655 | * @throws AssertionFailedException |
||
656 | */ |
||
657 | View Code Duplication | public function unsignedInt(string $message='', string $fieldName='') : Assert |
|
664 | |||
665 | /** |
||
666 | * Assert that value is a valid flag (0 or 1). |
||
667 | * |
||
668 | * @param string $message |
||
669 | * @param string $fieldName |
||
670 | * @return Assert |
||
671 | * @throws AssertionFailedException |
||
672 | */ |
||
673 | View Code Duplication | public function flag(string $message='', string $fieldName='') : Assert |
|
680 | |||
681 | /** |
||
682 | * Assert that value is a valid status (-1, 0, or 1). |
||
683 | * |
||
684 | * @param string $message |
||
685 | * @param string $fieldName |
||
686 | * @return Assert |
||
687 | * @throws AssertionFailedException |
||
688 | */ |
||
689 | public function status(string $message='', string $fieldName='') : Assert |
||
696 | |||
697 | /** |
||
698 | * Assert that value is null or a valid ID. |
||
699 | * |
||
700 | * @param string $message |
||
701 | * @param string $fieldName |
||
702 | * @return Assert |
||
703 | * @throws AssertionFailedException |
||
704 | */ |
||
705 | public function nullOrId(string $message='', string $fieldName='') : Assert |
||
709 | |||
710 | /** |
||
711 | * Assert that values are all valid IDs. |
||
712 | * |
||
713 | * @param string $message |
||
714 | * @param string $fieldName |
||
715 | * @return Assert |
||
716 | * @throws AssertionFailedException |
||
717 | */ |
||
718 | public function allIds(string $message='', string $fieldName='') : Assert |
||
722 | |||
723 | /** |
||
724 | * Alias of {@see integer()}. |
||
725 | * |
||
726 | * @param string $message |
||
727 | * @param string $fieldName |
||
728 | * @return Assert |
||
729 | * @throws AssertionFailedException |
||
730 | */ |
||
731 | public function int(string $message='', string $fieldName='') : Assert |
||
735 | |||
736 | /** |
||
737 | * Assert that value is a valid PHP integer. |
||
738 | * |
||
739 | * @param string $message |
||
740 | * @param string $fieldName |
||
741 | * @return Assert |
||
742 | * @throws AssertionFailedException |
||
743 | */ |
||
744 | public function integer(string $message='', string $fieldName='') : Assert |
||
763 | |||
764 | /** |
||
765 | * Assert that value is a valid PHP float. |
||
766 | * |
||
767 | * @param string $message |
||
768 | * @param string $fieldName |
||
769 | * @return Assert |
||
770 | * @throws AssertionFailedException |
||
771 | */ |
||
772 | public function float(string $message='', string $fieldName='') : Assert |
||
791 | |||
792 | /** |
||
793 | * Assert that value (integer or integer'ish) is a digit. |
||
794 | * |
||
795 | * @param string $message |
||
796 | * @param string $fieldName |
||
797 | * @return Assert |
||
798 | * @throws AssertionFailedException |
||
799 | */ |
||
800 | public function digit(string $message='', string $fieldName='') : Assert |
||
819 | |||
820 | /** |
||
821 | * Assert that value is a valid date. |
||
822 | * |
||
823 | * @param string $message |
||
824 | * @param string $fieldName |
||
825 | * @return Assert |
||
826 | * @throws AssertionFailedException |
||
827 | */ |
||
828 | public function date(string $message='', string $fieldName='') : Assert |
||
852 | |||
853 | /** |
||
854 | * @param $afterDate |
||
855 | * @param string $message |
||
856 | * @param string $fieldName |
||
857 | * @return $this |
||
858 | * @throws AssertionFailedException |
||
859 | */ |
||
860 | public function after($afterDate, string $message='', string $fieldName='') |
||
880 | |||
881 | /** |
||
882 | * Assert that value is a PHP integer'ish. |
||
883 | * |
||
884 | * @param string $message |
||
885 | * @param string $fieldName |
||
886 | * @return Assert |
||
887 | * @throws AssertionFailedException |
||
888 | */ |
||
889 | public function integerish(string $message='', string $fieldName='') : Assert |
||
908 | |||
909 | /** |
||
910 | * Assert that value is a valid PHP boolean. |
||
911 | * |
||
912 | * @param string $message |
||
913 | * @param string $fieldName |
||
914 | * @return Assert |
||
915 | * @throws AssertionFailedException |
||
916 | */ |
||
917 | public function boolean(string $message='', string $fieldName='') : Assert |
||
936 | |||
937 | /** |
||
938 | * Assert that value is a valid PHP scalar. |
||
939 | * |
||
940 | * @param string $message |
||
941 | * @param string $fieldName |
||
942 | * @return Assert |
||
943 | * @throws AssertionFailedException |
||
944 | */ |
||
945 | public function scalar(string $message='', string $fieldName='') : Assert |
||
964 | |||
965 | /** |
||
966 | * Assert that value is not empty. |
||
967 | * |
||
968 | * @param string $message |
||
969 | * @param string $fieldName |
||
970 | * @return Assert |
||
971 | * @throws AssertionFailedException |
||
972 | */ |
||
973 | View Code Duplication | public function notEmpty(string $message='', string $fieldName='') : Assert |
|
992 | |||
993 | /** |
||
994 | * Assert that value is empty. |
||
995 | * |
||
996 | * @param string $message |
||
997 | * @param string $fieldName |
||
998 | * @return Assert |
||
999 | * @throws AssertionFailedException |
||
1000 | */ |
||
1001 | public function noContent(string $message='', string $fieldName='') : Assert |
||
1020 | |||
1021 | /** |
||
1022 | * Assert that value is not null. |
||
1023 | * |
||
1024 | * @param string $message |
||
1025 | * @param string $fieldName |
||
1026 | * @return Assert |
||
1027 | * @throws AssertionFailedException |
||
1028 | */ |
||
1029 | public function notNull(string $message='', string $fieldName='') : Assert |
||
1048 | |||
1049 | /** |
||
1050 | * Assert that value is a string |
||
1051 | * |
||
1052 | * @param string $message |
||
1053 | * @param string $fieldName |
||
1054 | * @return Assert |
||
1055 | * @throws AssertionFailedException |
||
1056 | */ |
||
1057 | public function string(string $message='', string $fieldName='') : Assert |
||
1077 | |||
1078 | /** |
||
1079 | * Assert that value matches a provided Regex. |
||
1080 | * |
||
1081 | * @param string $pattern |
||
1082 | * @param string $message |
||
1083 | * @param string $fieldName |
||
1084 | * @return Assert |
||
1085 | * @throws AssertionFailedException |
||
1086 | */ |
||
1087 | public function regex(string $pattern, string $message='', string $fieldName='') : Assert |
||
1107 | |||
1108 | /** |
||
1109 | * Assert that value is a valid IP address. |
||
1110 | * |
||
1111 | * @param string $message |
||
1112 | * @param string $fieldName |
||
1113 | * @return Assert |
||
1114 | * @throws AssertionFailedException |
||
1115 | */ |
||
1116 | public function ipAddress(string $message='', string $fieldName='') : Assert |
||
1137 | |||
1138 | /** |
||
1139 | * Assert that value does not match a provided Regex. |
||
1140 | * |
||
1141 | * @param string $pattern |
||
1142 | * @param string $message |
||
1143 | * @param string $fieldName |
||
1144 | * @return Assert |
||
1145 | * @throws AssertionFailedException |
||
1146 | */ |
||
1147 | public function notRegex(string $pattern, string $message='', string $fieldName='') : Assert |
||
1167 | |||
1168 | /** |
||
1169 | * Assert that value is a string and has a character count which is equal to a given length. |
||
1170 | * |
||
1171 | * @param int $length |
||
1172 | * @param string $message |
||
1173 | * @param string $fieldName |
||
1174 | * @param string $encoding |
||
1175 | * @return Assert |
||
1176 | * @throws AssertionFailedException |
||
1177 | */ |
||
1178 | View Code Duplication | public function length(int $length, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
|
1201 | |||
1202 | /** |
||
1203 | * Assert that value is a string and has a character count which is |
||
1204 | * greater than or equal to a given lower limit ($minLength chars). |
||
1205 | * |
||
1206 | * @param int $minLength |
||
1207 | * @param string $message |
||
1208 | * @param string $fieldName |
||
1209 | * @param string $encoding |
||
1210 | * @return Assert |
||
1211 | * @throws AssertionFailedException |
||
1212 | */ |
||
1213 | View Code Duplication | public function minLength(int $minLength, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
|
1237 | |||
1238 | /** |
||
1239 | * Assert that value is a string and has a character count which is |
||
1240 | * less than or equal to given upper limit ($maxLength chars). |
||
1241 | * |
||
1242 | * @param int $maxLength |
||
1243 | * @param string $message |
||
1244 | * @param string $fieldName |
||
1245 | * @param string $encoding |
||
1246 | * @return Assert |
||
1247 | * @throws AssertionFailedException |
||
1248 | */ |
||
1249 | View Code Duplication | public function maxLength(int $maxLength, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
|
1272 | |||
1273 | /** |
||
1274 | * Assert that value has a length between min,max lengths (inclusive). |
||
1275 | * |
||
1276 | * @param int $minLength |
||
1277 | * @param int $maxLength |
||
1278 | * @param string $message |
||
1279 | * @param string $fieldName |
||
1280 | * @param string $encoding |
||
1281 | * @return Assert |
||
1282 | * @throws AssertionFailedException |
||
1283 | */ |
||
1284 | public function betweenLength(int $minLength, int $maxLength, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
||
1321 | |||
1322 | /** |
||
1323 | * Assert that value starts with a sequence of chars. |
||
1324 | * |
||
1325 | * @param string $needle |
||
1326 | * @param string $message |
||
1327 | * @param string $fieldName |
||
1328 | * @param string $encoding |
||
1329 | * @return Assert |
||
1330 | * @throws AssertionFailedException |
||
1331 | */ |
||
1332 | View Code Duplication | public function startsWith(string $needle, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
|
1354 | |||
1355 | /** |
||
1356 | * Assert that value ends with a sequence of chars. |
||
1357 | * |
||
1358 | * @param string $needle |
||
1359 | * @param string $message |
||
1360 | * @param string $fieldName |
||
1361 | * @param string $encoding |
||
1362 | * @return Assert |
||
1363 | * @throws AssertionFailedException |
||
1364 | */ |
||
1365 | public function endsWith(string $needle, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
||
1388 | |||
1389 | /** |
||
1390 | * Assert that value contains a sequence of chars. |
||
1391 | * |
||
1392 | * @param string $needle |
||
1393 | * @param string $message |
||
1394 | * @param string $fieldName |
||
1395 | * @param string $encoding |
||
1396 | * @return Assert |
||
1397 | * @throws AssertionFailedException |
||
1398 | */ |
||
1399 | View Code Duplication | public function contains(string $needle, string $message='', string $fieldName='', string $encoding='utf8') : Assert |
|
1421 | |||
1422 | /** |
||
1423 | * Assert that value is in an array of choices. |
||
1424 | * |
||
1425 | * @param array $choices |
||
1426 | * @param string $message |
||
1427 | * @param string $fieldName |
||
1428 | * @return Assert |
||
1429 | * @throws AssertionFailedException |
||
1430 | */ |
||
1431 | public function choice(array $choices, string $message='', string $fieldName='') : Assert |
||
1451 | |||
1452 | /** |
||
1453 | * Alias of {@see choice()} |
||
1454 | * |
||
1455 | * @param array $choices |
||
1456 | * @param string $message |
||
1457 | * @param string $fieldName |
||
1458 | * @return Assert |
||
1459 | * @throws AssertionFailedException |
||
1460 | */ |
||
1461 | public function inArray(array $choices, string $message='', string $fieldName='') : Assert |
||
1471 | |||
1472 | /** |
||
1473 | * Assert that value is numeric. |
||
1474 | * |
||
1475 | * @param string $message |
||
1476 | * @param string $fieldName |
||
1477 | * @return Assert |
||
1478 | * @throws AssertionFailedException |
||
1479 | */ |
||
1480 | public function numeric(string $message='', string $fieldName='') : Assert |
||
1499 | |||
1500 | /** |
||
1501 | * Assert that value is a non-empty array. |
||
1502 | * |
||
1503 | * @param string $message |
||
1504 | * @param string $fieldName |
||
1505 | * @return Assert |
||
1506 | * @throws AssertionFailedException |
||
1507 | */ |
||
1508 | public function nonEmptyArray(string $message='', string $fieldName='') : Assert |
||
1514 | |||
1515 | /** |
||
1516 | * Assert that value is a non-empty int. |
||
1517 | * |
||
1518 | * @param string $message |
||
1519 | * @param string $fieldName |
||
1520 | * @return Assert |
||
1521 | * @throws AssertionFailedException |
||
1522 | */ |
||
1523 | public function nonEmptyInt(string $message='', string $fieldName='') : Assert |
||
1529 | |||
1530 | /** |
||
1531 | * Assert that value is a non-empty string. |
||
1532 | * |
||
1533 | * @param string $message |
||
1534 | * @param string $fieldName |
||
1535 | * @return Assert |
||
1536 | * @throws AssertionFailedException |
||
1537 | */ |
||
1538 | public function nonEmptyString(string $message='', string $fieldName='') : Assert |
||
1544 | |||
1545 | /** |
||
1546 | * Assert that value is an array. |
||
1547 | * |
||
1548 | * @param string $message |
||
1549 | * @param string $fieldName |
||
1550 | * @return Assert |
||
1551 | * @throws AssertionFailedException |
||
1552 | */ |
||
1553 | public function isArray(string $message='', string $fieldName='') : Assert |
||
1572 | |||
1573 | /** |
||
1574 | * Assert that value is an array or a traversable object. |
||
1575 | * |
||
1576 | * @param string $message |
||
1577 | * @param string $fieldName |
||
1578 | * @return Assert |
||
1579 | * @throws AssertionFailedException |
||
1580 | */ |
||
1581 | public function isTraversable(string $message='', string $fieldName='') : Assert |
||
1600 | |||
1601 | /** |
||
1602 | * Assert that value is an array or an array-accessible object. |
||
1603 | * |
||
1604 | * @param string $message |
||
1605 | * @param string $fieldName |
||
1606 | * @return Assert |
||
1607 | * @throws AssertionFailedException |
||
1608 | */ |
||
1609 | public function isArrayAccessible(string $message='', string $fieldName='') : Assert |
||
1628 | |||
1629 | /** |
||
1630 | * Assert that key exists in the values array. |
||
1631 | * |
||
1632 | * @param string|integer $key |
||
1633 | * @param string $message |
||
1634 | * @param string $fieldName |
||
1635 | * @return Assert |
||
1636 | * @throws AssertionFailedException |
||
1637 | */ |
||
1638 | public function keyExists($key, string $message='', string $fieldName='') : Assert |
||
1658 | |||
1659 | /** |
||
1660 | * Assert that keys exist in the values array. |
||
1661 | * |
||
1662 | * @param array $keys |
||
1663 | * @param string $message |
||
1664 | * @param string $fieldName |
||
1665 | * @return Assert |
||
1666 | * @throws AssertionFailedException |
||
1667 | */ |
||
1668 | public function keysExist(array $keys, string $message='', string $fieldName='') : Assert |
||
1690 | |||
1691 | /** |
||
1692 | * Assert that a property (key) exists in the values array. |
||
1693 | * |
||
1694 | * @param string|integer $key |
||
1695 | * @param string|null $message |
||
1696 | * @param string|null $fieldName |
||
1697 | * @return Assert |
||
1698 | * @throws AssertionFailedException |
||
1699 | */ |
||
1700 | public function propertyExists($key, string $message='', string $fieldName='') : Assert |
||
1720 | |||
1721 | /** |
||
1722 | * Assert that properties (keys) exist in the values array. |
||
1723 | * |
||
1724 | * @param array $keys |
||
1725 | * @param string $message |
||
1726 | * @param string $fieldName |
||
1727 | * @return Assert |
||
1728 | * @throws AssertionFailedException |
||
1729 | */ |
||
1730 | public function propertiesExist(array $keys, string $message='', string $fieldName='') : Assert |
||
1753 | |||
1754 | /** |
||
1755 | * Assert that value is valid utf8. |
||
1756 | * |
||
1757 | * @param string $message |
||
1758 | * @param string $fieldName |
||
1759 | * @return Assert |
||
1760 | * @throws AssertionFailedException |
||
1761 | */ |
||
1762 | View Code Duplication | public function utf8(string $message='', string $fieldName='') : Assert |
|
1782 | |||
1783 | |||
1784 | /** |
||
1785 | * Assert that value is valid ascii. |
||
1786 | * |
||
1787 | * @param string $message |
||
1788 | * @param string $fieldName |
||
1789 | * @return Assert |
||
1790 | * @throws AssertionFailedException |
||
1791 | */ |
||
1792 | View Code Duplication | public function ascii(string $message='', string $fieldName='') : Assert |
|
1812 | |||
1813 | /** |
||
1814 | * Assert that key exists in an array/array-accessible object |
||
1815 | * (using isset()). |
||
1816 | * |
||
1817 | * @param string|integer $key |
||
1818 | * @param string|null $message |
||
1819 | * @param string|null $fieldName |
||
1820 | * @return Assert |
||
1821 | * @throws AssertionFailedException |
||
1822 | */ |
||
1823 | public function keyIsset($key, string $message='', string $fieldName='') : Assert |
||
1843 | |||
1844 | /** |
||
1845 | * Assert that key exists in an array/array-accessible object |
||
1846 | * and its value is not empty. |
||
1847 | * |
||
1848 | * @param string|integer $key |
||
1849 | * @param string|null $message |
||
1850 | * @param string|null $fieldName |
||
1851 | * @return Assert |
||
1852 | * @throws AssertionFailedException |
||
1853 | */ |
||
1854 | public function notEmptyKey($key, string $message='', string $fieldName='') : Assert |
||
1865 | |||
1866 | /** |
||
1867 | * Assert that value is not blank. |
||
1868 | * |
||
1869 | * @param string $message |
||
1870 | * @param string $fieldName |
||
1871 | * @return Assert |
||
1872 | * @throws AssertionFailedException |
||
1873 | */ |
||
1874 | View Code Duplication | public function notBlank(string $message='', string $fieldName='') : Assert |
|
1893 | |||
1894 | /** |
||
1895 | * Assert that value is an instance of a given class-name. |
||
1896 | * |
||
1897 | * @param string $className |
||
1898 | * @param string $message |
||
1899 | * @param string $fieldName |
||
1900 | * @return Assert |
||
1901 | * @throws AssertionFailedException |
||
1902 | */ |
||
1903 | public function isInstanceOf(string $className, string $message='', string $fieldName='') : Assert |
||
1923 | |||
1924 | /** |
||
1925 | * Assert that value is not an instance of given class-name. |
||
1926 | * |
||
1927 | * @param string $className |
||
1928 | * @param string $message |
||
1929 | * @param string $fieldName |
||
1930 | * @return Assert |
||
1931 | * @throws AssertionFailedException |
||
1932 | */ |
||
1933 | public function notIsInstanceOf(string $className, string $message='', string $fieldName='') : Assert |
||
1953 | |||
1954 | /** |
||
1955 | * Assert that value is a subclass of given class-name. |
||
1956 | * |
||
1957 | * @param string $className |
||
1958 | * @param string $message |
||
1959 | * @param string $fieldName |
||
1960 | * @return Assert |
||
1961 | * @throws AssertionFailedException |
||
1962 | */ |
||
1963 | public function subclassOf(string $className, string $message='', string $fieldName='') : Assert |
||
1983 | |||
1984 | /** |
||
1985 | * Assert that value is within a range of numbers (inclusive). |
||
1986 | * |
||
1987 | * @param float $minValue |
||
1988 | * @param float $maxValue |
||
1989 | * @param string $message |
||
1990 | * @param string $fieldName |
||
1991 | * @return Assert |
||
1992 | * @throws AssertionFailedException |
||
1993 | */ |
||
1994 | public function range(float $minValue, float $maxValue, string $message='', string $fieldName='') : Assert |
||
2019 | |||
2020 | /** |
||
2021 | * Assert that value is larger or equal to a given lower limit. |
||
2022 | * |
||
2023 | * @param int $minValue |
||
2024 | * @param string $message |
||
2025 | * @param string $fieldName |
||
2026 | * @return Assert |
||
2027 | * @throws AssertionFailedException |
||
2028 | */ |
||
2029 | public function min(int $minValue, string $message='', string $fieldName='') : Assert |
||
2050 | |||
2051 | /** |
||
2052 | * Assert that value is smaller than or equal to a given upper limit. |
||
2053 | * |
||
2054 | * @param int $maxValue |
||
2055 | * @param string $message |
||
2056 | * @param string $fieldName |
||
2057 | * @return Assert |
||
2058 | * @throws AssertionFailedException |
||
2059 | */ |
||
2060 | public function max(int $maxValue, string $message='', string $fieldName='') : Assert |
||
2081 | |||
2082 | /** |
||
2083 | * Assert that value is a file that exists. |
||
2084 | * |
||
2085 | * @param string $message |
||
2086 | * @param string $fieldName |
||
2087 | * @return Assert |
||
2088 | * @throws AssertionFailedException |
||
2089 | */ |
||
2090 | public function file(string $message='', string $fieldName='') : Assert |
||
2111 | |||
2112 | /** |
||
2113 | * Assert that value is a file or directory that exists. |
||
2114 | * |
||
2115 | * @param string $message |
||
2116 | * @param string $fieldName |
||
2117 | * @return Assert |
||
2118 | * @throws AssertionFailedException |
||
2119 | */ |
||
2120 | public function fileOrDirectoryExists(string $message='', string $fieldName='') : Assert |
||
2141 | |||
2142 | /** |
||
2143 | * Assert that value is a directory that exists. |
||
2144 | * |
||
2145 | * @param string $message |
||
2146 | * @param string $fieldName |
||
2147 | * @return Assert |
||
2148 | * @throws AssertionFailedException |
||
2149 | */ |
||
2150 | View Code Duplication | public function directory(string $message='', string $fieldName='') : Assert |
|
2170 | |||
2171 | /** |
||
2172 | * Assert that value is something readable. |
||
2173 | * |
||
2174 | * @param string $message |
||
2175 | * @param string $fieldName |
||
2176 | * @return Assert |
||
2177 | * @throws AssertionFailedException |
||
2178 | */ |
||
2179 | View Code Duplication | public function readable(string $message='', string $fieldName='') : Assert |
|
2199 | |||
2200 | /** |
||
2201 | * Assert that value is something writeable. |
||
2202 | * |
||
2203 | * @param string $message |
||
2204 | * @param string $fieldName |
||
2205 | * @return Assert |
||
2206 | * @throws AssertionFailedException |
||
2207 | */ |
||
2208 | View Code Duplication | public function writeable(string $message='', string $fieldName='') : Assert |
|
2228 | |||
2229 | /** |
||
2230 | * Assert that value is a valid email address (using input_filter/FILTER_VALIDATE_EMAIL). |
||
2231 | * |
||
2232 | * @param string $message |
||
2233 | * @param string $fieldName |
||
2234 | * @return Assert |
||
2235 | * @throws AssertionFailedException |
||
2236 | */ |
||
2237 | public function email(string $message='', string $fieldName='') : Assert |
||
2271 | |||
2272 | /** |
||
2273 | * Assert that value is a valid email prefix. |
||
2274 | * |
||
2275 | * @param string $message |
||
2276 | * @param string $fieldName |
||
2277 | * @return Assert |
||
2278 | * @throws AssertionFailedException |
||
2279 | */ |
||
2280 | public function emailPrefix(string $message='', string $fieldName='') : Assert |
||
2286 | |||
2287 | /** |
||
2288 | * Assert that value is a valid URL. |
||
2289 | * |
||
2290 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
2291 | * |
||
2292 | * @param string $message |
||
2293 | * @param string $fieldName |
||
2294 | * @return Assert |
||
2295 | * @throws AssertionFailedException |
||
2296 | * |
||
2297 | * |
||
2298 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
2299 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
2300 | */ |
||
2301 | public function url(string $message='', string $fieldName='') : Assert |
||
2337 | |||
2338 | /** |
||
2339 | * Assert that value is domain name. |
||
2340 | * |
||
2341 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
2342 | * |
||
2343 | * @param string $message |
||
2344 | * @param string $fieldName |
||
2345 | * @return Assert |
||
2346 | * @throws AssertionFailedException |
||
2347 | * |
||
2348 | */ |
||
2349 | public function domainName(string $message='', string $fieldName='') : Assert |
||
2370 | |||
2371 | /** |
||
2372 | * Assert that string is Australian Mobile Number |
||
2373 | * |
||
2374 | * @param string $message |
||
2375 | * @param string $fieldName |
||
2376 | * @return $this |
||
2377 | * @throws AssertionFailedException |
||
2378 | */ |
||
2379 | View Code Duplication | public function ausMobile(string $message='', string $fieldName='') : Assert |
|
2400 | |||
2401 | /** |
||
2402 | * @param string $value |
||
2403 | * @return bool |
||
2404 | */ |
||
2405 | public static function isAusMobile(string $value) : bool |
||
2409 | |||
2410 | /** |
||
2411 | * Assert that value is alphanumeric. |
||
2412 | * |
||
2413 | * @param string $message |
||
2414 | * @param string $fieldName |
||
2415 | * @return Assert |
||
2416 | * @throws AssertionFailedException |
||
2417 | */ |
||
2418 | View Code Duplication | public function alnum(string $message='', string $fieldName='') : Assert |
|
2442 | |||
2443 | /** |
||
2444 | * Assert that value is boolean True. |
||
2445 | * |
||
2446 | * @param string $message |
||
2447 | * @param string $fieldName |
||
2448 | * @return Assert |
||
2449 | * @throws AssertionFailedException |
||
2450 | */ |
||
2451 | public function true(string $message='', string $fieldName='') : Assert |
||
2470 | |||
2471 | /** |
||
2472 | * Assert that value is boolean True. |
||
2473 | * |
||
2474 | * @param string $message |
||
2475 | * @param string $fieldName |
||
2476 | * @return Assert |
||
2477 | * @throws AssertionFailedException |
||
2478 | */ |
||
2479 | public function truthy(string $message='', string $fieldName='') : Assert |
||
2498 | |||
2499 | /** |
||
2500 | * Assert that value is boolean False. |
||
2501 | * |
||
2502 | * @param string $message |
||
2503 | * @param string $fieldName |
||
2504 | * @return Assert |
||
2505 | * @throws AssertionFailedException |
||
2506 | */ |
||
2507 | public function false(string $message='', string $fieldName='') : Assert |
||
2526 | |||
2527 | /** |
||
2528 | * Assert that value is not boolean False. |
||
2529 | * |
||
2530 | * @param string $message |
||
2531 | * @param string $fieldName |
||
2532 | * @return Assert |
||
2533 | * @throws AssertionFailedException |
||
2534 | */ |
||
2535 | public function notFalse(string $message='', string $fieldName='') : Assert |
||
2554 | |||
2555 | /** |
||
2556 | * Assert that the class exists. |
||
2557 | * |
||
2558 | * @param string $message |
||
2559 | * @param string $fieldName |
||
2560 | * @return Assert |
||
2561 | * @throws AssertionFailedException |
||
2562 | */ |
||
2563 | public function classExists(string $message='', string $fieldName='') : Assert |
||
2582 | |||
2583 | /** |
||
2584 | * @param string $interfaceName |
||
2585 | * @param string $message |
||
2586 | * @param string $fieldName |
||
2587 | * @return Assert |
||
2588 | * @throws AssertionFailedException |
||
2589 | * @throws \ReflectionException |
||
2590 | */ |
||
2591 | public function implementsInterface(string $interfaceName, string $message='', string $fieldName='') : Assert |
||
2612 | |||
2613 | /** |
||
2614 | * Assert that value is a valid json string. |
||
2615 | * |
||
2616 | * NOTICE: |
||
2617 | * Since this does a json_decode to determine its validity |
||
2618 | * you probably should consider, when using the variable |
||
2619 | * content afterwards, just to decode and check for yourself instead |
||
2620 | * of using this assertion. |
||
2621 | * |
||
2622 | * @param string $message |
||
2623 | * @param string $fieldName |
||
2624 | * @return Assert |
||
2625 | * @throws AssertionFailedException |
||
2626 | */ |
||
2627 | public function isJsonString(string $message='', string $fieldName='') : Assert |
||
2646 | |||
2647 | /** |
||
2648 | * Assert that value is a valid UUID. |
||
2649 | * |
||
2650 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
2651 | * |
||
2652 | * @param string $message |
||
2653 | * @param string $fieldName |
||
2654 | * @return Assert |
||
2655 | * @throws AssertionFailedException |
||
2656 | */ |
||
2657 | public function uuid(string $message='', string $fieldName='') : Assert |
||
2681 | /** |
||
2682 | * Assert that value is a valid samAccountName (in line with Active |
||
2683 | * directory sAMAccountName restrictions for users). |
||
2684 | * |
||
2685 | * From: @link https://social.technet.microsoft.com/wiki/contents/articles/11216.active-directory-requirements-for-creating-objects.aspx#Objects_with_sAMAccountName_Attribute |
||
2686 | * The schema allows 256 characters in sAMAccountName values. However, the system limits sAMAccountName to |
||
2687 | * 20 characters for user objects and 16 characters for computer objects. The following characters are not |
||
2688 | * allowed in sAMAccountName values: " [ ] : ; | = + * ? < > / \ , |
||
2689 | * You cannot logon to a domain using a sAMAccountName that includes the "@" character. If a user has a |
||
2690 | * sAMAccountName with this character, they must logon using their userPrincipalName (UPN). |
||
2691 | * |
||
2692 | * @param string $message |
||
2693 | * @param string $fieldName |
||
2694 | * @return Assert |
||
2695 | * @throws AssertionFailedException |
||
2696 | */ |
||
2697 | View Code Duplication | public function samAccountName(string $message='', string $fieldName='') : Assert |
|
2716 | |||
2717 | /** |
||
2718 | * Assert that value is a valid userPrincipalName. |
||
2719 | * |
||
2720 | * @param string $message |
||
2721 | * @param string $fieldName |
||
2722 | * @return Assert |
||
2723 | * @throws AssertionFailedException |
||
2724 | */ |
||
2725 | View Code Duplication | public function userPrincipalName(string $message='', string $fieldName='') : Assert |
|
2749 | |||
2750 | /** |
||
2751 | * Assert that the given string is a valid UUID |
||
2752 | * |
||
2753 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
2754 | * |
||
2755 | * @param string $message |
||
2756 | * @param string $fieldName |
||
2757 | * @return Assert |
||
2758 | * @throws AssertionFailedException |
||
2759 | */ |
||
2760 | public function isni(string $message='', string $fieldName='') |
||
2779 | |||
2780 | /** |
||
2781 | * Assert that the count of countable is equal to count. |
||
2782 | * |
||
2783 | * @param int $count |
||
2784 | * @param string $message |
||
2785 | * @param string $fieldName |
||
2786 | * @return Assert |
||
2787 | * @throws AssertionFailedException |
||
2788 | */ |
||
2789 | public function count(int $count, string $message='', string $fieldName='') : Assert |
||
2809 | |||
2810 | /** |
||
2811 | * @param $func |
||
2812 | * @param $args |
||
2813 | * @return bool |
||
2814 | * @throws AssertionFailedException |
||
2815 | */ |
||
2816 | protected function doAllOrNullOr($func, $args) : bool |
||
2838 | |||
2839 | /** |
||
2840 | * Assert if values array has every choice as key and that this choice has content. |
||
2841 | * |
||
2842 | * @param array $choices |
||
2843 | * @param string $message |
||
2844 | * @param string $fieldName |
||
2845 | * @return Assert |
||
2846 | * @throws AssertionFailedException |
||
2847 | */ |
||
2848 | public function choicesNotEmpty(array $choices, string $message='', string $fieldName='') : Assert |
||
2862 | |||
2863 | /** |
||
2864 | * Assert that the named method is defined in the provided object. |
||
2865 | * |
||
2866 | * @param mixed $object |
||
2867 | * @param string $message |
||
2868 | * @param string $fieldName |
||
2869 | * @return Assert |
||
2870 | * @throws AssertionFailedException |
||
2871 | */ |
||
2872 | public function methodExists($object, string $message='', string $fieldName='') : Assert |
||
2892 | |||
2893 | /** |
||
2894 | * Assert that value is an object. |
||
2895 | * |
||
2896 | * @param string $message |
||
2897 | * @param string $fieldName |
||
2898 | * @return Assert |
||
2899 | * @throws AssertionFailedException |
||
2900 | */ |
||
2901 | public function isObject(string $message='', string $fieldName='') : Assert |
||
2920 | |||
2921 | /** |
||
2922 | * Make a string version of a value. |
||
2923 | * |
||
2924 | * @param $value |
||
2925 | * @return string |
||
2926 | */ |
||
2927 | private function stringify($value) : string |
||
2962 | } |
||
2963 | |||
2964 |
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.