Complex classes like Event 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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Event |
||
9 | { |
||
10 | /** |
||
11 | * @var string status |
||
12 | */ |
||
13 | private $event; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $id; |
||
19 | |||
20 | /** |
||
21 | * @var float |
||
22 | */ |
||
23 | private $timestamp; |
||
24 | |||
25 | /** |
||
26 | * A \DateTime representation of $timestamp. |
||
27 | * |
||
28 | * @var \DateTime |
||
29 | */ |
||
30 | private $eventDate; |
||
31 | |||
32 | /** |
||
33 | * @var array|string[] |
||
34 | */ |
||
35 | private $tags = []; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $url; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $severity; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $envelope = []; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $deliveryStatus; |
||
56 | |||
57 | /** |
||
58 | * @var array|string[] |
||
59 | */ |
||
60 | private $campaigns = []; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $ip; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $clientInfo = []; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $reason; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | private $userVariables = []; |
||
81 | |||
82 | /** |
||
83 | * @var array key=>bool |
||
84 | */ |
||
85 | private $flags = []; |
||
86 | |||
87 | /** |
||
88 | * @var array multi dimensions |
||
89 | */ |
||
90 | private $routes = []; |
||
91 | |||
92 | /** |
||
93 | * @var array multi dimensions |
||
94 | */ |
||
95 | private $message = []; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | private $recipient; |
||
101 | |||
102 | /** |
||
103 | * @var array |
||
104 | */ |
||
105 | private $geolocation = []; |
||
106 | |||
107 | /** |
||
108 | * @var array |
||
109 | */ |
||
110 | private $storage = []; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | private $method; |
||
116 | |||
117 | /** |
||
118 | * @param string $event |
||
119 | * @param string $id |
||
120 | * @param float $timestamp |
||
121 | */ |
||
122 | public function __construct($event, $id, $timestamp) |
||
130 | |||
131 | /** |
||
132 | * @param array $data |
||
133 | * |
||
134 | * @return Event |
||
135 | */ |
||
136 | public static function create(array $data) |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getEvent() |
||
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getId() |
||
210 | |||
211 | /** |
||
212 | * @return float |
||
213 | */ |
||
214 | public function getTimestamp() |
||
218 | |||
219 | /** |
||
220 | * @return \DateTime |
||
221 | */ |
||
222 | public function getEventDate() |
||
226 | |||
227 | /** |
||
228 | * @return array|\string[] |
||
229 | */ |
||
230 | public function getTags() |
||
234 | |||
235 | /** |
||
236 | * @param array|\string[] $tags |
||
237 | */ |
||
238 | private function setTags($tags) |
||
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getUrl() |
||
250 | |||
251 | /** |
||
252 | * @param string $url |
||
253 | */ |
||
254 | private function setUrl($url) |
||
258 | |||
259 | /** |
||
260 | * @return array |
||
261 | */ |
||
262 | public function getEnvelope() |
||
266 | |||
267 | /** |
||
268 | * @param array $envelope |
||
269 | */ |
||
270 | private function setEnvelope($envelope) |
||
274 | |||
275 | /** |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getDeliveryStatus() |
||
282 | |||
283 | /** |
||
284 | * @param array $deliveryStatus |
||
285 | */ |
||
286 | private function setDeliveryStatus($deliveryStatus) |
||
290 | |||
291 | /** |
||
292 | * @return array|\string[] |
||
293 | */ |
||
294 | public function getCampaigns() |
||
298 | |||
299 | /** |
||
300 | * @param array|\string[] $campaigns |
||
301 | */ |
||
302 | private function setCampaigns($campaigns) |
||
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getIp() |
||
314 | |||
315 | /** |
||
316 | * @param string $ip |
||
317 | */ |
||
318 | private function setIp($ip) |
||
322 | |||
323 | /** |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getClientInfo() |
||
330 | |||
331 | /** |
||
332 | * @param array $clientInfo |
||
333 | */ |
||
334 | private function setClientInfo($clientInfo) |
||
338 | |||
339 | /** |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getReason() |
||
346 | |||
347 | /** |
||
348 | * @param string $reason |
||
349 | */ |
||
350 | private function setReason($reason) |
||
354 | |||
355 | /** |
||
356 | * @return array |
||
357 | */ |
||
358 | public function getUserVariables() |
||
362 | |||
363 | /** |
||
364 | * @param array $userVariables |
||
365 | */ |
||
366 | private function setUserVariables($userVariables) |
||
370 | |||
371 | /** |
||
372 | * @return array |
||
373 | */ |
||
374 | public function getFlags() |
||
378 | |||
379 | /** |
||
380 | * @param array $flags |
||
381 | */ |
||
382 | private function setFlags($flags) |
||
386 | |||
387 | /** |
||
388 | * @return array |
||
389 | */ |
||
390 | public function getRoutes() |
||
394 | |||
395 | /** |
||
396 | * @param array $routes |
||
397 | */ |
||
398 | private function setRoutes($routes) |
||
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getMessage() |
||
410 | |||
411 | /** |
||
412 | * @param array $message |
||
413 | */ |
||
414 | private function setMessage($message) |
||
418 | |||
419 | /** |
||
420 | * @return string |
||
421 | */ |
||
422 | public function getRecipient() |
||
426 | |||
427 | /** |
||
428 | * @param string $recipient |
||
429 | */ |
||
430 | private function setRecipient($recipient) |
||
434 | |||
435 | /** |
||
436 | * @return array |
||
437 | */ |
||
438 | public function getGeolocation() |
||
442 | |||
443 | /** |
||
444 | * @param array $geolocation |
||
445 | */ |
||
446 | private function setGeolocation($geolocation) |
||
450 | |||
451 | /** |
||
452 | * @return array |
||
453 | */ |
||
454 | public function getStorage() |
||
458 | |||
459 | /** |
||
460 | * @param array $storage |
||
461 | */ |
||
462 | private function setStorage($storage) |
||
466 | |||
467 | /** |
||
468 | * @return string |
||
469 | */ |
||
470 | public function getMethod() |
||
474 | |||
475 | /** |
||
476 | * @param string $method |
||
477 | */ |
||
478 | private function setMethod($method) |
||
482 | |||
483 | /** |
||
484 | * @return string |
||
485 | */ |
||
486 | public function getSeverity() |
||
490 | |||
491 | /** |
||
492 | * @param string $severity |
||
493 | */ |
||
494 | private function setSeverity($severity) |
||
498 | } |
||
499 |