Complex classes like Event 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 Event, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Event |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string status |
||
| 19 | */ |
||
| 20 | private $event; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $id; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var float |
||
| 29 | */ |
||
| 30 | private $timestamp; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * A \DateTime representation of $timestamp. |
||
| 34 | * |
||
| 35 | * @var \DateTime |
||
| 36 | */ |
||
| 37 | private $eventDate; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array|string[] |
||
| 41 | */ |
||
| 42 | private $tags = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $url; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $severity; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $envelope = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $deliveryStatus; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array|string[] |
||
| 66 | */ |
||
| 67 | private $campaigns = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $ip; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $clientInfo = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $reason; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private $userVariables = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array key=>bool |
||
| 91 | */ |
||
| 92 | private $flags = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array multi dimensions |
||
| 96 | */ |
||
| 97 | private $routes = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array multi dimensions |
||
| 101 | */ |
||
| 102 | private $message = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $recipient; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $geolocation = []; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | private $storage = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | private $method; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $event |
||
| 126 | * @param string $id |
||
| 127 | * @param float $timestamp |
||
| 128 | */ |
||
| 129 | public function __construct($event, $id, $timestamp) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $data |
||
| 140 | * |
||
| 141 | * @return Event |
||
| 142 | */ |
||
| 143 | public static function create(array $data) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function getEvent() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function getId() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return float |
||
| 220 | */ |
||
| 221 | public function getTimestamp() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return \DateTime |
||
| 228 | */ |
||
| 229 | public function getEventDate() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return array|\string[] |
||
| 236 | */ |
||
| 237 | public function getTags() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param array|\string[] $tags |
||
| 244 | */ |
||
| 245 | private function setTags($tags) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getUrl() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $url |
||
| 260 | */ |
||
| 261 | private function setUrl($url) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function getEnvelope() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $envelope |
||
| 276 | */ |
||
| 277 | private function setEnvelope($envelope) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getDeliveryStatus() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $deliveryStatus |
||
| 292 | */ |
||
| 293 | private function setDeliveryStatus($deliveryStatus) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return array|\string[] |
||
| 300 | */ |
||
| 301 | public function getCampaigns() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array|\string[] $campaigns |
||
| 308 | */ |
||
| 309 | private function setCampaigns($campaigns) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getIp() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $ip |
||
| 324 | */ |
||
| 325 | private function setIp($ip) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | public function getClientInfo() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param array $clientInfo |
||
| 340 | */ |
||
| 341 | private function setClientInfo($clientInfo) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function getReason() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $reason |
||
| 356 | */ |
||
| 357 | private function setReason($reason) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function getUserVariables() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param array $userVariables |
||
| 372 | */ |
||
| 373 | private function setUserVariables($userVariables) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | public function getFlags() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param array $flags |
||
| 388 | */ |
||
| 389 | private function setFlags($flags) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function getRoutes() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param array $routes |
||
| 404 | */ |
||
| 405 | private function setRoutes($routes) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | public function getMessage() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param array $message |
||
| 420 | */ |
||
| 421 | private function setMessage($message) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getRecipient() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $recipient |
||
| 436 | */ |
||
| 437 | private function setRecipient($recipient) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | public function getGeolocation() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param array $geolocation |
||
| 452 | */ |
||
| 453 | private function setGeolocation($geolocation) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | public function getStorage() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param array $storage |
||
| 468 | */ |
||
| 469 | private function setStorage($storage) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getMethod() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $method |
||
| 484 | */ |
||
| 485 | private function setMethod($method) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getSeverity() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $severity |
||
| 500 | */ |
||
| 501 | private function setSeverity($severity) |
||
| 505 | } |
||
| 506 |