Complex classes like Response 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 Response, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Response |
||
13 | { |
||
14 | /** |
||
15 | * Emit response wrapper as raw string. |
||
16 | */ |
||
17 | public static $EMIT_RAW = 0; |
||
|
|||
18 | |||
19 | /** |
||
20 | * Emit response wrapper as json string. |
||
21 | */ |
||
22 | public static $EMIT_JSON = 1; |
||
23 | |||
24 | /** |
||
25 | * Emit response wrapper as html string. |
||
26 | */ |
||
27 | public static $EMIT_HTML = 2; |
||
28 | |||
29 | /** |
||
30 | * Emit response wrapper as string/jsonstring. |
||
31 | */ |
||
32 | public static $EMIT_JSONTEXT = 3; |
||
33 | |||
34 | /** |
||
35 | * Emit response wrapper as padded-json. |
||
36 | */ |
||
37 | public static $EMIT_JSONP = 4; |
||
38 | |||
39 | /** |
||
40 | * Error data. |
||
41 | */ |
||
42 | private $error; |
||
43 | |||
44 | /** |
||
45 | * Result data. |
||
46 | */ |
||
47 | private $result; |
||
48 | |||
49 | // Active emit type |
||
50 | private $emitType = 1; // EMIT_JSON |
||
51 | |||
52 | // JSONP padding |
||
53 | private $emitJSONPFn = false; // for EMIT_JSONP |
||
54 | |||
55 | // List of response headers |
||
56 | private $headers = []; |
||
57 | |||
58 | /** |
||
59 | * Set headers to send. |
||
60 | * |
||
61 | * @param mixed $header |
||
62 | */ |
||
63 | public function setHeader($header) |
||
67 | |||
68 | /** |
||
69 | * Set padding method name for JSONP emit type. |
||
70 | * |
||
71 | * @param mixed $fn |
||
72 | */ |
||
73 | public function setEmitJSONP($fn) |
||
78 | |||
79 | /** |
||
80 | * Set emit type. |
||
81 | * |
||
82 | * @param mixed $type |
||
83 | */ |
||
84 | public function setEmitType($type) |
||
88 | |||
89 | /** |
||
90 | * Is emit type configured to JSON? |
||
91 | */ |
||
92 | public function isJSON() |
||
96 | |||
97 | /** |
||
98 | * Get the error data. |
||
99 | */ |
||
100 | public function getError() |
||
104 | |||
105 | /** |
||
106 | * Set error data to send. |
||
107 | * |
||
108 | * @param mixed $code |
||
109 | * @param mixed|null $message |
||
110 | * @param mixed $trace |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | public function setError($code = 500, $message = null, $trace = false): void |
||
125 | |||
126 | /** |
||
127 | * Set exception error to send. |
||
128 | * |
||
129 | * @param Throwable $e |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | public function setException(\Throwable $e): void |
||
151 | |||
152 | /** |
||
153 | * Check the presence of error data. |
||
154 | */ |
||
155 | public function hasError() |
||
159 | |||
160 | /** |
||
161 | * Update the result data. |
||
162 | * |
||
163 | * @param mixed $key |
||
164 | * @param mixed $value |
||
165 | */ |
||
166 | public function updateResult($key, $value) |
||
170 | |||
171 | /** |
||
172 | * Get the result data. |
||
173 | */ |
||
174 | public function getResult() |
||
178 | |||
179 | /** |
||
180 | * Set the result data. |
||
181 | * |
||
182 | * @param mixed $result |
||
183 | */ |
||
184 | public function setResult($result) |
||
188 | |||
189 | /** |
||
190 | * Send response to client. |
||
191 | */ |
||
192 | public function emit() |
||
232 | |||
233 | /** |
||
234 | * Emit response wrapper as JSONString. |
||
235 | */ |
||
236 | protected function emitJSON() |
||
240 | |||
241 | /** |
||
242 | * Prepare the response wrapper. |
||
243 | */ |
||
244 | protected function prepareResponse() |
||
256 | |||
257 | /** |
||
258 | * Emit response wrapper as String/JSONString. |
||
259 | */ |
||
260 | protected function emitText() |
||
276 | |||
277 | /** |
||
278 | * Emit response wrapper as String. |
||
279 | */ |
||
280 | protected function emitRaw() |
||
287 | } |
||
288 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.