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 $interfaces = []; |
||
| 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 $params; |
||
| 89 | |||
| 90 | /** @var SimpleDataStore */ |
||
| 91 | private $data; |
||
| 92 | |||
| 93 | /** @var int */ |
||
| 94 | private $severity = self::SEVERITY_LOW; |
||
| 95 | |||
| 96 | /** @var array */ |
||
| 97 | private $outcome = []; |
||
| 98 | |||
| 99 | /** @var SimpleDataStore */ |
||
| 100 | private $result; |
||
| 101 | |||
| 102 | /** @var bool */ |
||
| 103 | private $async = false; |
||
| 104 | |||
| 105 | /** @var bool */ |
||
| 106 | private $limitedToInstanceWithMember = false; |
||
| 107 | |||
| 108 | /** @var bool */ |
||
| 109 | private $dataRequestOnly = false; |
||
| 110 | |||
| 111 | /** @var string */ |
||
| 112 | private $sender = ''; |
||
| 113 | |||
| 114 | |||
| 115 | /** @var string */ |
||
| 116 | private $wrapperToken = ''; |
||
| 117 | |||
| 118 | /** @var int */ |
||
| 119 | private $bypass = 0; |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * FederatedEvent constructor. |
||
| 124 | * |
||
| 125 | * @param string $class |
||
| 126 | */ |
||
| 127 | function __construct(string $class = '') { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getClass(): string { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param mixed $class |
||
| 144 | * |
||
| 145 | * @return self |
||
| 146 | */ |
||
| 147 | public function setClass($class): self { |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Origin of the event. |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function getOrigin(): string { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $origin |
||
| 165 | * |
||
| 166 | * @return self |
||
| 167 | */ |
||
| 168 | public function setOrigin(string $origin): self { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getInterfaces(): array { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param array $interfaces |
||
| 184 | * |
||
| 185 | * @return FederatedEvent |
||
| 186 | */ |
||
| 187 | public function setInterfaces(array $interfaces): self { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function obfuscateInterfaces(): self { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isAsync(): bool { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param bool $async |
||
| 212 | * |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | public function setAsync(bool $async): self { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function isLimitedToInstanceWithMember(): bool { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param bool $limitedToInstanceWithMember |
||
| 231 | * |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function isDataRequestOnly(): bool { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param bool $dataRequestOnly |
||
| 250 | * |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * |
||
| 262 | * Origin of the request |
||
| 263 | * |
||
| 264 | * @param string $sender |
||
| 265 | * |
||
| 266 | * @return self |
||
| 267 | */ |
||
| 268 | public function setSender(string $sender): self { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getSender(): string { |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $wrapperToken |
||
| 284 | * |
||
| 285 | * @return FederatedEvent |
||
| 286 | */ |
||
| 287 | public function setWrapperToken(string $wrapperToken): self { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getWrapperToken(): string { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function hasCircle(): bool { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param Circle $circle |
||
| 310 | * |
||
| 311 | * @return self |
||
| 312 | */ |
||
| 313 | public function setCircle(Circle $circle): self { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return Circle |
||
| 321 | */ |
||
| 322 | public function getCircle(): Circle { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $itemId |
||
| 329 | * |
||
| 330 | * @return self |
||
| 331 | */ |
||
| 332 | public function setItemId(string $itemId): self { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getItemId(): string { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $itemSource |
||
| 348 | * |
||
| 349 | * @return self |
||
| 350 | */ |
||
| 351 | public function setItemSource(string $itemSource): self { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getItemSource(): string { |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * @return Member |
||
| 367 | */ |
||
| 368 | public function getMember(): Member { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param Member|null $member |
||
| 374 | * |
||
| 375 | * @return self |
||
| 376 | */ |
||
| 377 | public function setMember(?Member $member): self { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function hasMember(): bool { |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * @return Member[] |
||
| 393 | */ |
||
| 394 | public function getMembers(): array { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param Member[] $members |
||
| 400 | * |
||
| 401 | * @return self |
||
| 402 | */ |
||
| 403 | public function setMembers(array $members): self { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @param SimpleDataStore $params |
||
| 412 | * |
||
| 413 | * @return self |
||
| 414 | */ |
||
| 415 | public function setParams(SimpleDataStore $params): self { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return SimpleDataStore |
||
| 423 | */ |
||
| 424 | public function getParams(): SimpleDataStore { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param SimpleDataStore $data |
||
| 431 | * |
||
| 432 | * @return self |
||
| 433 | */ |
||
| 434 | public function setData(SimpleDataStore $data): self { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return SimpleDataStore |
||
| 442 | */ |
||
| 443 | public function getData(): SimpleDataStore { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function resetData(): self { |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * @return int |
||
| 459 | */ |
||
| 460 | public function getSeverity(): int { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param int $severity |
||
| 466 | * |
||
| 467 | * @return self |
||
| 468 | */ |
||
| 469 | public function setSeverity(int $severity): self { |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function getOutcome(): array { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param array $data |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | public function setOutcome(array $data): self { |
||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * @return SimpleDataStore |
||
| 497 | */ |
||
| 498 | public function getResult(): SimpleDataStore { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param SimpleDataStore $result |
||
| 504 | * |
||
| 505 | * @return self |
||
| 506 | */ |
||
| 507 | public function setResult(SimpleDataStore $result): self { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $key |
||
| 515 | * @param array $result |
||
| 516 | * |
||
| 517 | * @return $this |
||
| 518 | */ |
||
| 519 | public function addResult(string $key, array $result): self { |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * @param int $flag |
||
| 532 | * |
||
| 533 | * @return FederatedEvent |
||
| 534 | */ |
||
| 535 | public function bypass(int $flag): self { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param int $flag |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function canBypass(int $flag): bool { |
||
| 551 | |||
| 552 | |||
| 553 | /** |
||
| 554 | * @param array $data |
||
| 555 | * |
||
| 556 | * @return self |
||
| 557 | * @throws InvalidItemException |
||
| 558 | */ |
||
| 559 | public function import(array $data): self { |
||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | function jsonSerialize(): array { |
||
| 620 | |||
| 621 | } |
||
| 622 | |||
| 623 |
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.