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 |
||
| 17 | class Validator |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The validated data. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $data; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The default error messages for the given rules. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $defaultMessages; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The list of validation errors. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $errors; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Tells if errors should be stored in an associative array |
||
| 42 | * where the key is the name of the validation rule. |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $storeErrorsWithRules; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor. |
||
| 50 | * |
||
| 51 | * @param bool $storeErrorsWithRules |
||
| 52 | * @param array $defaultMessages |
||
| 53 | */ |
||
| 54 | public function __construct($storeErrorsWithRules = true, array $defaultMessages = []) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Tells if there is no error. |
||
| 64 | * |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public function isValid() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Validates request parameters with the given rules. |
||
| 74 | * |
||
| 75 | * @param Request $request |
||
| 76 | * @param array $rules |
||
| 77 | * @param array $messages |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | public function validate(Request $request, array $rules, array $messages = []) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Adds an error for a parameter. |
||
| 118 | * |
||
| 119 | * @param string $param |
||
| 120 | * @param string $message |
||
| 121 | * |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | public function addError($param, $message) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Adds errors for a parameter. |
||
| 133 | * |
||
| 134 | * @deprecated since version 2.1, will be removed in 3.0. |
||
| 135 | * |
||
| 136 | * @param string $param |
||
| 137 | * @param string[] $messages |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function addErrors($param, array $messages) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Gets the validated data. |
||
| 152 | * |
||
| 153 | * @deprecated since version 2.1, will be removed in 3.0. Use getValues() instead. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getData() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets all errors. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public function getErrors() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets the first error of a parameter. |
||
| 174 | * |
||
| 175 | * @param string $param |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getFirstError($param) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets errors of a parameter. |
||
| 192 | * |
||
| 193 | * @param string $param |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function getParamErrors($param) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Gets the error of a validation rule for a parameter. |
||
| 204 | * |
||
| 205 | * @param string $param |
||
| 206 | * @param string $rule |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getParamRuleError($param, $rule) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Tells whether errors should be stored in an associative array or an indexed array. |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function getStoreErrorsWithRules() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Gets the value of a parameter in validated data. |
||
| 227 | * |
||
| 228 | * @param string $param |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function getValue($param) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Gets the validated data. |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function getValues() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Sets the validator data. |
||
| 249 | * |
||
| 250 | * @param array $data |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function setData(array $data) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Sets the default error message for a validation rule. |
||
| 263 | * |
||
| 264 | * @param string $rule |
||
| 265 | * @param string $message |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function setDefaultMessage($rule, $message) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sets default error messages. |
||
| 278 | * |
||
| 279 | * @param array $messages |
||
| 280 | * |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function setDefaultMessages(array $messages) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Sets all errors. |
||
| 292 | * |
||
| 293 | * @param array $errors |
||
| 294 | * |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function setErrors(array $errors) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Sets the errors of a parameter. |
||
| 306 | * |
||
| 307 | * @param string $param |
||
| 308 | * @param array $errors |
||
| 309 | * |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | public function setParamErrors($param, array $errors) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Sets errors storage mode. |
||
| 321 | * |
||
| 322 | * @param bool $bool |
||
| 323 | * |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function setStoreErrorsWithRules($bool) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Sets the value of parameters. |
||
| 335 | * |
||
| 336 | * @param array $data |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function setValues(array $data) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Fetch request parameter value from body or query string (in that order). |
||
| 349 | * |
||
| 350 | * @param Request $request |
||
| 351 | * @param string $key The parameter key. |
||
| 352 | * @param string $default The default value. |
||
| 353 | * |
||
| 354 | * @return mixed The parameter value. |
||
| 355 | */ |
||
| 356 | protected function getRequestParam(Request $request, $key, $default = null) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Sets error messages after validation. |
||
| 375 | * |
||
| 376 | * @param NestedValidationException $e |
||
| 377 | * @param string $param |
||
| 378 | * @param AbstractComposite|array $options |
||
| 379 | * @param array $messages |
||
| 380 | */ |
||
| 381 | protected function setMessages(NestedValidationException $e, $param, $options, array $messages) |
||
| 418 | } |
||
| 419 |