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 string */ |
||
| 75 | private $itemSource = ''; |
||
| 76 | |||
| 77 | /** @var Member */ |
||
| 78 | private $member; |
||
| 79 | |||
| 80 | /** @var Member[] */ |
||
| 81 | private $members = []; |
||
| 82 | |||
| 83 | /** @var SimpleDataStore */ |
||
| 84 | private $data; |
||
| 85 | |||
| 86 | /** @var int */ |
||
| 87 | private $severity = self::SEVERITY_LOW; |
||
| 88 | |||
| 89 | /** @var array */ |
||
| 90 | private $outcome = []; |
||
| 91 | |||
| 92 | /** @var SimpleDataStore */ |
||
| 93 | private $result; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | private $async = false; |
||
| 97 | |||
| 98 | /** @var bool */ |
||
| 99 | private $limitedToInstanceWithMember = false; |
||
| 100 | |||
| 101 | /** @var bool */ |
||
| 102 | private $dataRequestOnly = false; |
||
| 103 | |||
| 104 | /** @var string */ |
||
| 105 | private $incomingOrigin = ''; |
||
| 106 | |||
| 107 | |||
| 108 | /** @var string */ |
||
| 109 | private $wrapperToken = ''; |
||
| 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 = '') { |
||
|
|
|||
| 121 | $this->class = $class; |
||
| 122 | $this->data = new SimpleDataStore(); |
||
| 123 | $this->result = new SimpleDataStore(); |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getClass(): string { |
||
| 131 | return $this->class; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param mixed $class |
||
| 136 | * |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | public function setClass($class): self { |
||
| 140 | $this->class = $class; |
||
| 141 | |||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function getSource(): string { |
||
| 150 | return $this->source; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $source |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | public function setSource(string $source): self { |
||
| 159 | $this->source = $source; |
||
| 160 | |||
| 161 | if ($this->hasMember() && $this->member->getInstance() === '') { |
||
| 162 | $this->member->setInstance($source); |
||
| 163 | } |
||
| 164 | |||
| 165 | // if ($this->hasCircle() |
||
| 166 | // && $this->getCircle() |
||
| 167 | // ->hasViewer() |
||
| 168 | // && $this->getCircle() |
||
| 169 | // ->getViewer() |
||
| 170 | // ->getInstance() === '') { |
||
| 171 | // $this->getCircle() |
||
| 172 | // ->getViewer() |
||
| 173 | // ->setInstance($source); |
||
| 174 | // } |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function isAsync(): bool { |
||
| 184 | return $this->async; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param bool $async |
||
| 189 | * |
||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | public function setAsync(bool $async): self { |
||
| 193 | $this->async = $async; |
||
| 194 | |||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function isLimitedToInstanceWithMember(): bool { |
||
| 203 | return $this->limitedToInstanceWithMember; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param bool $limitedToInstanceWithMember |
||
| 208 | * |
||
| 209 | * @return self |
||
| 210 | */ |
||
| 211 | public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self { |
||
| 212 | $this->limitedToInstanceWithMember = $limitedToInstanceWithMember; |
||
| 213 | |||
| 214 | return $this; |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function isDataRequestOnly(): bool { |
||
| 222 | return $this->dataRequestOnly; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param bool $dataRequestOnly |
||
| 227 | * |
||
| 228 | * @return self |
||
| 229 | */ |
||
| 230 | public function setDataRequestOnly(bool $dataRequestOnly): self { |
||
| 231 | $this->dataRequestOnly = $dataRequestOnly; |
||
| 232 | |||
| 233 | return $this; |
||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $incomingOrigin |
||
| 239 | * |
||
| 240 | * @return self |
||
| 241 | */ |
||
| 242 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
| 243 | $this->incomingOrigin = $incomingOrigin; |
||
| 244 | |||
| 245 | return $this; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getIncomingOrigin(): string { |
||
| 252 | return $this->incomingOrigin; |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $wrapperToken |
||
| 258 | * |
||
| 259 | * @return FederatedEvent |
||
| 260 | */ |
||
| 261 | public function setWrapperToken(string $wrapperToken): self { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getWrapperToken(): string { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function hasCircle(): bool { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param Circle $circle |
||
| 284 | * |
||
| 285 | * @return self |
||
| 286 | */ |
||
| 287 | public function setCircle(Circle $circle): self { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return Circle |
||
| 295 | */ |
||
| 296 | public function getCircle(): Circle { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $itemId |
||
| 303 | * |
||
| 304 | * @return self |
||
| 305 | */ |
||
| 306 | public function setItemId(string $itemId): self { |
||
| 307 | $this->itemId = $itemId; |
||
| 308 | |||
| 309 | return $this; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getItemId(): string { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $itemSource |
||
| 322 | * |
||
| 323 | * @return self |
||
| 324 | */ |
||
| 325 | public function setItemSource(string $itemSource): self { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getItemSource(): string { |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * @return Member |
||
| 341 | */ |
||
| 342 | public function getMember(): Member { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param Member|null $member |
||
| 348 | * |
||
| 349 | * @return self |
||
| 350 | */ |
||
| 351 | public function setMember(?Member $member): self { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public function hasMember(): bool { |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * @return Member[] |
||
| 367 | */ |
||
| 368 | public function getMembers(): array { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param Member[] $members |
||
| 374 | * |
||
| 375 | * @return self |
||
| 376 | */ |
||
| 377 | public function setMembers(array $members): self { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param SimpleDataStore $data |
||
| 386 | * |
||
| 387 | * @return self |
||
| 388 | */ |
||
| 389 | public function setData(SimpleDataStore $data): self { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return SimpleDataStore |
||
| 397 | */ |
||
| 398 | public function getData(): SimpleDataStore { |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * @return int |
||
| 405 | */ |
||
| 406 | public function getSeverity(): int { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param int $severity |
||
| 412 | * |
||
| 413 | * @return self |
||
| 414 | */ |
||
| 415 | public function setSeverity(int $severity): self { |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | public function getOutcome(): array { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param array $data |
||
| 431 | * |
||
| 432 | * @return $this |
||
| 433 | */ |
||
| 434 | public function setOutcome(array $data): self { |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * @return SimpleDataStore |
||
| 443 | */ |
||
| 444 | public function getResult(): SimpleDataStore { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param SimpleDataStore $result |
||
| 450 | * |
||
| 451 | * @return self |
||
| 452 | */ |
||
| 453 | public function setResult(SimpleDataStore $result): self { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $key |
||
| 461 | * @param SimpleDataStore $result |
||
| 462 | * |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | public function addResult(string $key, SimpleDataStore $result): self { |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * @param array $data |
||
| 478 | * |
||
| 479 | * @return self |
||
| 480 | * @throws InvalidItemException |
||
| 481 | */ |
||
| 482 | public function import(array $data): self { |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | function jsonSerialize(): array { |
||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * @param int $flag |
||
| 539 | * |
||
| 540 | * @return FederatedEvent |
||
| 541 | */ |
||
| 542 | public function bypass(int $flag): self { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param int $flag |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | public function canBypass(int $flag): bool { |
||
| 558 | |||
| 559 | } |
||
| 560 | |||
| 561 |
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.