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 | const BYPASS_INITIATORMEMBERSHIP = 8; |
||
| 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 string */ |
||
| 72 | private $itemId = ''; |
||
| 73 | |||
| 74 | /** @var Member */ |
||
| 75 | private $member; |
||
| 76 | |||
| 77 | /** @var SimpleDataStore */ |
||
| 78 | private $data; |
||
| 79 | |||
| 80 | /** @var int */ |
||
| 81 | private $severity = self::SEVERITY_LOW; |
||
| 82 | |||
| 83 | /** @var SimpleDataStore */ |
||
| 84 | private $readingOutcome; |
||
| 85 | |||
| 86 | /** @var SimpleDataStore */ |
||
| 87 | private $dataOutcome; |
||
| 88 | |||
| 89 | /** @var SimpleDataStore */ |
||
| 90 | private $result; |
||
| 91 | |||
| 92 | /** @var bool */ |
||
| 93 | private $async = false; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | private $limitedToInstanceWithMember = false; |
||
| 97 | |||
| 98 | /** @var bool */ |
||
| 99 | private $dataRequestOnly = false; |
||
| 100 | |||
| 101 | /** @var string */ |
||
| 102 | private $incomingOrigin = ''; |
||
| 103 | |||
| 104 | |||
| 105 | /** @var string */ |
||
| 106 | private $wrapperToken = ''; |
||
| 107 | |||
| 108 | /** @var bool */ |
||
| 109 | private $verifiedViewer = false; |
||
| 110 | |||
| 111 | /** @var bool */ |
||
| 112 | private $verifiedCircle = false; |
||
| 113 | |||
| 114 | /** @var int */ |
||
| 115 | private $bypass = 0; |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * FederatedEvent constructor. |
||
| 120 | * |
||
| 121 | * @param string $class |
||
| 122 | */ |
||
| 123 | function __construct(string $class = '') { |
||
| 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 | * @return string |
||
| 153 | */ |
||
| 154 | public function getSource(): string { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $source |
||
| 160 | * |
||
| 161 | * @return self |
||
| 162 | */ |
||
| 163 | public function setSource(string $source): self { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function isAsync(): bool { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param bool $async |
||
| 194 | * |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | public function setAsync(bool $async): self { |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isLimitedToInstanceWithMember(): bool { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param bool $limitedToInstanceWithMember |
||
| 213 | * |
||
| 214 | * @return self |
||
| 215 | */ |
||
| 216 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function isDataRequestOnly(): bool { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param bool $dataRequestOnly |
||
| 232 | * |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $incomingOrigin |
||
| 244 | * |
||
| 245 | * @return self |
||
| 246 | */ |
||
| 247 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getIncomingOrigin(): string { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * @param bool $verifiedViewer |
||
| 263 | * |
||
| 264 | * @return FederatedEvent |
||
| 265 | */ |
||
| 266 | public function setVerifiedViewer(bool $verifiedViewer): self { |
||
| 271 | |||
| 272 | // /** |
||
| 273 | // * @return bool |
||
| 274 | // */ |
||
| 275 | // public function isVerifiedViewer(): bool { |
||
| 276 | // return $this->verifiedViewer; |
||
| 277 | // } |
||
| 278 | |||
| 279 | // /** |
||
| 280 | // * @throws InitiatorNotConfirmedException |
||
| 281 | // */ |
||
| 282 | // public function confirmVerifiedViewer(): void { |
||
| 283 | // if ($this->isVerifiedViewer()) { |
||
| 284 | // return; |
||
| 285 | // } |
||
| 286 | // |
||
| 287 | // throw new InitiatorNotConfirmedException(); |
||
| 288 | // } |
||
| 289 | |||
| 290 | |||
| 291 | // /** |
||
| 292 | // * @param bool $verifiedCircle |
||
| 293 | // * |
||
| 294 | // * @return FederatedEvent |
||
| 295 | // */ |
||
| 296 | // public function setVerifiedCircle(bool $verifiedCircle): self { |
||
| 297 | // $this->verifiedCircle = $verifiedCircle; |
||
| 298 | // |
||
| 299 | // return $this; |
||
| 300 | // } |
||
| 301 | // |
||
| 302 | // /** |
||
| 303 | // * @return bool |
||
| 304 | // */ |
||
| 305 | // public function isVerifiedCircle(): bool { |
||
| 306 | // return $this->verifiedCircle; |
||
| 307 | // } |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $wrapperToken |
||
| 312 | * |
||
| 313 | * @return FederatedEvent |
||
| 314 | */ |
||
| 315 | public function setWrapperToken(string $wrapperToken): self { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getWrapperToken(): string { |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function hasCircle(): bool { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param Circle $circle |
||
| 338 | * |
||
| 339 | * @return self |
||
| 340 | */ |
||
| 341 | public function setCircle(Circle $circle): self { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return Circle |
||
| 349 | */ |
||
| 350 | public function getCircle(): Circle { |
||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $itemId |
||
| 358 | * |
||
| 359 | * @return self |
||
| 360 | */ |
||
| 361 | public function setItemId(string $itemId): self { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function getItemId(): string { |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * @return Member |
||
| 377 | */ |
||
| 378 | public function getMember(): Member { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param Member|null $member |
||
| 384 | * |
||
| 385 | * @return self |
||
| 386 | */ |
||
| 387 | public function setMember(?Member $member): self { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | public function hasMember(): bool { |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * @param SimpleDataStore $data |
||
| 403 | * |
||
| 404 | * @return self |
||
| 405 | */ |
||
| 406 | public function setData(SimpleDataStore $data): self { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return SimpleDataStore |
||
| 414 | */ |
||
| 415 | public function getData(): SimpleDataStore { |
||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * @return int |
||
| 422 | */ |
||
| 423 | public function getSeverity(): int { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param int $severity |
||
| 429 | * |
||
| 430 | * @return self |
||
| 431 | */ |
||
| 432 | public function setSeverity(int $severity): self { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * @return SimpleDataStore |
||
| 441 | */ |
||
| 442 | public function getOutcome(): SimpleDataStore { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return SimpleDataStore |
||
| 453 | */ |
||
| 454 | public function getReadingOutcome(): SimpleDataStore { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param string $message |
||
| 460 | * @param array $params |
||
| 461 | * @param bool $fail |
||
| 462 | * |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | public function setReadingOutcome(string $message, array $params = [], bool $fail = false): self { |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * @return SimpleDataStore |
||
| 480 | */ |
||
| 481 | public function getDataOutcome(): SimpleDataStore { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param array $data |
||
| 487 | * |
||
| 488 | * @return $this |
||
| 489 | */ |
||
| 490 | public function setDataOutcome(array $data): self { |
||
| 495 | |||
| 496 | |||
| 497 | /** |
||
| 498 | * @return SimpleDataStore |
||
| 499 | */ |
||
| 500 | public function getResult(): SimpleDataStore { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param SimpleDataStore $result |
||
| 506 | * |
||
| 507 | * @return self |
||
| 508 | */ |
||
| 509 | public function setResult(SimpleDataStore $result): self { |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * @param array $data |
||
| 518 | * |
||
| 519 | * @return self |
||
| 520 | * @throws InvalidItemException |
||
| 521 | */ |
||
| 522 | public function import(array $data): self { |
||
| 543 | |||
| 544 | |||
| 545 | /** |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | function jsonSerialize(): array { |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * @param int $flag |
||
| 574 | * |
||
| 575 | * @return FederatedEvent |
||
| 576 | */ |
||
| 577 | public function bypass(int $flag): self { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param int $flag |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | public function canBypass(int $flag): bool { |
||
| 593 | |||
| 594 | } |
||
| 595 | |||
| 596 |
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.