Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
18 | class Validator |
||
19 | { |
||
20 | const MODE_ASSOCIATIVE = 1; |
||
21 | const MODE_INDEXED = 2; |
||
22 | |||
23 | /** |
||
24 | * The validated data. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $values; |
||
29 | |||
30 | /** |
||
31 | * The default error messages for the given rules. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $defaultMessages; |
||
36 | |||
37 | /** |
||
38 | * The list of validation errors. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $errors; |
||
43 | |||
44 | /** |
||
45 | * Tells if errors should be stored in an associative array |
||
46 | * or in an indexed array. |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $errorStorageMode; |
||
51 | |||
52 | /** |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param int $errorStorageMode |
||
56 | * @param array $defaultMessages |
||
57 | */ |
||
58 | public function __construct(int $errorStorageMode = self::MODE_ASSOCIATIVE, array $defaultMessages = []) |
||
65 | |||
66 | /** |
||
67 | * Tells if there is no error. |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function isValid() |
||
75 | |||
76 | /** |
||
77 | * Validates an array with the given rules. |
||
78 | * |
||
79 | * @param array $array |
||
80 | * @param array $rules |
||
81 | * @param array $messages |
||
82 | * @param string|int $group |
||
83 | * |
||
84 | * @return self |
||
85 | */ |
||
86 | View Code Duplication | public function array(array $array, array $rules, array $messages = [], $group = null) |
|
100 | |||
101 | /** |
||
102 | * Validates an objects properties with the given rules. |
||
103 | * |
||
104 | * @param object $object |
||
105 | * @param array $rules |
||
106 | * @param array $messages |
||
107 | * @param string|int $group |
||
108 | * |
||
109 | * @return self |
||
110 | */ |
||
111 | View Code Duplication | public function object(object $object, array $rules, array $messages = [], $group = null) |
|
125 | |||
126 | /** |
||
127 | * Validates request parameters with the given rules. |
||
128 | * |
||
129 | * @param Request $request |
||
130 | * @param array $rules |
||
131 | * @param array $messages |
||
132 | * @param string|int $group |
||
133 | * |
||
134 | * @return self |
||
135 | */ |
||
136 | View Code Duplication | public function request(Request $request, array $rules, array $messages = [], $group = null) |
|
150 | |||
151 | /** |
||
152 | * Validates request parameters, an array or an objects properties. |
||
153 | * |
||
154 | * @param Request|object|array $input |
||
155 | * @param array $rules |
||
156 | * @param array $messages |
||
157 | * @param string|int $group |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | public function validate($input, array $rules, array $messages = [], $group = null) |
||
177 | |||
178 | /** |
||
179 | * Validates a single value with the given rules. |
||
180 | * |
||
181 | * @param mixed $value |
||
182 | * @param string $key |
||
183 | * @param RespectValidator|array $rules |
||
184 | * @param array $messages |
||
185 | * @param string|int $group |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | public function value($value, $key, $rules, array $messages = [], $group = null) |
||
231 | |||
232 | /** |
||
233 | * Gets the error count. |
||
234 | * |
||
235 | * @return int |
||
236 | */ |
||
237 | public function count() |
||
241 | |||
242 | /** |
||
243 | * Adds an error for a parameter. |
||
244 | * |
||
245 | * @param string $param |
||
246 | * @param string $message |
||
247 | * @param string $group |
||
248 | * |
||
249 | * @return self |
||
250 | */ |
||
251 | View Code Duplication | public function addError($param, $message, $group = null) |
|
261 | |||
262 | /** |
||
263 | * Gets all default messages. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function getDefaultMessages() |
||
271 | |||
272 | /** |
||
273 | * Gets all errors. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public function getErrors() |
||
281 | |||
282 | /** |
||
283 | * Gets the first error of a parameter. |
||
284 | * |
||
285 | * @param string $param |
||
286 | * @param string $group |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getFirstError($param, $group = null) |
||
306 | |||
307 | /** |
||
308 | * Gets errors of a parameter. |
||
309 | * |
||
310 | * @param string $param |
||
311 | * @param string $group |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public function getParamErrors($param, $group = null) |
||
323 | |||
324 | /** |
||
325 | * Gets the error of a validation rule for a parameter. |
||
326 | * |
||
327 | * @param string $param |
||
328 | * @param string $rule |
||
329 | * @param string $group |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | View Code Duplication | public function getParamRuleError($param, $rule, $group = null) |
|
341 | |||
342 | /** |
||
343 | * Gets the value of a request parameter in validated data. |
||
344 | * |
||
345 | * @param string $param |
||
346 | * @param string $group |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | View Code Duplication | public function getValue($param, $group = null) |
|
358 | |||
359 | /** |
||
360 | * Gets the validated data. |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function getValues() |
||
368 | |||
369 | /** |
||
370 | * Gets the error storage mode. |
||
371 | * |
||
372 | * @return int |
||
373 | */ |
||
374 | public function getErrorStorageMode() |
||
378 | |||
379 | /** |
||
380 | * Sets the default error message for a validation rule. |
||
381 | * |
||
382 | * @param string $rule |
||
383 | * @param string $message |
||
384 | * |
||
385 | * @return self |
||
386 | */ |
||
387 | public function setDefaultMessage($rule, $message) |
||
393 | |||
394 | /** |
||
395 | * Sets default error messages. |
||
396 | * |
||
397 | * @param array $messages |
||
398 | * |
||
399 | * @return self |
||
400 | */ |
||
401 | public function setDefaultMessages(array $messages) |
||
407 | |||
408 | /** |
||
409 | * Sets all errors. |
||
410 | * |
||
411 | * @param array $errors |
||
412 | * |
||
413 | * @return self |
||
414 | */ |
||
415 | public function setErrors(array $errors) |
||
421 | |||
422 | /** |
||
423 | * Sets the error storage mode. |
||
424 | * |
||
425 | * @param int $errorStorageMode |
||
426 | * |
||
427 | * @return self |
||
428 | */ |
||
429 | public function setErrorStorageMode(int $errorStorageMode) |
||
435 | |||
436 | /** |
||
437 | * Sets the errors of a parameter. |
||
438 | * |
||
439 | * @param string $param |
||
440 | * @param array $errors |
||
441 | * @param string $group |
||
442 | * |
||
443 | * @return self |
||
444 | */ |
||
445 | View Code Duplication | public function setParamErrors($param, array $errors, $group = null) |
|
455 | |||
456 | /** |
||
457 | * Sets the value of a request parameter. |
||
458 | * |
||
459 | * @param string $param |
||
460 | * @param mixed $value |
||
461 | * @param string $group |
||
462 | * |
||
463 | * @return self |
||
464 | */ |
||
465 | public function setValue($param, $value, $group = null) |
||
475 | |||
476 | /** |
||
477 | * Sets the values of request parameters. |
||
478 | * |
||
479 | * @param array $values |
||
480 | * |
||
481 | * @return self |
||
482 | */ |
||
483 | public function setValues(array $values) |
||
489 | |||
490 | /** |
||
491 | * Gets the value of a property of an object. |
||
492 | * |
||
493 | * @param object $object |
||
494 | * @param string $propertyName |
||
495 | * @param mixed $default |
||
496 | * |
||
497 | * @return mixed |
||
498 | */ |
||
499 | protected function getPropertyValue(object $object, string $propertyName, $default = null) |
||
510 | |||
511 | /** |
||
512 | * Fetch request parameter value from body or query string (in that order). |
||
513 | * |
||
514 | * @param Request $request |
||
515 | * @param string $key The parameter key. |
||
516 | * @param string $default The default value. |
||
517 | * |
||
518 | * @return mixed The parameter value. |
||
519 | */ |
||
520 | protected function getRequestParam(Request $request, $key, $default = null) |
||
536 | |||
537 | /** |
||
538 | * Sets error messages after validation. |
||
539 | * |
||
540 | * @param NestedValidationException $e |
||
541 | * @param string $param |
||
542 | * @param AbstractComposite|array $options |
||
543 | * @param array $messages |
||
544 | * @param string|int $group |
||
545 | */ |
||
546 | protected function setMessages(NestedValidationException $e, $param, $options, array $messages = [], $group = null) |
||
587 | } |
||
588 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.