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:
| 1 | <?php |
||
| 6 | class RestrictionUtils |
||
| 7 | { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Defines a list of acceptable values. |
||
| 11 | * |
||
| 12 | * @param mixed $value |
||
| 13 | * @param array $enumeration |
||
| 14 | * @throws RestrictionException |
||
| 15 | * @return mixed |
||
| 16 | */ |
||
| 17 | 2 | public static function checkEnumeration($value, $enumeration) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Defines the exact sequence of characters that are acceptable. |
||
| 32 | * |
||
| 33 | * @param mixed $value |
||
| 34 | * @param string $pattern |
||
| 35 | * @throws RestrictionException |
||
| 36 | * @return mixed |
||
| 37 | */ |
||
| 38 | 2 | public static function checkPattern($value, $pattern) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Check is numeric valid |
||
| 52 | * |
||
| 53 | * @param mixed $value |
||
| 54 | * @return mixed |
||
| 55 | * @throws RestrictionException |
||
| 56 | */ |
||
| 57 | 31 | private static function getNumeric($value) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero. |
||
| 71 | * |
||
| 72 | * @param float $value |
||
| 73 | * @param int $fractionDigits |
||
| 74 | * @throws RestrictionException |
||
| 75 | * @return float |
||
| 76 | */ |
||
| 77 | 3 | public static function checkFractionDigits($value, $fractionDigits) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Specifies the exact number of digits allowed. Must be greater than zero. |
||
| 102 | * |
||
| 103 | * @param float $value |
||
| 104 | * @param int $totalDigits |
||
| 105 | * @throws RestrictionException |
||
| 106 | * @return float |
||
| 107 | */ |
||
| 108 | 4 | public static function checkTotalDigits($value, $totalDigits) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Specifies the upper bounds for numeric values (the value must be less than this value) |
||
| 136 | * |
||
| 137 | * @param mixed $value |
||
| 138 | * @param int $maxExclusive |
||
| 139 | * @throws RestrictionException |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | 3 | View Code Duplication | public static function checkMaxExclusive($value, $maxExclusive) |
| 154 | |||
| 155 | /** |
||
| 156 | * Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
||
| 157 | * |
||
| 158 | * @param mixed $value |
||
| 159 | * @param int $maxInclusive |
||
| 160 | * @throws RestrictionException |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | 3 | View Code Duplication | public static function checkMaxInclusive($value, $maxInclusive) |
| 175 | |||
| 176 | /** |
||
| 177 | * Retrive the number of characters or list items allowed. |
||
| 178 | * |
||
| 179 | * @param mixed $value |
||
| 180 | * @param string $nativeType |
||
| 181 | * @throws RestrictionException |
||
| 182 | * @return int |
||
| 183 | */ |
||
| 184 | 21 | private static function getLength($value, $nativeType=null) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero. |
||
| 206 | * |
||
| 207 | * @param mixed $value |
||
| 208 | * @param int $length |
||
| 209 | * @param string $nativeType |
||
| 210 | * @throws RestrictionException |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | 7 | View Code Duplication | public static function checkLength($value, $length, $nativeType=null) |
| 224 | |||
| 225 | /** |
||
| 226 | * Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
||
| 227 | * |
||
| 228 | * @param mixed $value |
||
| 229 | * @param int $maxLength |
||
| 230 | * @param string $nativeType |
||
| 231 | * @throws RestrictionException |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | 7 | View Code Duplication | public static function checkMaxLength($value, $maxLength, $nativeType=null) |
| 245 | |||
| 246 | /** |
||
| 247 | * Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
||
| 248 | * |
||
| 249 | * @param mixed $value |
||
| 250 | * @param int $minLength |
||
| 251 | * @param string $nativeType |
||
| 252 | * @throws RestrictionException |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | 7 | View Code Duplication | public static function checkMinLength($value, $minLength, $nativeType=null) |
| 266 | |||
| 267 | /** |
||
| 268 | * Specifies the lower bounds for numeric values (the value must be greater than this value) |
||
| 269 | * |
||
| 270 | * @param mixed $value |
||
| 271 | * @param int $minExclusive |
||
| 272 | * @throws RestrictionException |
||
| 273 | * @return mixed |
||
| 274 | */ |
||
| 275 | 3 | View Code Duplication | public static function checkMinExclusive($value, $minExclusive) |
| 287 | |||
| 288 | /** |
||
| 289 | * Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
||
| 290 | * |
||
| 291 | * @param mixed $value |
||
| 292 | * @param int $minInclusive |
||
| 293 | * @throws RestrictionException |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | 3 | View Code Duplication | public static function checkMinInclusive($value, $minInclusive) |
| 308 | |||
| 309 | /** |
||
| 310 | * Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |
||
| 311 | * |
||
| 312 | * @param mixed $value |
||
| 313 | * @param string $whiteSpace |
||
| 314 | * @return mixed |
||
| 315 | */ |
||
| 316 | 1 | public static function checkWhiteSpace($value, $whiteSpace) |
|
| 327 | |||
| 328 | } |
||
| 329 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: