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 |
||
| 47 | class FederatedEvent implements JsonSerializable { |
||
| 48 | |||
| 49 | |||
| 50 | const SEVERITY_LOW = 1; |
||
| 51 | const SEVERITY_HIGH = 3; |
||
| 52 | |||
| 53 | const BYPASS_LOCALCIRCLECHECK = 1; |
||
| 54 | const BYPASS_LOCALMEMBERCHECK = 2; |
||
| 55 | const BYPASS_INITIATORCHECK = 4; |
||
| 56 | |||
| 57 | |||
| 58 | use TArrayTools; |
||
| 59 | |||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $class; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $source = ''; |
||
| 66 | |||
| 67 | /** @var Circle */ |
||
| 68 | private $circle; |
||
| 69 | |||
| 70 | /** @var Member */ |
||
| 71 | private $member; |
||
| 72 | |||
| 73 | /** @var SimpleDataStore */ |
||
| 74 | private $data; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | private $severity = self::SEVERITY_LOW; |
||
| 78 | |||
| 79 | /** @var SimpleDataStore */ |
||
| 80 | private $readingOutcome; |
||
| 81 | |||
| 82 | /** @var SimpleDataStore */ |
||
| 83 | private $dataOutcome; |
||
| 84 | |||
| 85 | /** @var SimpleDataStore */ |
||
| 86 | private $result; |
||
| 87 | |||
| 88 | /** @var bool */ |
||
| 89 | private $async = false; |
||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | private $incomingOrigin = ''; |
||
| 93 | |||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $wrapperToken = ''; |
||
| 97 | |||
| 98 | /** @var bool */ |
||
| 99 | private $verifiedViewer = false; |
||
| 100 | |||
| 101 | /** @var bool */ |
||
| 102 | private $verifiedCircle = false; |
||
| 103 | |||
| 104 | /** @var int */ |
||
| 105 | private $bypass = 0; |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * FederatedEvent constructor. |
||
| 110 | * |
||
| 111 | * @param string $class |
||
| 112 | */ |
||
| 113 | function __construct(string $class = '') { |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getClass(): string { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param mixed $class |
||
| 131 | * |
||
| 132 | * @return self |
||
| 133 | */ |
||
| 134 | public function setClass($class): self { |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getSource(): string { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $source |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | public function setSource(string $source): self { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function isAsync(): bool { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param bool $async |
||
| 184 | * |
||
| 185 | * @return self |
||
| 186 | */ |
||
| 187 | public function setAsync(bool $async): self { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $incomingOrigin |
||
| 195 | * |
||
| 196 | * @return self |
||
| 197 | */ |
||
| 198 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getIncomingOrigin(): string { |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * @param bool $verifiedViewer |
||
| 214 | * |
||
| 215 | * @return FederatedEvent |
||
| 216 | */ |
||
| 217 | public function setVerifiedViewer(bool $verifiedViewer): self { |
||
| 222 | |||
| 223 | // /** |
||
| 224 | // * @return bool |
||
| 225 | // */ |
||
| 226 | // public function isVerifiedViewer(): bool { |
||
| 227 | // return $this->verifiedViewer; |
||
| 228 | // } |
||
| 229 | |||
| 230 | // /** |
||
| 231 | // * @throws InitiatorNotConfirmedException |
||
| 232 | // */ |
||
| 233 | // public function confirmVerifiedViewer(): void { |
||
| 234 | // if ($this->isVerifiedViewer()) { |
||
| 235 | // return; |
||
| 236 | // } |
||
| 237 | // |
||
| 238 | // throw new InitiatorNotConfirmedException(); |
||
| 239 | // } |
||
| 240 | |||
| 241 | |||
| 242 | // /** |
||
| 243 | // * @param bool $verifiedCircle |
||
| 244 | // * |
||
| 245 | // * @return FederatedEvent |
||
| 246 | // */ |
||
| 247 | // public function setVerifiedCircle(bool $verifiedCircle): self { |
||
| 248 | // $this->verifiedCircle = $verifiedCircle; |
||
| 249 | // |
||
| 250 | // return $this; |
||
| 251 | // } |
||
| 252 | // |
||
| 253 | // /** |
||
| 254 | // * @return bool |
||
| 255 | // */ |
||
| 256 | // public function isVerifiedCircle(): bool { |
||
| 257 | // return $this->verifiedCircle; |
||
| 258 | // } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $wrapperToken |
||
| 263 | * |
||
| 264 | * @return FederatedEvent |
||
| 265 | */ |
||
| 266 | public function setWrapperToken(string $wrapperToken): self { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function getWrapperToken(): string { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function hasCircle(): bool { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param Circle $circle |
||
| 289 | * |
||
| 290 | * @return self |
||
| 291 | */ |
||
| 292 | public function setCircle(Circle $circle): self { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return Circle |
||
| 300 | */ |
||
| 301 | public function getCircle(): Circle { |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * @return Member |
||
| 308 | */ |
||
| 309 | public function getMember(): Member { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param Member|null $member |
||
| 315 | * |
||
| 316 | * @return self |
||
| 317 | */ |
||
| 318 | public function setMember(?Member $member): self { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function hasMember(): bool { |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * @param SimpleDataStore $data |
||
| 334 | * |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | public function setData(SimpleDataStore $data): self { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return SimpleDataStore |
||
| 345 | */ |
||
| 346 | public function getData(): SimpleDataStore { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | public function getSeverity(): int { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param int $severity |
||
| 360 | * |
||
| 361 | * @return self |
||
| 362 | */ |
||
| 363 | public function setSeverity(int $severity): self { |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * @return SimpleDataStore |
||
| 372 | */ |
||
| 373 | public function getOutcome(): SimpleDataStore { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return SimpleDataStore |
||
| 384 | */ |
||
| 385 | public function getReadingOutcome(): SimpleDataStore { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $message |
||
| 391 | * @param array $params |
||
| 392 | * @param bool $success |
||
| 393 | * |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | public function setReadingOutcome(string $message, array $params = [], bool $success = true): self { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return SimpleDataStore |
||
| 410 | */ |
||
| 411 | public function getDataOutcome(): SimpleDataStore { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param array $data |
||
| 417 | * |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function setDataOutcome(array $data): self { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * @return SimpleDataStore |
||
| 429 | */ |
||
| 430 | public function getResult(): SimpleDataStore { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param SimpleDataStore $result |
||
| 436 | * |
||
| 437 | * @return self |
||
| 438 | */ |
||
| 439 | public function setResult(SimpleDataStore $result): self { |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * @param array $data |
||
| 448 | * |
||
| 449 | * @return self |
||
| 450 | */ |
||
| 451 | public function import(array $data): self { |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | function jsonSerialize(): array { |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * @param int $flag |
||
| 505 | * |
||
| 506 | * @return FederatedEvent |
||
| 507 | */ |
||
| 508 | public function bypass(int $flag): self { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param int $flag |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | public function canBypass(int $flag): bool { |
||
| 524 | |||
| 525 | } |
||
| 526 | |||
| 527 |
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.