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_LOCALCIRCLECHECK = 1; |
||
55 | const BYPASS_LOCALMEMBERCHECK = 2; |
||
56 | const BYPASS_INITIATORCHECK = 4; |
||
57 | |||
58 | |||
59 | use TArrayTools; |
||
60 | |||
61 | |||
62 | /** @var string */ |
||
63 | private $class; |
||
64 | |||
65 | /** @var string */ |
||
66 | private $source = ''; |
||
67 | |||
68 | /** @var Circle */ |
||
69 | private $circle; |
||
70 | |||
71 | /** @var Member */ |
||
72 | private $member; |
||
73 | |||
74 | /** @var SimpleDataStore */ |
||
75 | private $data; |
||
76 | |||
77 | /** @var int */ |
||
78 | private $severity = self::SEVERITY_LOW; |
||
79 | |||
80 | /** @var SimpleDataStore */ |
||
81 | private $readingOutcome; |
||
82 | |||
83 | /** @var SimpleDataStore */ |
||
84 | private $dataOutcome; |
||
85 | |||
86 | /** @var SimpleDataStore */ |
||
87 | private $result; |
||
88 | |||
89 | /** @var bool */ |
||
90 | private $async = false; |
||
91 | |||
92 | /** @var bool */ |
||
93 | private $dataRequestOnly = false; |
||
94 | |||
95 | /** @var string */ |
||
96 | private $incomingOrigin = ''; |
||
97 | |||
98 | |||
99 | /** @var string */ |
||
100 | private $wrapperToken = ''; |
||
101 | |||
102 | /** @var bool */ |
||
103 | private $verifiedViewer = false; |
||
104 | |||
105 | /** @var bool */ |
||
106 | private $verifiedCircle = false; |
||
107 | |||
108 | /** @var int */ |
||
109 | private $bypass = 0; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * FederatedEvent constructor. |
||
114 | * |
||
115 | * @param string $class |
||
116 | */ |
||
117 | function __construct(string $class = '') { |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getClass(): string { |
||
132 | |||
133 | /** |
||
134 | * @param mixed $class |
||
135 | * |
||
136 | * @return self |
||
137 | */ |
||
138 | public function setClass($class): self { |
||
143 | |||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getSource(): string { |
||
151 | |||
152 | /** |
||
153 | * @param string $source |
||
154 | * |
||
155 | * @return self |
||
156 | */ |
||
157 | public function setSource(string $source): self { |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @return bool |
||
181 | */ |
||
182 | public function isAsync(): bool { |
||
185 | |||
186 | /** |
||
187 | * @param bool $async |
||
188 | * |
||
189 | * @return self |
||
190 | */ |
||
191 | public function setAsync(bool $async): self { |
||
196 | |||
197 | |||
198 | /** |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function isDataRequestOnly(): bool { |
||
204 | |||
205 | /** |
||
206 | * @param bool $dataRequestOnly |
||
207 | * |
||
208 | * @return self |
||
209 | */ |
||
210 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @param string $incomingOrigin |
||
219 | * |
||
220 | * @return self |
||
221 | */ |
||
222 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getIncomingOrigin(): string { |
||
234 | |||
235 | |||
236 | /** |
||
237 | * @param bool $verifiedViewer |
||
238 | * |
||
239 | * @return FederatedEvent |
||
240 | */ |
||
241 | public function setVerifiedViewer(bool $verifiedViewer): self { |
||
246 | |||
247 | // /** |
||
248 | // * @return bool |
||
249 | // */ |
||
250 | // public function isVerifiedViewer(): bool { |
||
251 | // return $this->verifiedViewer; |
||
252 | // } |
||
253 | |||
254 | // /** |
||
255 | // * @throws InitiatorNotConfirmedException |
||
256 | // */ |
||
257 | // public function confirmVerifiedViewer(): void { |
||
258 | // if ($this->isVerifiedViewer()) { |
||
259 | // return; |
||
260 | // } |
||
261 | // |
||
262 | // throw new InitiatorNotConfirmedException(); |
||
263 | // } |
||
264 | |||
265 | |||
266 | // /** |
||
267 | // * @param bool $verifiedCircle |
||
268 | // * |
||
269 | // * @return FederatedEvent |
||
270 | // */ |
||
271 | // public function setVerifiedCircle(bool $verifiedCircle): self { |
||
272 | // $this->verifiedCircle = $verifiedCircle; |
||
273 | // |
||
274 | // return $this; |
||
275 | // } |
||
276 | // |
||
277 | // /** |
||
278 | // * @return bool |
||
279 | // */ |
||
280 | // public function isVerifiedCircle(): bool { |
||
281 | // return $this->verifiedCircle; |
||
282 | // } |
||
283 | |||
284 | |||
285 | /** |
||
286 | * @param string $wrapperToken |
||
287 | * |
||
288 | * @return FederatedEvent |
||
289 | */ |
||
290 | public function setWrapperToken(string $wrapperToken): self { |
||
295 | |||
296 | /** |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getWrapperToken(): string { |
||
302 | |||
303 | |||
304 | /** |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function hasCircle(): bool { |
||
310 | |||
311 | /** |
||
312 | * @param Circle $circle |
||
313 | * |
||
314 | * @return self |
||
315 | */ |
||
316 | public function setCircle(Circle $circle): self { |
||
321 | |||
322 | /** |
||
323 | * @return Circle |
||
324 | */ |
||
325 | public function getCircle(): Circle { |
||
328 | |||
329 | |||
330 | /** |
||
331 | * @return Member |
||
332 | */ |
||
333 | public function getMember(): Member { |
||
336 | |||
337 | /** |
||
338 | * @param Member|null $member |
||
339 | * |
||
340 | * @return self |
||
341 | */ |
||
342 | public function setMember(?Member $member): self { |
||
347 | |||
348 | /** |
||
349 | * @return bool |
||
350 | */ |
||
351 | public function hasMember(): bool { |
||
354 | |||
355 | |||
356 | /** |
||
357 | * @param SimpleDataStore $data |
||
358 | * |
||
359 | * @return self |
||
360 | */ |
||
361 | public function setData(SimpleDataStore $data): self { |
||
366 | |||
367 | /** |
||
368 | * @return SimpleDataStore |
||
369 | */ |
||
370 | public function getData(): SimpleDataStore { |
||
373 | |||
374 | |||
375 | /** |
||
376 | * @return int |
||
377 | */ |
||
378 | public function getSeverity(): int { |
||
381 | |||
382 | /** |
||
383 | * @param int $severity |
||
384 | * |
||
385 | * @return self |
||
386 | */ |
||
387 | public function setSeverity(int $severity): self { |
||
392 | |||
393 | |||
394 | /** |
||
395 | * @return SimpleDataStore |
||
396 | */ |
||
397 | public function getOutcome(): SimpleDataStore { |
||
405 | |||
406 | /** |
||
407 | * @return SimpleDataStore |
||
408 | */ |
||
409 | public function getReadingOutcome(): SimpleDataStore { |
||
412 | |||
413 | /** |
||
414 | * @param string $message |
||
415 | * @param array $params |
||
416 | * @param bool $fail |
||
417 | * |
||
418 | * @return $this |
||
419 | */ |
||
420 | public function setReadingOutcome(string $message, array $params = [], bool $fail = false): self { |
||
431 | |||
432 | |||
433 | /** |
||
434 | * @return SimpleDataStore |
||
435 | */ |
||
436 | public function getDataOutcome(): SimpleDataStore { |
||
439 | |||
440 | /** |
||
441 | * @param array $data |
||
442 | * |
||
443 | * @return $this |
||
444 | */ |
||
445 | public function setDataOutcome(array $data): self { |
||
450 | |||
451 | |||
452 | /** |
||
453 | * @return SimpleDataStore |
||
454 | */ |
||
455 | public function getResult(): SimpleDataStore { |
||
458 | |||
459 | /** |
||
460 | * @param SimpleDataStore $result |
||
461 | * |
||
462 | * @return self |
||
463 | */ |
||
464 | public function setResult(SimpleDataStore $result): self { |
||
469 | |||
470 | |||
471 | /** |
||
472 | * @param array $data |
||
473 | * |
||
474 | * @return self |
||
475 | * @throws InvalidItemException |
||
476 | */ |
||
477 | public function import(array $data): self { |
||
498 | |||
499 | |||
500 | /** |
||
501 | * @return array |
||
502 | */ |
||
503 | function jsonSerialize(): array { |
||
525 | |||
526 | |||
527 | /** |
||
528 | * @param int $flag |
||
529 | * |
||
530 | * @return FederatedEvent |
||
531 | */ |
||
532 | public function bypass(int $flag): self { |
||
539 | |||
540 | /** |
||
541 | * @param int $flag |
||
542 | * |
||
543 | * @return bool |
||
544 | */ |
||
545 | public function canBypass(int $flag): bool { |
||
548 | |||
549 | } |
||
550 | |||
551 |
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.