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:
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 = true; |
||
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) |
||
91 | |||
92 | public function __call($name, $arguments) |
||
96 | |||
97 | /** |
||
98 | * Declare the value as being required |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function required() |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Declare data is optional |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function optional() |
||
127 | |||
128 | /** |
||
129 | * Add adhoc validator to validation stack |
||
130 | * |
||
131 | * @param callable $validation |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function add(callable $validation) |
||
140 | |||
141 | /** |
||
142 | * Validate given value matches any of the provided strings |
||
143 | * |
||
144 | * @param array $vals |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function anyOf(array $vals) |
||
157 | |||
158 | /** |
||
159 | * On validation failure whether to return false or a validation error |
||
160 | * |
||
161 | * @param bool $val |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function toBool($val = true) |
||
170 | |||
171 | /** |
||
172 | * Set default value |
||
173 | * |
||
174 | * @param $value |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function defaultValue($value) |
||
183 | |||
184 | /** |
||
185 | * Provide conditional validation |
||
186 | * |
||
187 | * @param $conditions |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function alternatives($conditions) |
||
230 | |||
231 | /** |
||
232 | * Set error messages |
||
233 | * |
||
234 | * @param array $errorMessages |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function errorMessages(array $errorMessages) |
||
250 | |||
251 | /** |
||
252 | * Create an error with a formatted message |
||
253 | * |
||
254 | * @param string $key |
||
255 | * @param null|string $value |
||
256 | * @param null|string $valueKey |
||
257 | * @param mixed $validationValue |
||
258 | * @throws DiscomfortException |
||
259 | * @throws ValidationException |
||
260 | */ |
||
261 | protected function createError($key, $value = null, $valueKey = null, $validationValue = null) |
||
300 | } |
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.