Complex classes like Validation 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 Validation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Validation { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $errors = []; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $mainMessage = ''; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $mainStatus = 0; |
||
28 | |||
29 | /** |
||
30 | * @var bool Whether or not fields should be translated. |
||
31 | */ |
||
32 | private $translateFieldNames = false; |
||
33 | |||
34 | /** |
||
35 | * Add an error. |
||
36 | * |
||
37 | * @param string $field The name and path of the field to add or an empty string if this is a global error. |
||
38 | * @param string $error The message code. |
||
39 | * @param int|array $options An array of additional information to add to the error entry or a numeric error code. |
||
40 | * @return $this |
||
41 | */ |
||
42 | 64 | public function addError($field, $error, $options = []) { |
|
43 | 64 | if (empty($error)) { |
|
44 | throw new \InvalidArgumentException('The error code cannot be empty.', 500); |
||
45 | } |
||
46 | |||
47 | 64 | $fieldKey = $field; |
|
48 | 64 | $row = ['field' => null, 'code' => null, 'path' => null, 'index' => null]; |
|
|
|||
49 | |||
50 | // Split the field out into a path, field, and possible index. |
||
51 | 64 | if (($pos = strrpos($field, '.')) !== false) { |
|
52 | 6 | $row['path'] = substr($field, 0, $pos); |
|
53 | 6 | $field = substr($field, $pos + 1); |
|
54 | } |
||
55 | 64 | if (preg_match('`^([^[]+)\[(\d+)\]$`', $field, $m)) { |
|
56 | 3 | $row['index'] = (int)$m[2]; |
|
57 | 3 | $field = $m[1]; |
|
58 | } |
||
59 | 64 | $row['field'] = $field; |
|
60 | 64 | $row['code'] = $error; |
|
61 | |||
62 | 64 | $row = array_filter($row, function ($v) { |
|
63 | 64 | return $v !== null; |
|
64 | 64 | }); |
|
65 | |||
66 | 64 | if (is_array($options)) { |
|
67 | 61 | $row += $options; |
|
68 | 4 | } elseif (is_int($options)) { |
|
69 | 4 | $row['status'] = $options; |
|
70 | } |
||
71 | |||
72 | 64 | $this->errors[$fieldKey][] = $row; |
|
73 | |||
74 | 64 | return $this; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get or set the error status code. |
||
79 | * |
||
80 | * The status code is an http response code and should be of the 4xx variety. |
||
81 | * |
||
82 | * @return int Returns the current status code. |
||
83 | */ |
||
84 | 57 | public function getStatus() { |
|
103 | |||
104 | /** |
||
105 | * Get the message for this exception. |
||
106 | * |
||
107 | * @return string Returns the exception message. |
||
108 | */ |
||
109 | 58 | public function getMessage() { |
|
127 | |||
128 | /** |
||
129 | * Gets all of the errors as a flat array. |
||
130 | * |
||
131 | * The errors are internally stored indexed by field. This method flattens them for final error returns. |
||
132 | * |
||
133 | * @return array Returns all of the errors. |
||
134 | */ |
||
135 | 4 | public function getErrors() { |
|
136 | 4 | $result = []; |
|
137 | 4 | foreach ($this->getRawErrors() as $error) { |
|
138 | 4 | $result[] = $this->formatError($error); |
|
139 | } |
||
140 | 4 | return $result; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the errors for a specific field. |
||
145 | * |
||
146 | * @param string $field The full path to the field. |
||
147 | * @return array Returns an array of errors. |
||
148 | */ |
||
149 | 6 | public function getFieldErrors($field) { |
|
150 | 6 | if (empty($this->errors[$field])) { |
|
151 | return []; |
||
152 | } else { |
||
153 | 6 | $result = []; |
|
154 | 6 | foreach ($this->errors[$field] as $error) { |
|
155 | 6 | $result[] = $this->formatError($error); |
|
156 | } |
||
157 | 6 | return $result; |
|
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Gets all of the errors as a flat array. |
||
163 | * |
||
164 | * The errors are internally stored indexed by field. This method flattens them for final error returns. |
||
165 | * |
||
166 | * @return \Traversable Returns all of the errors. |
||
167 | */ |
||
168 | 75 | protected function getRawErrors() { |
|
169 | 75 | foreach ($this->errors as $errors) { |
|
170 | 61 | foreach ($errors as $error) { |
|
171 | 61 | yield $error; |
|
172 | } |
||
173 | } |
||
174 | 75 | } |
|
175 | |||
176 | /** |
||
177 | * Check whether or not the validation is free of errors. |
||
178 | * |
||
179 | * @return bool Returns true if there are no errors, false otherwise. |
||
180 | */ |
||
181 | 145 | public function isValid() { |
|
184 | |||
185 | /** |
||
186 | * Check whether or not a particular field is has errors. |
||
187 | * |
||
188 | * @param string $field The name of the field to check for validity. |
||
189 | * @return bool Returns true if the field has no errors, false otherwise. |
||
190 | */ |
||
191 | 90 | public function isValidField($field) { |
|
195 | |||
196 | /** |
||
197 | * Get the error count, optionally for a particular field. |
||
198 | * |
||
199 | * @param string $field The name of a field or an empty string for all errors. |
||
200 | * @return int Returns the error count. |
||
201 | */ |
||
202 | 61 | public function getErrorCount($field = '') { |
|
211 | |||
212 | /** |
||
213 | * Get the error message for an error row. |
||
214 | * |
||
215 | * @param array $error The error row. |
||
216 | * @return string Returns a formatted/translated error message. |
||
217 | */ |
||
218 | 59 | private function getErrorMessage(array $error) { |
|
248 | |||
249 | /** |
||
250 | * Expand and translate a message format against an array of values. |
||
251 | * |
||
252 | * @param string $format The message format. |
||
253 | * @param array $context The context arguments to apply to the message. |
||
254 | * @return string Returns a formatted string. |
||
255 | */ |
||
256 | 59 | private function formatMessage($format, $context = []) { |
|
266 | |||
267 | /** |
||
268 | * Translate an argument being placed in an error message. |
||
269 | * |
||
270 | * @param mixed $value The argument to translate. |
||
271 | * @param array $args Formatting arguments. |
||
272 | * @return string Returns the translated string. |
||
273 | */ |
||
274 | 56 | private function formatField($value, array $args = []) { |
|
306 | |||
307 | /** |
||
308 | * Translate a string. |
||
309 | * |
||
310 | * This method doesn't do any translation itself, but is meant for subclasses wanting to add translation ability to |
||
311 | * this class. |
||
312 | * |
||
313 | * @param string $str The string to translate. |
||
314 | * @return string Returns the translated string. |
||
315 | */ |
||
316 | 60 | public function translate($str) { |
|
324 | |||
325 | /** |
||
326 | * Merge another validation object with this one. |
||
327 | * |
||
328 | * @param Validation $validation The validation object to merge. |
||
329 | * @param string $name The path to merge to. Use this parameter when the validation object is meant to be a subset of this one. |
||
330 | * @return $this |
||
331 | */ |
||
332 | 1 | public function merge(Validation $validation, $name = '') { |
|
349 | |||
350 | /** |
||
351 | * Get the main error message. |
||
352 | * |
||
353 | * If set, this message will be returned as the error message. Otherwise the message will be set from individual |
||
354 | * errors. |
||
355 | * |
||
356 | * @return string Returns the main message. |
||
357 | */ |
||
358 | 58 | public function getMainMessage() { |
|
361 | |||
362 | /** |
||
363 | * Set the main error message. |
||
364 | * |
||
365 | * @param string $message The new message. |
||
366 | * @return $this |
||
367 | */ |
||
368 | 1 | public function setMainMessage($message) { |
|
372 | |||
373 | /** |
||
374 | * Get the main status. |
||
375 | * |
||
376 | * @return int Returns an HTTP response code or zero to indicate it should be calculated. |
||
377 | */ |
||
378 | 57 | public function getMainStatus() { |
|
381 | |||
382 | /** |
||
383 | * Set the main status. |
||
384 | * |
||
385 | * @param int $status An HTTP response code or zero. |
||
386 | * @return $this |
||
387 | */ |
||
388 | 1 | public function setMainStatus($status) { |
|
392 | |||
393 | /** |
||
394 | * Whether or not fields should be translated. |
||
395 | * |
||
396 | * @return bool Returns **true** if field names are translated or **false** otherwise. |
||
397 | */ |
||
398 | 59 | public function getTranslateFieldNames() { |
|
401 | |||
402 | /** |
||
403 | * Set whether or not fields should be translated. |
||
404 | * |
||
405 | * @param bool $translate Whether or not fields should be translated. |
||
406 | * @return $this |
||
407 | */ |
||
408 | 2 | public function setTranslateFieldNames($translate) { |
|
412 | |||
413 | /** |
||
414 | * @param $error |
||
415 | * @return array |
||
416 | */ |
||
417 | 10 | private function formatError($error) { |
|
426 | } |
||
427 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.