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 AbstractValidator 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 AbstractValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class AbstractValidator |
||
15 | { |
||
16 | /** |
||
17 | * @var \Closure[] |
||
18 | */ |
||
19 | protected $validationStack = []; |
||
20 | /** |
||
21 | * @var Comfort |
||
22 | */ |
||
23 | private $comfort; |
||
24 | /** |
||
25 | * @var boolean |
||
26 | */ |
||
27 | private $toBool = false; |
||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $optional = true; |
||
32 | /** |
||
33 | * @var mixed |
||
34 | */ |
||
35 | private $defaultValue; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $errorHandlers = [ |
||
40 | 'default' => [ |
||
41 | 'message' => 'There was a validation error' |
||
42 | ], |
||
43 | 'required' => [ |
||
44 | 'message' => '%s is required', |
||
45 | 'default' => 'value' |
||
46 | ] |
||
47 | ]; |
||
48 | |||
49 | public function __construct(Comfort $comfort) |
||
54 | |||
55 | /** |
||
56 | * Execute validation stack |
||
57 | * |
||
58 | * @param mixed $value |
||
59 | * @param null|string $key |
||
60 | * @return bool|ValidationError |
||
61 | */ |
||
62 | public function __invoke($value, $key = null) |
||
95 | |||
96 | public function __call($name, $arguments) |
||
100 | |||
101 | /** |
||
102 | * Declare the value as being required |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function required() |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Declare data is optional |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function optional() |
||
132 | |||
133 | /** |
||
134 | * Add adhoc validator to validation stack |
||
135 | * |
||
136 | * @param callable $validation |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function add(callable $validation) |
||
145 | |||
146 | /** |
||
147 | <<<<<<< HEAD |
||
148 | ======= |
||
149 | * Validate given value matches any of the provided strings |
||
150 | * |
||
151 | * @param array $vals |
||
152 | * @return $this |
||
153 | */ |
||
154 | View Code Duplication | public function anyOf(array $vals) |
|
164 | |||
165 | /** |
||
166 | >>>>>>> feature/phpcs |
||
167 | * On validation failure whether to return false or a validation error |
||
168 | * |
||
169 | * @param bool $val |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function toBool($val = true) |
||
178 | |||
179 | /** |
||
180 | * Set default value |
||
181 | * |
||
182 | * @param $value |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function defaultValue($value) |
||
191 | |||
192 | /** |
||
193 | * Provide conditional validation |
||
194 | * |
||
195 | * @param $conditions |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function alternatives($conditions) |
||
243 | |||
244 | /** |
||
245 | * Set error messages |
||
246 | * |
||
247 | * @param array $errorMessages |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function errorMessages(array $errorMessages) |
||
263 | |||
264 | /** |
||
265 | * Create an error with a formatted message |
||
266 | * |
||
267 | * @param string $key |
||
268 | * @param null|string $value |
||
269 | * @param null|string $valueKey |
||
270 | * @param mixed $validationValue |
||
271 | * @throws DiscomfortException |
||
272 | * @throws ValidationException |
||
273 | */ |
||
274 | protected function createError($key, $value = null, $valueKey = null, $validationValue = null) |
||
313 | } |
||
314 |
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.