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 default messages. |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getDefaultMessages() |
||
171 | |||
172 | /** |
||
173 | * Gets all errors. |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function getErrors() |
||
181 | |||
182 | /** |
||
183 | * Gets the first error of a parameter. |
||
184 | * |
||
185 | * @param string $param |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getFirstError($param) |
||
199 | |||
200 | /** |
||
201 | * Gets errors of a parameter. |
||
202 | * |
||
203 | * @param string $param |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getParamErrors($param) |
||
211 | |||
212 | /** |
||
213 | * Gets the error of a validation rule for a parameter. |
||
214 | * |
||
215 | * @param string $param |
||
216 | * @param string $rule |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | public function getParamRuleError($param, $rule) |
||
224 | |||
225 | /** |
||
226 | * Tells whether errors should be stored in an associative array or an indexed array. |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function getStoreErrorsWithRules() |
||
234 | |||
235 | /** |
||
236 | * Gets the value of a parameter in validated data. |
||
237 | * |
||
238 | * @param string $param |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getValue($param) |
||
246 | |||
247 | /** |
||
248 | * Gets the validated data. |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | public function getValues() |
||
256 | |||
257 | /** |
||
258 | * Sets the validator data. |
||
259 | * |
||
260 | * @param array $data |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setData(array $data) |
||
270 | |||
271 | /** |
||
272 | * Sets the default error message for a validation rule. |
||
273 | * |
||
274 | * @param string $rule |
||
275 | * @param string $message |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function setDefaultMessage($rule, $message) |
||
285 | |||
286 | /** |
||
287 | * Sets default error messages. |
||
288 | * |
||
289 | * @param array $messages |
||
290 | * |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function setDefaultMessages(array $messages) |
||
299 | |||
300 | /** |
||
301 | * Sets all errors. |
||
302 | * |
||
303 | * @param array $errors |
||
304 | * |
||
305 | * @return $this |
||
306 | */ |
||
307 | public function setErrors(array $errors) |
||
313 | |||
314 | /** |
||
315 | * Sets the errors of a parameter. |
||
316 | * |
||
317 | * @param string $param |
||
318 | * @param array $errors |
||
319 | * |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function setParamErrors($param, array $errors) |
||
328 | |||
329 | /** |
||
330 | * Sets errors storage mode. |
||
331 | * |
||
332 | * @param bool $bool |
||
333 | * |
||
334 | * @return $this |
||
335 | */ |
||
336 | public function setStoreErrorsWithRules($bool) |
||
342 | |||
343 | /** |
||
344 | * Sets the value of parameters. |
||
345 | * |
||
346 | * @param array $data |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function setValues(array $data) |
||
356 | |||
357 | /** |
||
358 | * Fetch request parameter value from body or query string (in that order). |
||
359 | * |
||
360 | * @param Request $request |
||
361 | * @param string $key The parameter key. |
||
362 | * @param string $default The default value. |
||
363 | * |
||
364 | * @return mixed The parameter value. |
||
365 | */ |
||
366 | protected function getRequestParam(Request $request, $key, $default = null) |
||
382 | |||
383 | /** |
||
384 | * Sets error messages after validation. |
||
385 | * |
||
386 | * @param NestedValidationException $e |
||
387 | * @param string $param |
||
388 | * @param AbstractComposite|array $options |
||
389 | * @param array $messages |
||
390 | */ |
||
391 | protected function setMessages(NestedValidationException $e, $param, $options, array $messages) |
||
428 | } |
||
429 |