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 |
||
16 | class Validator |
||
17 | { |
||
18 | /** |
||
19 | * The validated data. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $data; |
||
24 | |||
25 | /** |
||
26 | * The default error messages for the given rules. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $defaultMessages; |
||
31 | |||
32 | /** |
||
33 | * The list of validation errors. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $errors; |
||
38 | |||
39 | /** |
||
40 | * Tells if errors should be stored in an associative array |
||
41 | * where the key is the name of the validation rule. |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $storeErrorsWithRules; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param bool $storeErrorsWithRules |
||
51 | * @param array $defaultMessages |
||
52 | */ |
||
53 | public function __construct($storeErrorsWithRules = true, array $defaultMessages = []) |
||
58 | |||
59 | /** |
||
60 | * Tells if there is no error. |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function isValid() |
||
68 | |||
69 | /** |
||
70 | * Validates request parameters with the given rules. |
||
71 | * |
||
72 | * @param Request $request |
||
73 | * @param array $rules |
||
74 | * @param array $messages |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function validate(Request $request, array $rules, array $messages = []) |
||
137 | |||
138 | /** |
||
139 | * Adds an error for a parameter. |
||
140 | * |
||
141 | * @param string $param |
||
142 | * @param string $message |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function addError($param, $message) |
||
152 | |||
153 | /** |
||
154 | * Adds errors for a parameter. |
||
155 | * |
||
156 | * @param string $param |
||
157 | * @param string[] $messages |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function addErrors($param, array $messages) |
||
169 | |||
170 | /** |
||
171 | * Gets the validated data. |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getData() |
||
179 | |||
180 | /** |
||
181 | * Gets all errors. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function getErrors() |
||
189 | |||
190 | /** |
||
191 | * Gets the first error of a parameter. |
||
192 | * |
||
193 | * @param string $param |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getFirstError($param) |
||
207 | |||
208 | /** |
||
209 | * Gets errors of a parameter. |
||
210 | * |
||
211 | * @param string $param |
||
212 | * |
||
213 | * @return string[] |
||
214 | */ |
||
215 | public function getParamErrors($param) |
||
219 | |||
220 | /** |
||
221 | * Gets the error of a validation rule for a parameter. |
||
222 | * |
||
223 | * @param string $param |
||
224 | * @param string $rule |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getParamRuleError($param, $rule) |
||
232 | |||
233 | /** |
||
234 | * Fetch request parameter value from body or query string (in that order). |
||
235 | * |
||
236 | * @param Request $request |
||
237 | * @param string $key The parameter key. |
||
238 | * @param string $default The default value. |
||
239 | * |
||
240 | * @return mixed The parameter value. |
||
241 | */ |
||
242 | public function getRequestParam(Request $request, $key, $default = null) |
||
258 | |||
259 | /** |
||
260 | * Gets the value of a parameter in validated data. |
||
261 | * |
||
262 | * @param string $param |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public function getValue($param) |
||
270 | |||
271 | /** |
||
272 | * Sets the validator data. |
||
273 | * |
||
274 | * @param array $data |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function setData(array $data) |
||
284 | |||
285 | /** |
||
286 | * Sets all errors. |
||
287 | * |
||
288 | * @param array $errors |
||
289 | * |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function setErrors(array $errors) |
||
297 | |||
298 | /** |
||
299 | * Sets the errors of a parameter. |
||
300 | * |
||
301 | * @param string $param |
||
302 | * @param string[] $errors |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function setParamErrors($param, array $errors) |
||
312 | |||
313 | /** |
||
314 | * Sets the value of parameters. |
||
315 | * |
||
316 | * @param array $data |
||
317 | * |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function setValues(array $data) |
||
326 | } |
||
327 |