Complex classes like ErrorContext 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 ErrorContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class ErrorContext |
||
20 | { |
||
21 | /** |
||
22 | * Holds the unique identifier of the error (a name). |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $_name = null; |
||
27 | |||
28 | /** |
||
29 | * An array that holds all the errors occurred separated by severity. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $_errors = null; |
||
34 | |||
35 | /** |
||
36 | * The default severity code. |
||
37 | * |
||
38 | * @var mixed |
||
39 | */ |
||
40 | private $_default_severity = 0; |
||
41 | |||
42 | /** |
||
43 | * A list of all severity code from the less important to the most serious. |
||
44 | * |
||
45 | * @var array|mixed |
||
46 | */ |
||
47 | private $_severity_levels = array(0); |
||
48 | |||
49 | /** |
||
50 | * Certain errors may need some specific language file... |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $_language_files = array(); |
||
55 | |||
56 | /** |
||
57 | * Multiton. This is an array of instances of ErrorContext. |
||
58 | * All callers use an error context ('post', 'attach', or 'default' if none chosen). |
||
59 | * |
||
60 | * @var array of ErrorContext |
||
61 | */ |
||
62 | private static $_contexts = null; |
||
63 | |||
64 | const MINOR = 0; |
||
65 | const SERIOUS = 1; |
||
66 | |||
67 | /** |
||
68 | * Create and initialize an instance of the class |
||
69 | * |
||
70 | * @param string $id the error identifier |
||
71 | * @param int|null $default_severity the default error severity level |
||
72 | */ |
||
73 | 2 | private function __construct($id = 'default', $default_severity = null) |
|
89 | |||
90 | /** |
||
91 | * Add an error to the list |
||
92 | * |
||
93 | * @param mixed[]|mixed $error error code |
||
94 | * @param string|int|null $severity error severity |
||
95 | * @param string|null $lang_file lang_file |
||
96 | */ |
||
97 | 1 | public function addError($error, $severity = null, $lang_file = null) |
|
110 | |||
111 | /** |
||
112 | * Remove an error from the list |
||
113 | * |
||
114 | * @param mixed[]|mixed $error error code |
||
115 | */ |
||
116 | 1 | public function removeError($error) |
|
131 | |||
132 | /** |
||
133 | * Finds the "name" of the error (either the string, the first element |
||
134 | * of the array, or the result of getName) |
||
135 | * |
||
136 | * @param mixed|mixed[] $error error code |
||
137 | */ |
||
138 | 1 | protected function getErrorName($error) |
|
161 | |||
162 | /** |
||
163 | * Finds the "value" of the error (Usually applicable only to |
||
164 | * array of strings, being the second element of the array) |
||
165 | * |
||
166 | * @param mixed|mixed[] $error error code |
||
167 | */ |
||
168 | protected function getErrorValue($error) |
||
187 | |||
188 | /** |
||
189 | * Return an array of errors of a certain severity. |
||
190 | * |
||
191 | * @todo is it needed at all? |
||
192 | * @param string|int|null $severity the severity level wanted. If null returns all the errors |
||
193 | */ |
||
194 | 1 | public function getErrors($severity = null) |
|
203 | |||
204 | /** |
||
205 | * Return an error based on the id of the error set when adding the error itself. |
||
206 | * |
||
207 | * @param mixed|mixed[] $error error code |
||
208 | * @return null|mixed whatever the error is (string, object, array), noll if not found |
||
209 | */ |
||
210 | public function getError($error = null) |
||
218 | |||
219 | /** |
||
220 | * Returns if there are errors or not. |
||
221 | * |
||
222 | * @param string|null $severity the severity level wanted. If null returns all the errors |
||
223 | * @return bool |
||
224 | */ |
||
225 | 2 | public function hasErrors($severity = null) |
|
234 | |||
235 | /** |
||
236 | * Check if a particular error exists. |
||
237 | * |
||
238 | * @param string $errors the error |
||
239 | */ |
||
240 | 1 | public function hasError($errors) |
|
260 | |||
261 | /** |
||
262 | * Return the code of the highest error level encountered |
||
263 | */ |
||
264 | 1 | public function getErrorType() |
|
277 | |||
278 | /** |
||
279 | * Return an array containing the error strings |
||
280 | * |
||
281 | * - If severity is null the function returns all the errors |
||
282 | * |
||
283 | * @param string|null $severity the severity level wanted |
||
284 | */ |
||
285 | public function prepareErrors($severity = null) |
||
330 | |||
331 | /** |
||
332 | * Load the default error language and any other language file needed |
||
333 | */ |
||
334 | private function _loadLang() |
||
350 | |||
351 | /** |
||
352 | * Find and return ErrorContext instance if it exists, |
||
353 | * or create a new instance for $id if it didn't already exist. |
||
354 | * |
||
355 | * @param string $id |
||
356 | * @param int|null $default_severity |
||
357 | * @return ErrorContext |
||
358 | */ |
||
359 | 2 | public static function context($id = 'default', $default_severity = null) |
|
369 | } |
||
370 |