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 $data; |
||
| 86 | |||
| 87 | /** @var int */ |
||
| 88 | private $severity = self::SEVERITY_LOW; |
||
| 89 | |||
| 90 | /** @var array */ |
||
| 91 | private $outcome = []; |
||
| 92 | |||
| 93 | /** @var SimpleDataStore */ |
||
| 94 | private $result; |
||
| 95 | |||
| 96 | /** @var bool */ |
||
| 97 | private $async = false; |
||
| 98 | |||
| 99 | /** @var bool */ |
||
| 100 | private $limitedToInstanceWithMember = false; |
||
| 101 | |||
| 102 | /** @var bool */ |
||
| 103 | private $dataRequestOnly = false; |
||
| 104 | |||
| 105 | /** @var string */ |
||
| 106 | private $sender = ''; |
||
| 107 | |||
| 108 | |||
| 109 | /** @var string */ |
||
| 110 | private $wrapperToken = ''; |
||
| 111 | |||
| 112 | /** @var int */ |
||
| 113 | private $bypass = 0; |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * FederatedEvent constructor. |
||
| 118 | * |
||
| 119 | * @param string $class |
||
| 120 | */ |
||
| 121 | function __construct(string $class = '') { |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getClass(): string { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param mixed $class |
||
| 137 | * |
||
| 138 | * @return self |
||
| 139 | */ |
||
| 140 | public function setClass($class): self { |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getOrigin(): string { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Source (instance) of the event. |
||
| 156 | * |
||
| 157 | * @param string $origin |
||
| 158 | * |
||
| 159 | * @return self |
||
| 160 | */ |
||
| 161 | public function setOrigin(string $origin): self { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function isAsync(): bool { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param bool $async |
||
| 182 | * |
||
| 183 | * @return self |
||
| 184 | */ |
||
| 185 | public function setAsync(bool $async): self { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function isLimitedToInstanceWithMember(): bool { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param bool $limitedToInstanceWithMember |
||
| 201 | * |
||
| 202 | * @return self |
||
| 203 | */ |
||
| 204 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function isDataRequestOnly(): bool { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param bool $dataRequestOnly |
||
| 220 | * |
||
| 221 | * @return self |
||
| 222 | */ |
||
| 223 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * |
||
| 232 | * Origin of the request |
||
| 233 | * |
||
| 234 | * @param string $sender |
||
| 235 | * |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | public function setSender(string $sender): self { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getSender(): string { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $wrapperToken |
||
| 254 | * |
||
| 255 | * @return FederatedEvent |
||
| 256 | */ |
||
| 257 | public function setWrapperToken(string $wrapperToken): self { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function getWrapperToken(): string { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function hasCircle(): bool { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param Circle $circle |
||
| 280 | * |
||
| 281 | * @return self |
||
| 282 | */ |
||
| 283 | public function setCircle(Circle $circle): self { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return Circle |
||
| 291 | */ |
||
| 292 | public function getCircle(): Circle { |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $itemId |
||
| 299 | * |
||
| 300 | * @return self |
||
| 301 | */ |
||
| 302 | public function setItemId(string $itemId): self { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getItemId(): string { |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $itemSource |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | public function setItemSource(string $itemSource): self { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getItemSource(): string { |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @return Member |
||
| 337 | */ |
||
| 338 | public function getMember(): Member { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param Member|null $member |
||
| 344 | * |
||
| 345 | * @return self |
||
| 346 | */ |
||
| 347 | public function setMember(?Member $member): self { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function hasMember(): bool { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * @return Member[] |
||
| 363 | */ |
||
| 364 | public function getMembers(): array { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param Member[] $members |
||
| 370 | * |
||
| 371 | * @return self |
||
| 372 | */ |
||
| 373 | public function setMembers(array $members): self { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * @param SimpleDataStore $data |
||
| 382 | * |
||
| 383 | * @return self |
||
| 384 | */ |
||
| 385 | public function setData(SimpleDataStore $data): self { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return SimpleDataStore |
||
| 393 | */ |
||
| 394 | public function getData(): SimpleDataStore { |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * @return int |
||
| 401 | */ |
||
| 402 | public function getSeverity(): int { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param int $severity |
||
| 408 | * |
||
| 409 | * @return self |
||
| 410 | */ |
||
| 411 | public function setSeverity(int $severity): self { |
||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function getOutcome(): array { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param array $data |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function setOutcome(array $data): self { |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * @return SimpleDataStore |
||
| 439 | */ |
||
| 440 | public function getResult(): SimpleDataStore { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param SimpleDataStore $result |
||
| 446 | * |
||
| 447 | * @return self |
||
| 448 | */ |
||
| 449 | public function setResult(SimpleDataStore $result): self { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $key |
||
| 457 | * @param SimpleDataStore $result |
||
| 458 | * |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | public function addResult(string $key, SimpleDataStore $result): self { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @param array $data |
||
| 474 | * |
||
| 475 | * @return self |
||
| 476 | * @throws InvalidItemException |
||
| 477 | */ |
||
| 478 | public function import(array $data): self { |
||
| 508 | |||
| 509 | |||
| 510 | /** |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | function jsonSerialize(): array { |
||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * @param int $flag |
||
| 538 | * |
||
| 539 | * @return FederatedEvent |
||
| 540 | */ |
||
| 541 | public function bypass(int $flag): self { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param int $flag |
||
| 551 | * |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | public function canBypass(int $flag): bool { |
||
| 557 | |||
| 558 | } |
||
| 559 | |||
| 560 |
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.