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 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 $limitedToInstanceWithMember = false; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | private $dataRequestOnly = false; |
||
| 97 | |||
| 98 | /** @var string */ |
||
| 99 | private $incomingOrigin = ''; |
||
| 100 | |||
| 101 | |||
| 102 | /** @var string */ |
||
| 103 | private $wrapperToken = ''; |
||
| 104 | |||
| 105 | /** @var bool */ |
||
| 106 | private $verifiedViewer = false; |
||
| 107 | |||
| 108 | /** @var bool */ |
||
| 109 | private $verifiedCircle = false; |
||
| 110 | |||
| 111 | /** @var int */ |
||
| 112 | private $bypass = 0; |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * FederatedEvent constructor. |
||
| 117 | * |
||
| 118 | * @param string $class |
||
| 119 | */ |
||
| 120 | function __construct(string $class = '') { |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getClass(): string { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param mixed $class |
||
| 138 | * |
||
| 139 | * @return self |
||
| 140 | */ |
||
| 141 | public function setClass($class): self { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getSource(): string { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $source |
||
| 157 | * |
||
| 158 | * @return self |
||
| 159 | */ |
||
| 160 | public function setSource(string $source): self { |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function isAsync(): bool { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param bool $async |
||
| 191 | * |
||
| 192 | * @return self |
||
| 193 | */ |
||
| 194 | public function setAsync(bool $async): self { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function isLimitedToInstanceWithMember(): bool { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param bool $limitedToInstanceWithMember |
||
| 210 | * |
||
| 211 | * @return self |
||
| 212 | */ |
||
| 213 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isDataRequestOnly(): bool { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param bool $dataRequestOnly |
||
| 230 | * |
||
| 231 | * @return self |
||
| 232 | */ |
||
| 233 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $incomingOrigin |
||
| 242 | * |
||
| 243 | * @return self |
||
| 244 | */ |
||
| 245 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getIncomingOrigin(): string { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * @param bool $verifiedViewer |
||
| 261 | * |
||
| 262 | * @return FederatedEvent |
||
| 263 | */ |
||
| 264 | public function setVerifiedViewer(bool $verifiedViewer): self { |
||
| 269 | |||
| 270 | // /** |
||
| 271 | // * @return bool |
||
| 272 | // */ |
||
| 273 | // public function isVerifiedViewer(): bool { |
||
| 274 | // return $this->verifiedViewer; |
||
| 275 | // } |
||
| 276 | |||
| 277 | // /** |
||
| 278 | // * @throws InitiatorNotConfirmedException |
||
| 279 | // */ |
||
| 280 | // public function confirmVerifiedViewer(): void { |
||
| 281 | // if ($this->isVerifiedViewer()) { |
||
| 282 | // return; |
||
| 283 | // } |
||
| 284 | // |
||
| 285 | // throw new InitiatorNotConfirmedException(); |
||
| 286 | // } |
||
| 287 | |||
| 288 | |||
| 289 | // /** |
||
| 290 | // * @param bool $verifiedCircle |
||
| 291 | // * |
||
| 292 | // * @return FederatedEvent |
||
| 293 | // */ |
||
| 294 | // public function setVerifiedCircle(bool $verifiedCircle): self { |
||
| 295 | // $this->verifiedCircle = $verifiedCircle; |
||
| 296 | // |
||
| 297 | // return $this; |
||
| 298 | // } |
||
| 299 | // |
||
| 300 | // /** |
||
| 301 | // * @return bool |
||
| 302 | // */ |
||
| 303 | // public function isVerifiedCircle(): bool { |
||
| 304 | // return $this->verifiedCircle; |
||
| 305 | // } |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $wrapperToken |
||
| 310 | * |
||
| 311 | * @return FederatedEvent |
||
| 312 | */ |
||
| 313 | public function setWrapperToken(string $wrapperToken): self { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getWrapperToken(): string { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function hasCircle(): bool { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param Circle $circle |
||
| 336 | * |
||
| 337 | * @return self |
||
| 338 | */ |
||
| 339 | public function setCircle(Circle $circle): self { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return Circle |
||
| 347 | */ |
||
| 348 | public function getCircle(): Circle { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * @return Member |
||
| 355 | */ |
||
| 356 | public function getMember(): Member { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param Member|null $member |
||
| 362 | * |
||
| 363 | * @return self |
||
| 364 | */ |
||
| 365 | public function setMember(?Member $member): self { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function hasMember(): bool { |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * @param SimpleDataStore $data |
||
| 381 | * |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | public function setData(SimpleDataStore $data): self { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return SimpleDataStore |
||
| 392 | */ |
||
| 393 | public function getData(): SimpleDataStore { |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * @return int |
||
| 400 | */ |
||
| 401 | public function getSeverity(): int { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param int $severity |
||
| 407 | * |
||
| 408 | * @return self |
||
| 409 | */ |
||
| 410 | public function setSeverity(int $severity): self { |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @return SimpleDataStore |
||
| 419 | */ |
||
| 420 | public function getOutcome(): SimpleDataStore { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return SimpleDataStore |
||
| 431 | */ |
||
| 432 | public function getReadingOutcome(): SimpleDataStore { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $message |
||
| 438 | * @param array $params |
||
| 439 | * @param bool $fail |
||
| 440 | * |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function setReadingOutcome(string $message, array $params = [], bool $fail = false): self { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * @return SimpleDataStore |
||
| 458 | */ |
||
| 459 | public function getDataOutcome(): SimpleDataStore { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param array $data |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | */ |
||
| 468 | public function setDataOutcome(array $data): self { |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * @return SimpleDataStore |
||
| 477 | */ |
||
| 478 | public function getResult(): SimpleDataStore { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param SimpleDataStore $result |
||
| 484 | * |
||
| 485 | * @return self |
||
| 486 | */ |
||
| 487 | public function setResult(SimpleDataStore $result): self { |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * @param array $data |
||
| 496 | * |
||
| 497 | * @return self |
||
| 498 | * @throws InvalidItemException |
||
| 499 | */ |
||
| 500 | public function import(array $data): self { |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | function jsonSerialize(): array { |
||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * @param int $flag |
||
| 552 | * |
||
| 553 | * @return FederatedEvent |
||
| 554 | */ |
||
| 555 | public function bypass(int $flag): self { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param int $flag |
||
| 565 | * |
||
| 566 | * @return bool |
||
| 567 | */ |
||
| 568 | public function canBypass(int $flag): bool { |
||
| 571 | |||
| 572 | } |
||
| 573 | |||
| 574 |
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.