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 | |||
| 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 string */ | ||
| 93 | private $incomingOrigin = ''; | ||
| 94 | |||
| 95 | |||
| 96 | /** @var string */ | ||
| 97 | private $wrapperToken = ''; | ||
| 98 | |||
| 99 | /** @var bool */ | ||
| 100 | private $verifiedViewer = false; | ||
| 101 | |||
| 102 | /** @var bool */ | ||
| 103 | private $verifiedCircle = false; | ||
| 104 | |||
| 105 | /** @var int */ | ||
| 106 | private $bypass = 0; | ||
| 107 | |||
| 108 | |||
| 109 | /** | ||
| 110 | * FederatedEvent constructor. | ||
| 111 | * | ||
| 112 | * @param string $class | ||
| 113 | */ | ||
| 114 | 	function __construct(string $class = '') { | ||
|  | |||
| 115 | $this->class = $class; | ||
| 116 | $this->data = new SimpleDataStore(); | ||
| 117 | $this->result = new SimpleDataStore(); | ||
| 118 | $this->readingOutcome = new SimpleDataStore(); | ||
| 119 | $this->dataOutcome = new SimpleDataStore(); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | /** | ||
| 124 | * @return string | ||
| 125 | */ | ||
| 126 | 	public function getClass(): string { | ||
| 127 | return $this->class; | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * @param mixed $class | ||
| 132 | * | ||
| 133 | * @return self | ||
| 134 | */ | ||
| 135 | 	public function setClass($class): self { | ||
| 136 | $this->class = $class; | ||
| 137 | |||
| 138 | return $this; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | /** | ||
| 143 | * @return string | ||
| 144 | */ | ||
| 145 | 	public function getSource(): string { | ||
| 146 | return $this->source; | ||
| 147 | } | ||
| 148 | |||
| 149 | /** | ||
| 150 | * @param string $source | ||
| 151 | * | ||
| 152 | * @return self | ||
| 153 | */ | ||
| 154 | 	public function setSource(string $source): self { | ||
| 155 | $this->source = $source; | ||
| 156 | |||
| 157 | 		if ($this->hasMember() && $this->member->getInstance() === '') { | ||
| 158 | $this->member->setInstance($source); | ||
| 159 | } | ||
| 160 | |||
| 161 | // if ($this->hasCircle() | ||
| 162 | // && $this->getCircle() | ||
| 163 | // ->hasViewer() | ||
| 164 | // && $this->getCircle() | ||
| 165 | // ->getViewer() | ||
| 166 | //					->getInstance() === '') { | ||
| 167 | // $this->getCircle() | ||
| 168 | // ->getViewer() | ||
| 169 | // ->setInstance($source); | ||
| 170 | // } | ||
| 171 | |||
| 172 | return $this; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | /** | ||
| 177 | * @return bool | ||
| 178 | */ | ||
| 179 | 	public function isAsync(): bool { | ||
| 180 | return $this->async; | ||
| 181 | } | ||
| 182 | |||
| 183 | /** | ||
| 184 | * @param bool $async | ||
| 185 | * | ||
| 186 | * @return self | ||
| 187 | */ | ||
| 188 | 	public function setAsync(bool $async): self { | ||
| 189 | $this->async = $async; | ||
| 190 | |||
| 191 | return $this; | ||
| 192 | } | ||
| 193 | |||
| 194 | /** | ||
| 195 | * @param string $incomingOrigin | ||
| 196 | * | ||
| 197 | * @return self | ||
| 198 | */ | ||
| 199 | 	public function setIncomingOrigin(string $incomingOrigin): self { | ||
| 200 | $this->incomingOrigin = $incomingOrigin; | ||
| 201 | |||
| 202 | return $this; | ||
| 203 | } | ||
| 204 | |||
| 205 | /** | ||
| 206 | * @return string | ||
| 207 | */ | ||
| 208 | 	public function getIncomingOrigin(): string { | ||
| 209 | return $this->incomingOrigin; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | /** | ||
| 214 | * @param bool $verifiedViewer | ||
| 215 | * | ||
| 216 | * @return FederatedEvent | ||
| 217 | */ | ||
| 218 | 	public function setVerifiedViewer(bool $verifiedViewer): self { | ||
| 219 | $this->verifiedViewer = $verifiedViewer; | ||
| 220 | |||
| 221 | return $this; | ||
| 222 | } | ||
| 223 | |||
| 224 | // /** | ||
| 225 | // * @return bool | ||
| 226 | // */ | ||
| 227 | //	public function isVerifiedViewer(): bool { | ||
| 228 | // return $this->verifiedViewer; | ||
| 229 | // } | ||
| 230 | |||
| 231 | // /** | ||
| 232 | // * @throws InitiatorNotConfirmedException | ||
| 233 | // */ | ||
| 234 | //	public function confirmVerifiedViewer(): void { | ||
| 235 | //		if ($this->isVerifiedViewer()) { | ||
| 236 | // return; | ||
| 237 | // } | ||
| 238 | // | ||
| 239 | // throw new InitiatorNotConfirmedException(); | ||
| 240 | // } | ||
| 241 | |||
| 242 | |||
| 243 | // /** | ||
| 244 | // * @param bool $verifiedCircle | ||
| 245 | // * | ||
| 246 | // * @return FederatedEvent | ||
| 247 | // */ | ||
| 248 | //	public function setVerifiedCircle(bool $verifiedCircle): self { | ||
| 249 | // $this->verifiedCircle = $verifiedCircle; | ||
| 250 | // | ||
| 251 | // return $this; | ||
| 252 | // } | ||
| 253 | // | ||
| 254 | // /** | ||
| 255 | // * @return bool | ||
| 256 | // */ | ||
| 257 | //	public function isVerifiedCircle(): bool { | ||
| 258 | // return $this->verifiedCircle; | ||
| 259 | // } | ||
| 260 | |||
| 261 | |||
| 262 | /** | ||
| 263 | * @param string $wrapperToken | ||
| 264 | * | ||
| 265 | * @return FederatedEvent | ||
| 266 | */ | ||
| 267 | 	public function setWrapperToken(string $wrapperToken): self { | ||
| 268 | $this->wrapperToken = $wrapperToken; | ||
| 269 | |||
| 270 | return $this; | ||
| 271 | } | ||
| 272 | |||
| 273 | /** | ||
| 274 | * @return string | ||
| 275 | */ | ||
| 276 | 	public function getWrapperToken(): string { | ||
| 277 | return $this->wrapperToken; | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | /** | ||
| 282 | * @return bool | ||
| 283 | */ | ||
| 284 | 	public function hasCircle(): bool { | ||
| 285 | return ($this->circle !== null); | ||
| 286 | } | ||
| 287 | |||
| 288 | /** | ||
| 289 | * @param Circle $circle | ||
| 290 | * | ||
| 291 | * @return self | ||
| 292 | */ | ||
| 293 | 	public function setCircle(Circle $circle): self { | ||
| 294 | $this->circle = $circle; | ||
| 295 | |||
| 296 | return $this; | ||
| 297 | } | ||
| 298 | |||
| 299 | /** | ||
| 300 | * @return Circle | ||
| 301 | */ | ||
| 302 | 	public function getCircle(): Circle { | ||
| 303 | return $this->circle; | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | /** | ||
| 308 | * @return Member | ||
| 309 | */ | ||
| 310 | 	public function getMember(): Member { | ||
| 311 | return $this->member; | ||
| 312 | } | ||
| 313 | |||
| 314 | /** | ||
| 315 | * @param Member|null $member | ||
| 316 | * | ||
| 317 | * @return self | ||
| 318 | */ | ||
| 319 | 	public function setMember(?Member $member): self { | ||
| 320 | $this->member = $member; | ||
| 321 | |||
| 322 | return $this; | ||
| 323 | } | ||
| 324 | |||
| 325 | /** | ||
| 326 | * @return bool | ||
| 327 | */ | ||
| 328 | 	public function hasMember(): bool { | ||
| 329 | return ($this->member !== null); | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | /** | ||
| 334 | * @param SimpleDataStore $data | ||
| 335 | * | ||
| 336 | * @return self | ||
| 337 | */ | ||
| 338 | 	public function setData(SimpleDataStore $data): self { | ||
| 339 | $this->data = $data; | ||
| 340 | |||
| 341 | return $this; | ||
| 342 | } | ||
| 343 | |||
| 344 | /** | ||
| 345 | * @return SimpleDataStore | ||
| 346 | */ | ||
| 347 | 	public function getData(): SimpleDataStore { | ||
| 348 | return $this->data; | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | /** | ||
| 353 | * @return int | ||
| 354 | */ | ||
| 355 | 	public function getSeverity(): int { | ||
| 356 | return $this->severity; | ||
| 357 | } | ||
| 358 | |||
| 359 | /** | ||
| 360 | * @param int $severity | ||
| 361 | * | ||
| 362 | * @return self | ||
| 363 | */ | ||
| 364 | 	public function setSeverity(int $severity): self { | ||
| 365 | $this->severity = $severity; | ||
| 366 | |||
| 367 | return $this; | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | /** | ||
| 372 | * @return SimpleDataStore | ||
| 373 | */ | ||
| 374 | 	public function getOutcome(): SimpleDataStore { | ||
| 375 | return new SimpleDataStore( | ||
| 376 | [ | ||
| 377 | 'reading' => $this->getReadingOutcome(), | ||
| 378 | 'data' => $this->getDataOutcome() | ||
| 379 | ] | ||
| 380 | ); | ||
| 381 | } | ||
| 382 | |||
| 383 | /** | ||
| 384 | * @return SimpleDataStore | ||
| 385 | */ | ||
| 386 | 	public function getReadingOutcome(): SimpleDataStore { | ||
| 387 | return $this->readingOutcome; | ||
| 388 | } | ||
| 389 | |||
| 390 | /** | ||
| 391 | * @param string $message | ||
| 392 | * @param array $params | ||
| 393 | * @param bool $fail | ||
| 394 | * | ||
| 395 | * @return $this | ||
| 396 | */ | ||
| 397 | 	public function setReadingOutcome(string $message, array $params = [], bool $fail = false): self { | ||
| 398 | $this->readingOutcome = new SimpleDataStore( | ||
| 399 | [ | ||
| 400 | 'message' => $message, | ||
| 401 | 'params' => $params, | ||
| 402 | 'fail' => $fail | ||
| 403 | ] | ||
| 404 | ); | ||
| 405 | |||
| 406 | return $this; | ||
| 407 | } | ||
| 408 | |||
| 409 | /** | ||
| 410 | * @return SimpleDataStore | ||
| 411 | */ | ||
| 412 | 	public function getDataOutcome(): SimpleDataStore { | ||
| 413 | return $this->dataOutcome; | ||
| 414 | } | ||
| 415 | |||
| 416 | /** | ||
| 417 | * @param array $data | ||
| 418 | * | ||
| 419 | * @return $this | ||
| 420 | */ | ||
| 421 | 	public function setDataOutcome(array $data): self { | ||
| 422 | $this->dataOutcome = new SimpleDataStore($data); | ||
| 423 | |||
| 424 | return $this; | ||
| 425 | } | ||
| 426 | |||
| 427 | |||
| 428 | /** | ||
| 429 | * @return SimpleDataStore | ||
| 430 | */ | ||
| 431 | 	public function getResult(): SimpleDataStore { | ||
| 432 | return $this->result; | ||
| 433 | } | ||
| 434 | |||
| 435 | /** | ||
| 436 | * @param SimpleDataStore $result | ||
| 437 | * | ||
| 438 | * @return self | ||
| 439 | */ | ||
| 440 | 	public function setResult(SimpleDataStore $result): self { | ||
| 441 | $this->result = $result; | ||
| 442 | |||
| 443 | return $this; | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | /** | ||
| 448 | * @param array $data | ||
| 449 | * | ||
| 450 | * @return self | ||
| 451 | * @throws InvalidItemException | ||
| 452 | */ | ||
| 453 | 	public function import(array $data): self { | ||
| 454 | 		$this->setClass($this->get('class', $data)); | ||
| 455 | 		$this->setSeverity($this->getInt('severity', $data)); | ||
| 456 | 		$this->setData(new SimpleDataStore($this->getArray('data', $data))); | ||
| 457 | 		$this->setResult(new SimpleDataStore($this->getArray('result', $data))); | ||
| 458 | 		$this->setSource($this->get('source', $data)); | ||
| 459 | |||
| 460 | 		if (array_key_exists('circle', $data)) { | ||
| 461 | $circle = new Circle(); | ||
| 462 | 			$circle->import($this->getArray('circle', $data)); | ||
| 463 | $this->setCircle($circle); | ||
| 464 | } | ||
| 465 | |||
| 466 | 		if (array_key_exists('member', $data)) { | ||
| 467 | $member = new Member(); | ||
| 468 | 			$member->import($this->getArray('member', $data)); | ||
| 469 | $this->setMember($member); | ||
| 470 | } | ||
| 471 | |||
| 472 | return $this; | ||
| 473 | } | ||
| 474 | |||
| 475 | |||
| 476 | /** | ||
| 477 | * @return array | ||
| 478 | */ | ||
| 479 | 	function jsonSerialize(): array { | ||
| 480 | $arr = [ | ||
| 481 | 'class' => $this->getClass(), | ||
| 482 | 'severity' => $this->getSeverity(), | ||
| 483 | 'data' => $this->getData(), | ||
| 484 | 'result' => $this->getResult(), | ||
| 485 | 'source' => $this->getSource(), | ||
| 486 | 'outcome' => [ | ||
| 487 | 'message' => $this->getReadingOutcome(), | ||
| 488 | 'data' => $this->getDataOutcome() | ||
| 489 | ] | ||
| 490 | ]; | ||
| 491 | |||
| 492 | 		if ($this->hasCircle()) { | ||
| 493 | $arr['circle'] = $this->getCircle(); | ||
| 494 | } | ||
| 495 | 		if ($this->hasMember()) { | ||
| 496 | $arr['member'] = $this->getMember(); | ||
| 497 | } | ||
| 498 | |||
| 499 | return $arr; | ||
| 500 | } | ||
| 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.