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 array */ |
||
| 70 | private $originPerInterfaces = []; |
||
| 71 | |||
| 72 | /** @var Circle */ |
||
| 73 | private $circle; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | private $itemId = ''; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | private $itemSource = ''; |
||
| 80 | |||
| 81 | /** @var Member */ |
||
| 82 | private $member; |
||
| 83 | |||
| 84 | /** @var Member[] */ |
||
| 85 | private $members = []; |
||
| 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 = '') { |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function getClass(): string { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param mixed $class |
||
| 140 | * |
||
| 141 | * @return self |
||
| 142 | */ |
||
| 143 | public function setClass($class): self { |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Origin of the event. |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getOrigin(): string { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $origin |
||
| 161 | * |
||
| 162 | * @return self |
||
| 163 | */ |
||
| 164 | public function setOrigin(string $origin): self { |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function getOriginPerInterfaces(): array { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array $originPerInterfaces |
||
| 180 | * |
||
| 181 | * @return FederatedEvent |
||
| 182 | */ |
||
| 183 | public function setOriginPerInterfaces(array $originPerInterfaces): self { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function obfuscateOriginPerInterfaces(): self { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function isAsync(): bool { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param bool $async |
||
| 208 | * |
||
| 209 | * @return self |
||
| 210 | */ |
||
| 211 | public function setAsync(bool $async): self { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function isLimitedToInstanceWithMember(): bool { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param bool $limitedToInstanceWithMember |
||
| 227 | * |
||
| 228 | * @return self |
||
| 229 | */ |
||
| 230 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function isDataRequestOnly(): bool { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param bool $dataRequestOnly |
||
| 246 | * |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * |
||
| 258 | * Origin of the request |
||
| 259 | * |
||
| 260 | * @param string $sender |
||
| 261 | * |
||
| 262 | * @return self |
||
| 263 | */ |
||
| 264 | public function setSender(string $sender): self { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getSender(): string { |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $wrapperToken |
||
| 280 | * |
||
| 281 | * @return FederatedEvent |
||
| 282 | */ |
||
| 283 | public function setWrapperToken(string $wrapperToken): self { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getWrapperToken(): string { |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | public function hasCircle(): bool { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param Circle $circle |
||
| 306 | * |
||
| 307 | * @return self |
||
| 308 | */ |
||
| 309 | public function setCircle(Circle $circle): self { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return Circle |
||
| 317 | */ |
||
| 318 | public function getCircle(): Circle { |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $itemId |
||
| 325 | * |
||
| 326 | * @return self |
||
| 327 | */ |
||
| 328 | public function setItemId(string $itemId): self { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function getItemId(): string { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $itemSource |
||
| 344 | * |
||
| 345 | * @return self |
||
| 346 | */ |
||
| 347 | public function setItemSource(string $itemSource): self { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function getItemSource(): string { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * @return Member |
||
| 363 | */ |
||
| 364 | public function getMember(): Member { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param Member|null $member |
||
| 370 | * |
||
| 371 | * @return self |
||
| 372 | */ |
||
| 373 | public function setMember(?Member $member): self { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public function hasMember(): bool { |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * @return Member[] |
||
| 389 | */ |
||
| 390 | public function getMembers(): array { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param Member[] $members |
||
| 396 | * |
||
| 397 | * @return self |
||
| 398 | */ |
||
| 399 | public function setMembers(array $members): self { |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * @param SimpleDataStore $data |
||
| 408 | * |
||
| 409 | * @return self |
||
| 410 | */ |
||
| 411 | public function setData(SimpleDataStore $data): self { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return SimpleDataStore |
||
| 419 | */ |
||
| 420 | public function getData(): SimpleDataStore { |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * @return int |
||
| 427 | */ |
||
| 428 | public function getSeverity(): int { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param int $severity |
||
| 434 | * |
||
| 435 | * @return self |
||
| 436 | */ |
||
| 437 | public function setSeverity(int $severity): self { |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getOutcome(): array { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param array $data |
||
| 453 | * |
||
| 454 | * @return $this |
||
| 455 | */ |
||
| 456 | public function setOutcome(array $data): self { |
||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * @return SimpleDataStore |
||
| 465 | */ |
||
| 466 | public function getResult(): SimpleDataStore { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param SimpleDataStore $result |
||
| 472 | * |
||
| 473 | * @return self |
||
| 474 | */ |
||
| 475 | public function setResult(SimpleDataStore $result): self { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $key |
||
| 483 | * @param array $result |
||
| 484 | * |
||
| 485 | * @return $this |
||
| 486 | */ |
||
| 487 | public function addResult(string $key, array $result): self { |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * @param array $data |
||
| 500 | * |
||
| 501 | * @return self |
||
| 502 | * @throws InvalidItemException |
||
| 503 | */ |
||
| 504 | public function import(array $data): self { |
||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | function jsonSerialize(): array { |
||
| 561 | |||
| 562 | |||
| 563 | /** |
||
| 564 | * @param int $flag |
||
| 565 | * |
||
| 566 | * @return FederatedEvent |
||
| 567 | */ |
||
| 568 | public function bypass(int $flag): self { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param int $flag |
||
| 578 | * |
||
| 579 | * @return bool |
||
| 580 | */ |
||
| 581 | public function canBypass(int $flag): bool { |
||
| 584 | |||
| 585 | } |
||
| 586 | |||
| 587 |
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.