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 = []) |
||
59 | |||
60 | /** |
||
61 | * Tells if there is no error. |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function isValid() |
||
69 | |||
70 | /** |
||
71 | * Validates request parameters with the given rules. |
||
72 | * |
||
73 | * @param Request $request |
||
74 | * @param array $rules |
||
75 | * @param array $messages |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function validate(Request $request, array $rules, array $messages = []) |
||
110 | |||
111 | /** |
||
112 | * Adds an error for a parameter. |
||
113 | * |
||
114 | * @param string $param |
||
115 | * @param string $message |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function addError($param, $message) |
||
125 | |||
126 | /** |
||
127 | * Adds errors for a parameter. |
||
128 | * |
||
129 | * @param string $param |
||
130 | * @param string[] $messages |
||
131 | * |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function addErrors($param, array $messages) |
||
142 | |||
143 | /** |
||
144 | * Gets the validated data. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getData() |
||
152 | |||
153 | /** |
||
154 | * Gets all errors. |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getErrors() |
||
162 | |||
163 | /** |
||
164 | * Gets the first error of a parameter. |
||
165 | * |
||
166 | * @param string $param |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getFirstError($param) |
||
180 | |||
181 | /** |
||
182 | * Gets errors of a parameter. |
||
183 | * |
||
184 | * @param string $param |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | public function getParamErrors($param) |
||
192 | |||
193 | /** |
||
194 | * Gets the error of a validation rule for a parameter. |
||
195 | * |
||
196 | * @param string $param |
||
197 | * @param string $rule |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getParamRuleError($param, $rule) |
||
205 | |||
206 | /** |
||
207 | * Gets the value of a parameter in validated data. |
||
208 | * |
||
209 | * @param string $param |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getValue($param) |
||
217 | |||
218 | /** |
||
219 | * Sets the validator data. |
||
220 | * |
||
221 | * @param array $data |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function setData(array $data) |
||
231 | |||
232 | /** |
||
233 | * Sets all errors. |
||
234 | * |
||
235 | * @param array $errors |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function setErrors(array $errors) |
||
244 | |||
245 | /** |
||
246 | * Sets the errors of a parameter. |
||
247 | * |
||
248 | * @param string $param |
||
249 | * @param array $errors |
||
250 | * |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function setParamErrors($param, array $errors) |
||
259 | |||
260 | /** |
||
261 | * Sets the value of parameters. |
||
262 | * |
||
263 | * @param array $data |
||
264 | * |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function setValues(array $data) |
||
273 | |||
274 | /** |
||
275 | * Fetch request parameter value from body or query string (in that order). |
||
276 | * |
||
277 | * @param Request $request |
||
278 | * @param string $key The parameter key. |
||
279 | * @param string $default The default value. |
||
280 | * |
||
281 | * @return mixed The parameter value. |
||
282 | */ |
||
283 | protected function getRequestParam(Request $request, $key, $default = null) |
||
299 | |||
300 | /** |
||
301 | * Sets error messages after validation |
||
302 | * |
||
303 | * @param NestedValidationException $e |
||
304 | * @param string $param |
||
305 | * @param AbstractComposite|array $options |
||
306 | * @param bool $isRule |
||
307 | */ |
||
308 | protected function setMessages(NestedValidationException $e, $param, $options, $isRule) |
||
341 | } |
||
342 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.