Complex classes like StringValidation 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 StringValidation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class StringValidation |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @param $value |
||
| 21 | * |
||
| 22 | * @return bool |
||
| 23 | */ |
||
| 24 | public static function isString($value) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $value |
||
| 31 | * |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | public static function isAlphanumeric($value) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $value |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | public static function isAlpha($value) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $value |
||
| 51 | * @param integer $min |
||
| 52 | * @param integer $max |
||
| 53 | * @param bool $inclusive |
||
| 54 | * |
||
| 55 | * @throws \InvalidArgumentException |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public static function isBetween($value, $min, $max, $inclusive = false) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $value |
||
| 79 | * @param string $charset |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public static function isCharset($value, $charset) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $value |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public static function isAllConsonants($value) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $value |
||
| 113 | * @param $contains |
||
| 114 | * @param bool $identical |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public static function contains($value, $contains, $identical = false) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $value |
||
| 129 | * |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public static function isControlCharacters($value) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param $value |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public static function isDigit($value) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $value |
||
| 149 | * @param $contains |
||
| 150 | * @param bool $identical |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public static function endsWith($value, $contains, $identical = false) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Validates if the input is equal some value. |
||
| 167 | * |
||
| 168 | * @param string $value |
||
| 169 | * @param $comparedValue |
||
| 170 | * @param bool $identical |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public static function equals($value, $comparedValue, $identical = false) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $value |
||
| 185 | * @param string $haystack |
||
| 186 | * @param bool $identical |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public static function in($value, $haystack, $identical = false) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $value |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public static function hasGraphicalCharsOnly($value) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $value |
||
| 214 | * @param integer $length |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public static function hasLength($value, $length) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $value |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public static function isLowercase($value) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $value |
||
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public static function notEmpty($value) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $value |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public static function noWhitespace($value) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $value |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public static function hasPrintableCharsOnly($value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $value |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | public static function isPunctuation($value) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $value |
||
| 279 | * @param string $regex |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public static function matchesRegex($value, $regex) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $value |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public static function isSlug($value) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $value |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public static function isSpace($value) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $value |
||
| 317 | * @param $contains |
||
| 318 | * @param bool $identical |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public static function startsWith($value, $contains, $identical = false) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $value |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public static function isUppercase($value) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $value |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public static function isVersion($value) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $value |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public static function isVowel($value) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $value |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public static function isHexDigit($value) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $value |
||
| 375 | * @param int $amount |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public static function hasLowercase($value, $amount = null) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $value |
||
| 386 | * @param integer|null $amount |
||
| 387 | * @param string $regex |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | private static function hasStringSubset($value, $amount, $regex) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $value |
||
| 419 | * @param int $amount |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | public static function hasUppercase($value, $amount = null) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $value |
||
| 430 | * @param int $amount |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | public static function hasNumeric($value, $amount = null) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $value |
||
| 441 | * @param int $amount |
||
| 442 | * |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | public static function hasSpecialCharacters($value, $amount = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $value |
||
| 452 | * |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | public static function isEmail($value) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $value |
||
| 464 | * |
||
| 465 | * @return boolean |
||
| 466 | */ |
||
| 467 | public static function isUrl($value) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $value |
||
| 478 | * @param bool $strict |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | public static function isUUID($value, $strict = true) |
||
| 494 | } |
||
| 495 |