Complex classes like FederatedEvent 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 FederatedEvent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class FederatedEvent implements JsonSerializable { |
||
49 | |||
50 | |||
51 | const SEVERITY_LOW = 1; |
||
52 | const SEVERITY_HIGH = 3; |
||
53 | |||
54 | const BYPASS_CIRCLE = 1; |
||
55 | const BYPASS_LOCALCIRCLECHECK = 2; |
||
56 | const BYPASS_LOCALMEMBERCHECK = 4; |
||
57 | const BYPASS_INITIATORCHECK = 8; |
||
58 | const BYPASS_INITIATORMEMBERSHIP = 16; |
||
59 | |||
60 | use TArrayTools; |
||
61 | |||
62 | |||
63 | /** @var string */ |
||
64 | private $class; |
||
65 | |||
66 | /** @var string */ |
||
67 | private $origin = ''; |
||
68 | |||
69 | /** @var Circle */ |
||
70 | private $circle; |
||
71 | |||
72 | /** @var string */ |
||
73 | private $itemId = ''; |
||
74 | |||
75 | /** @var string */ |
||
76 | private $itemSource = ''; |
||
77 | |||
78 | /** @var Member */ |
||
79 | private $member; |
||
80 | |||
81 | /** @var Member[] */ |
||
82 | private $members = []; |
||
83 | |||
84 | /** @var SimpleDataStore */ |
||
85 | private $params; |
||
86 | |||
87 | /** @var SimpleDataStore */ |
||
88 | private $data; |
||
89 | |||
90 | /** @var int */ |
||
91 | private $severity = self::SEVERITY_LOW; |
||
92 | |||
93 | /** @var array */ |
||
94 | private $outcome = []; |
||
95 | |||
96 | /** @var SimpleDataStore */ |
||
97 | private $result; |
||
98 | |||
99 | /** @var bool */ |
||
100 | private $async = false; |
||
101 | |||
102 | /** @var bool */ |
||
103 | private $limitedToInstanceWithMember = false; |
||
104 | |||
105 | /** @var bool */ |
||
106 | private $dataRequestOnly = false; |
||
107 | |||
108 | /** @var string */ |
||
109 | private $sender = ''; |
||
110 | |||
111 | |||
112 | /** @var string */ |
||
113 | private $wrapperToken = ''; |
||
114 | |||
115 | /** @var int */ |
||
116 | private $bypass = 0; |
||
117 | |||
118 | |||
119 | /** |
||
120 | * FederatedEvent constructor. |
||
121 | * |
||
122 | * @param string $class |
||
123 | */ |
||
124 | function __construct(string $class = '') { |
||
|
|||
125 | $this->class = $class; |
||
126 | $this->params = new SimpleDataStore(); |
||
127 | $this->data = new SimpleDataStore(); |
||
128 | $this->result = new SimpleDataStore(); |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getClass(): string { |
||
138 | |||
139 | /** |
||
140 | * @param mixed $class |
||
141 | * |
||
142 | * @return self |
||
143 | */ |
||
144 | public function setClass($class): self { |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Origin of the event. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getOrigin(): string { |
||
159 | |||
160 | /** |
||
161 | * @param string $origin |
||
162 | * |
||
163 | * @return self |
||
164 | */ |
||
165 | public function setOrigin(string $origin): self { |
||
170 | |||
171 | |||
172 | /** |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function isAsync(): bool { |
||
178 | |||
179 | /** |
||
180 | * @param bool $async |
||
181 | * |
||
182 | * @return self |
||
183 | */ |
||
184 | public function setAsync(bool $async): self { |
||
189 | |||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isLimitedToInstanceWithMember(): bool { |
||
197 | |||
198 | /** |
||
199 | * @param bool $limitedToInstanceWithMember |
||
200 | * |
||
201 | * @return self |
||
202 | */ |
||
203 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function isDataRequestOnly(): bool { |
||
216 | |||
217 | /** |
||
218 | * @param bool $dataRequestOnly |
||
219 | * |
||
220 | * @return self |
||
221 | */ |
||
222 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
227 | |||
228 | |||
229 | /** |
||
230 | * |
||
231 | * Origin of the request |
||
232 | * |
||
233 | * @param string $sender |
||
234 | * |
||
235 | * @return self |
||
236 | */ |
||
237 | public function setSender(string $sender): self { |
||
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getSender(): string { |
||
249 | |||
250 | |||
251 | /** |
||
252 | * @param string $wrapperToken |
||
253 | * |
||
254 | * @return FederatedEvent |
||
255 | */ |
||
256 | public function setWrapperToken(string $wrapperToken): self { |
||
261 | |||
262 | /** |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getWrapperToken(): string { |
||
268 | |||
269 | |||
270 | /** |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function hasCircle(): bool { |
||
276 | |||
277 | /** |
||
278 | * @param Circle $circle |
||
279 | * |
||
280 | * @return self |
||
281 | */ |
||
282 | public function setCircle(Circle $circle): self { |
||
287 | |||
288 | /** |
||
289 | * @return Circle |
||
290 | */ |
||
291 | public function getCircle(): Circle { |
||
294 | |||
295 | |||
296 | /** |
||
297 | * @param string $itemId |
||
298 | * |
||
299 | * @return self |
||
300 | */ |
||
301 | public function setItemId(string $itemId): self { |
||
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getItemId(): string { |
||
313 | |||
314 | |||
315 | /** |
||
316 | * @param string $itemSource |
||
317 | * |
||
318 | * @return self |
||
319 | */ |
||
320 | public function setItemSource(string $itemSource): self { |
||
325 | |||
326 | /** |
||
327 | * @return string |
||
328 | */ |
||
329 | public function getItemSource(): string { |
||
332 | |||
333 | |||
334 | /** |
||
335 | * @return Member |
||
336 | */ |
||
337 | public function getMember(): Member { |
||
340 | |||
341 | /** |
||
342 | * @param Member|null $member |
||
343 | * |
||
344 | * @return self |
||
345 | */ |
||
346 | public function setMember(?Member $member): self { |
||
351 | |||
352 | /** |
||
353 | * @return bool |
||
354 | */ |
||
355 | public function hasMember(): bool { |
||
358 | |||
359 | |||
360 | /** |
||
361 | * @return Member[] |
||
362 | */ |
||
363 | public function getMembers(): array { |
||
366 | |||
367 | /** |
||
368 | * @param Member[] $members |
||
369 | * |
||
370 | * @return self |
||
371 | */ |
||
372 | public function setMembers(array $members): self { |
||
377 | |||
378 | |||
379 | /** |
||
380 | * @param SimpleDataStore $params |
||
381 | * |
||
382 | * @return self |
||
383 | */ |
||
384 | public function setParams(SimpleDataStore $params): self { |
||
389 | |||
390 | /** |
||
391 | * @return SimpleDataStore |
||
392 | */ |
||
393 | public function getParams(): SimpleDataStore { |
||
396 | |||
397 | |||
398 | /** |
||
399 | * @param SimpleDataStore $data |
||
400 | * |
||
401 | * @return self |
||
402 | */ |
||
403 | public function setData(SimpleDataStore $data): self { |
||
408 | |||
409 | /** |
||
410 | * @return SimpleDataStore |
||
411 | */ |
||
412 | public function getData(): SimpleDataStore { |
||
415 | |||
416 | /** |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function resetData(): self { |
||
424 | |||
425 | |||
426 | /** |
||
427 | * @return int |
||
428 | */ |
||
429 | public function getSeverity(): int { |
||
432 | |||
433 | /** |
||
434 | * @param int $severity |
||
435 | * |
||
436 | * @return self |
||
437 | */ |
||
438 | public function setSeverity(int $severity): self { |
||
443 | |||
444 | |||
445 | /** |
||
446 | * @return array |
||
447 | */ |
||
448 | public function getOutcome(): array { |
||
451 | |||
452 | /** |
||
453 | * @param array $data |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function setOutcome(array $data): self { |
||
462 | |||
463 | |||
464 | /** |
||
465 | * @return SimpleDataStore |
||
466 | */ |
||
467 | public function getResult(): SimpleDataStore { |
||
470 | |||
471 | /** |
||
472 | * @param SimpleDataStore $result |
||
473 | * |
||
474 | * @return self |
||
475 | */ |
||
476 | public function setResult(SimpleDataStore $result): self { |
||
481 | |||
482 | /** |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function resetResult(): self { |
||
490 | |||
491 | /** |
||
492 | * @param string $key |
||
493 | * @param array $result |
||
494 | * |
||
495 | * @return $this |
||
496 | */ |
||
497 | public function addResult(string $key, array $result): self { |
||
506 | |||
507 | |||
508 | /** |
||
509 | * @param int $flag |
||
510 | * |
||
511 | * @return FederatedEvent |
||
512 | */ |
||
513 | public function bypass(int $flag): self { |
||
520 | |||
521 | /** |
||
522 | * @param int $flag |
||
523 | * |
||
524 | * @return bool |
||
525 | */ |
||
526 | public function canBypass(int $flag): bool { |
||
529 | |||
530 | |||
531 | /** |
||
532 | * @param array $data |
||
533 | * |
||
534 | * @return self |
||
535 | * @throws InvalidItemException |
||
536 | */ |
||
537 | public function import(array $data): self { |
||
568 | |||
569 | |||
570 | /** |
||
571 | * @return array |
||
572 | */ |
||
573 | function jsonSerialize(): array { |
||
596 | |||
597 | } |
||
598 | |||
599 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.