Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Validator |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $config = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array|string |
||
| 33 | */ |
||
| 34 | private $rules; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string|null |
||
| 38 | */ |
||
| 39 | private $failingRule; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Changes settings for the validator. |
||
| 43 | * |
||
| 44 | * @param array $config |
||
| 45 | */ |
||
| 46 | public static function configure($config) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param array|string $rules can be key-value array matching data or a string |
||
| 53 | */ |
||
| 54 | public function __construct($rules) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Gets the rules. |
||
| 61 | * |
||
| 62 | * @return array|string |
||
| 63 | */ |
||
| 64 | public function getRules() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Validates the given data against the rules. |
||
| 71 | * |
||
| 72 | * @param array|mixed $data can be key-value array matching rules or a single value |
||
| 73 | */ |
||
| 74 | public function validate(&$data): bool |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Gets the failing rule. |
||
| 92 | */ |
||
| 93 | public function getFailingRule(): ?string |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Validates a value according to a rule or set of rules. |
||
| 100 | * This will short-circuit on the first failing rule. |
||
| 101 | * |
||
| 102 | * @param mixed $value |
||
| 103 | * @param string $rule rule string |
||
| 104 | */ |
||
| 105 | private function validateRule(&$value, $rule): bool |
||
| 123 | |||
| 124 | //////////////////////////////// |
||
| 125 | // Validators |
||
| 126 | //////////////////////////////// |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Validates an alpha string. |
||
| 130 | * OPTIONAL alpha:5 can specify minimum length. |
||
| 131 | * |
||
| 132 | * @param mixed $value |
||
| 133 | */ |
||
| 134 | private function alpha(&$value, array $parameters): bool |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Validates an alpha-numeric string |
||
| 143 | * OPTIONAL alpha_numeric:6 can specify minimum length. |
||
| 144 | * |
||
| 145 | * @param mixed $value |
||
| 146 | */ |
||
| 147 | private function alpha_numeric(&$value, array $parameters): bool |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Validates an alpha-numeric string with dashes and underscores |
||
| 156 | * OPTIONAL alpha_dash:7 can specify minimum length. |
||
| 157 | * |
||
| 158 | * @param mixed $value |
||
| 159 | */ |
||
| 160 | private function alpha_dash(&$value, array $parameters): bool |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Validates a boolean value. |
||
| 169 | * |
||
| 170 | * @param mixed $value |
||
| 171 | */ |
||
| 172 | private function boolean(&$value): bool |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Validates an e-mail address. |
||
| 181 | * |
||
| 182 | * @param string $email e-mail address |
||
|
|
|||
| 183 | * @param array $parameters parameters for validation |
||
| 184 | * |
||
| 185 | * @return bool success |
||
| 186 | */ |
||
| 187 | private function email(&$value, array $parameters): bool |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Validates a value exists in an array. i.e. enum:blue,red,green,yellow. |
||
| 196 | * |
||
| 197 | * @param mixed $value |
||
| 198 | */ |
||
| 199 | private function enum(&$value, array $parameters): bool |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Validates a date string. |
||
| 208 | * |
||
| 209 | * @param mixed $value |
||
| 210 | */ |
||
| 211 | private function date(&$value): bool |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Validates an IP address. |
||
| 218 | * |
||
| 219 | * @param mixed $value |
||
| 220 | */ |
||
| 221 | private function ip(&$value): bool |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Validates that an array of values matches. The array will |
||
| 228 | * be flattened to a single value if it matches. |
||
| 229 | * |
||
| 230 | * @param mixed $value |
||
| 231 | */ |
||
| 232 | private function matching(&$value): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Validates a number. |
||
| 254 | * OPTIONAL numeric:int specifies a type. |
||
| 255 | * |
||
| 256 | * @param mixed $value |
||
| 257 | */ |
||
| 258 | private function numeric(&$value, array $parameters): bool |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Validates a password and hashes the value using |
||
| 271 | * password_hash(). |
||
| 272 | * OPTIONAL password:10 sets the minimum length. |
||
| 273 | * |
||
| 274 | * @param mixed $value |
||
| 275 | */ |
||
| 276 | private function password_php(&$value, array $parameters): bool |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Validates that a number falls within a range. |
||
| 296 | * |
||
| 297 | * @param mixed $value |
||
| 298 | */ |
||
| 299 | private function range(&$value, array $parameters): bool |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Makes sure that a variable is not empty. |
||
| 316 | * |
||
| 317 | * @param mixed $value |
||
| 318 | */ |
||
| 319 | private function required(&$value): bool |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Validates a string. |
||
| 326 | * OPTIONAL string:5 supplies a minimum length |
||
| 327 | * string:1:5 supplies a minimum and maximum length. |
||
| 328 | * |
||
| 329 | * @param mixed $value |
||
| 330 | */ |
||
| 331 | private function string(&$value, array $parameters): bool |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Validates a PHP time zone identifier. |
||
| 346 | * |
||
| 347 | * @param mixed $value |
||
| 348 | */ |
||
| 349 | private function time_zone(&$value): bool |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Validates a Unix timestamp. If the value is not a timestamp it will be |
||
| 362 | * converted to one with `strtotime()`. |
||
| 363 | * |
||
| 364 | * @param mixed $value |
||
| 365 | */ |
||
| 366 | private function timestamp(&$value): bool |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Converts a Unix timestamp into a format compatible with database |
||
| 379 | * timestamp types. |
||
| 380 | * |
||
| 381 | * @param mixed $value |
||
| 382 | */ |
||
| 383 | private function db_timestamp(&$value): bool |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Validates a URL. |
||
| 397 | * |
||
| 398 | * @param mixed $value |
||
| 399 | */ |
||
| 400 | private function url(&$value): bool |
||
| 404 | |||
| 405 | ///////////////////////// |
||
| 406 | // Uniqueness |
||
| 407 | ///////////////////////// |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Checks if a value is unique for a property. |
||
| 411 | * |
||
| 412 | * @param mixed $value |
||
| 413 | */ |
||
| 414 | public static function isUnique(Model $model, Property $property, $value): bool |
||
| 418 | } |
||
| 419 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.