Complex classes like JSend 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 JSend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | final class JSend implements JSendInterface |
||
15 | { |
||
16 | use JSendStatusTrait; |
||
17 | use JSendJsonTrait; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $decoded = []; |
||
23 | /** |
||
24 | * @var JSendInterface |
||
25 | */ |
||
26 | private $jsend; |
||
27 | |||
28 | /** |
||
29 | * JSend constructor. |
||
30 | * |
||
31 | * @param Status $status |
||
32 | * @param array $decoded |
||
33 | */ |
||
34 | public function __construct(Status $status, array $decoded) |
||
43 | |||
44 | /** |
||
45 | * @param string $json |
||
46 | * |
||
47 | * @return array |
||
48 | * @throws InvalidJsonException |
||
49 | */ |
||
50 | public static function safeDecode(string $json): array |
||
59 | |||
60 | /** |
||
61 | * @param string $json |
||
62 | * |
||
63 | * @return JSend |
||
64 | * @throws InvalidJsonException |
||
65 | */ |
||
66 | public static function decode(string $json): self |
||
72 | |||
73 | /** |
||
74 | * @param array $decoded |
||
75 | * |
||
76 | * @return JSend |
||
77 | */ |
||
78 | public static function from(array $decoded): self |
||
84 | |||
85 | /** |
||
86 | * @param ResponseInterface $response |
||
87 | * |
||
88 | * @return JSendInterface |
||
89 | * @throws EnsuranceException |
||
90 | * @throws InvalidJsonException |
||
91 | */ |
||
92 | public static function translate(ResponseInterface $response): JSendInterface |
||
103 | |||
104 | /** |
||
105 | * @param array|null $data |
||
106 | * |
||
107 | * @return JSend |
||
108 | */ |
||
109 | public static function success(array $data = null): self |
||
113 | |||
114 | /** |
||
115 | * @param array|null $data |
||
116 | * |
||
117 | * @return JSend |
||
118 | */ |
||
119 | public static function fail(array $data = null): self |
||
123 | |||
124 | /** |
||
125 | * @param string $message |
||
126 | * @param int|null $code |
||
127 | * |
||
128 | * @return JSend |
||
129 | */ |
||
130 | public static function error(string $message, int $code = null): self |
||
134 | |||
135 | /** |
||
136 | * @return array |
||
137 | */ |
||
138 | public function asArray(): array |
||
142 | |||
143 | /** |
||
144 | * @param callable $closure |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function onSuccess(callable $closure): bool |
||
158 | |||
159 | /** |
||
160 | * @param callable $closure |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function onFail(callable $closure): bool |
||
174 | |||
175 | /** |
||
176 | * @param callable $closure |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function onError(callable $closure): bool |
||
190 | |||
191 | /** |
||
192 | * @return JSendInterface |
||
193 | * @throws EnsuranceException |
||
194 | */ |
||
195 | public function into(): JSendInterface |
||
211 | |||
212 | /** |
||
213 | * @return JSendSuccess |
||
214 | */ |
||
215 | public function intoSuccess(): JSendSuccess |
||
223 | |||
224 | /** |
||
225 | * @return JSendFail |
||
226 | */ |
||
227 | public function intoFail(): JSendFail |
||
235 | |||
236 | /** |
||
237 | * @return JSendError |
||
238 | */ |
||
239 | public function intoError(): JSendError |
||
247 | |||
248 | /** |
||
249 | * @return int |
||
250 | * @throws EnsuranceException |
||
251 | */ |
||
252 | public function getDefaultHttpStatusCode(): int |
||
256 | |||
257 | /** |
||
258 | * @param string|null $key |
||
259 | * |
||
260 | * @return bool |
||
261 | * @throws EnsuranceException |
||
262 | */ |
||
263 | public function hasData(string $key = null): bool |
||
267 | |||
268 | /** |
||
269 | * @param string|null $key |
||
270 | * @param null $default |
||
271 | * |
||
272 | * @return mixed |
||
273 | * @throws EnsuranceException |
||
274 | */ |
||
275 | public function getData(string $key = null, $default = null) |
||
279 | |||
280 | /** |
||
281 | * @param int|null $code |
||
282 | * |
||
283 | * @throws EnsuranceException |
||
284 | */ |
||
285 | public function respond(int $code = null): void |
||
289 | |||
290 | /** |
||
291 | * @return int |
||
292 | * @throws EnsuranceException |
||
293 | */ |
||
294 | public function getStatusCode(): int |
||
298 | |||
299 | /** |
||
300 | * @return string |
||
301 | * @throws EnsuranceException |
||
302 | */ |
||
303 | public function getProtocolVersion() |
||
307 | |||
308 | /** |
||
309 | * @param string $version |
||
310 | * |
||
311 | * @return JSendInterface |
||
312 | * @throws EnsuranceException |
||
313 | */ |
||
314 | public function withProtocolVersion($version) |
||
318 | |||
319 | /** |
||
320 | * @return \string[][] |
||
321 | * @throws EnsuranceException |
||
322 | */ |
||
323 | public function getHeaders() |
||
327 | |||
328 | /** |
||
329 | * @param string $name |
||
330 | * |
||
331 | * @return bool |
||
332 | * @throws EnsuranceException |
||
333 | */ |
||
334 | public function hasHeader($name) |
||
338 | |||
339 | /** |
||
340 | * @param string $name |
||
341 | * |
||
342 | * @return string[] |
||
343 | * @throws EnsuranceException |
||
344 | */ |
||
345 | public function getHeader($name) |
||
349 | |||
350 | /** |
||
351 | * @param string $name |
||
352 | * |
||
353 | * @return string |
||
354 | * @throws EnsuranceException |
||
355 | */ |
||
356 | public function getHeaderLine($name) |
||
360 | |||
361 | /** |
||
362 | * @param string $name |
||
363 | * @param string|string[] $value |
||
364 | * |
||
365 | * @return JSendInterface |
||
366 | * @throws EnsuranceException |
||
367 | */ |
||
368 | public function withHeader($name, $value) |
||
372 | |||
373 | /** |
||
374 | * @param string $name |
||
375 | * @param string|string[] $value |
||
376 | * |
||
377 | * @return JSendInterface |
||
378 | * @throws EnsuranceException |
||
379 | */ |
||
380 | public function withAddedHeader($name, $value) |
||
384 | |||
385 | /** |
||
386 | * @param string $name |
||
387 | * |
||
388 | * @return JSendInterface |
||
389 | * @throws EnsuranceException |
||
390 | */ |
||
391 | public function withoutHeader($name) |
||
395 | |||
396 | /** |
||
397 | * @return StreamInterface |
||
398 | * @throws EnsuranceException |
||
399 | */ |
||
400 | public function getBody() |
||
404 | |||
405 | /** |
||
406 | * @param StreamInterface $body |
||
407 | * |
||
408 | * @return JSendInterface |
||
409 | * @throws EnsuranceException |
||
410 | */ |
||
411 | public function withBody(StreamInterface $body) |
||
415 | |||
416 | /** |
||
417 | * @param int $code |
||
418 | * @param string $reasonPhrase |
||
419 | * |
||
420 | * @return JSendInterface |
||
421 | * @throws EnsuranceException |
||
422 | */ |
||
423 | public function withStatus($code, $reasonPhrase = '') |
||
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | * @throws EnsuranceException |
||
431 | */ |
||
432 | public function getReasonPhrase() |
||
436 | } |
||
437 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.