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 | 2 | public function __construct($event, $id, $timestamp) |
|
| 130 | { |
||
| 131 | 2 | $this->event = $event; |
|
| 132 | 2 | $this->id = $id; |
|
| 133 | 2 | $this->timestamp = $timestamp; |
|
| 134 | 2 | $this->eventDate = new \DateTime(); |
|
| 135 | 2 | $this->eventDate->setTimestamp((int) $timestamp); |
|
| 136 | 2 | } |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $data |
||
| 140 | * |
||
| 141 | * @return Event |
||
| 142 | */ |
||
| 143 | 2 | public static function create(array $data) |
|
| 144 | { |
||
| 145 | 2 | $event = new self($data['event'], $data['id'], $data['timestamp']); |
|
| 146 | |||
| 147 | 2 | if (isset($data['tags'])) { |
|
| 148 | 2 | $event->setTags($data['tags']); |
|
| 149 | 2 | } |
|
| 150 | 2 | if (isset($data['envelope'])) { |
|
| 151 | 2 | $event->setEnvelope($data['envelope']); |
|
| 152 | 2 | } |
|
| 153 | 2 | if (isset($data['campaigns'])) { |
|
| 154 | 2 | $event->setCampaigns($data['campaigns']); |
|
| 155 | 2 | } |
|
| 156 | 2 | if (isset($data['user-variables'])) { |
|
| 157 | 2 | $event->setUserVariables($data['user-variables']); |
|
| 158 | 2 | } |
|
| 159 | 2 | if (isset($data['flags'])) { |
|
| 160 | 2 | $event->setFlags($data['flags']); |
|
| 161 | 2 | } |
|
| 162 | 2 | if (isset($data['routes'])) { |
|
| 163 | $event->setRoutes($data['routes']); |
||
| 164 | } |
||
| 165 | 2 | if (isset($data['message'])) { |
|
| 166 | 2 | $event->setMessage($data['message']); |
|
| 167 | 2 | } |
|
| 168 | 2 | if (isset($data['recipient'])) { |
|
| 169 | 2 | $event->setRecipient($data['recipient']); |
|
| 170 | 2 | } |
|
| 171 | 2 | if (isset($data['method'])) { |
|
| 172 | 2 | $event->setMethod($data['method']); |
|
| 173 | 2 | } |
|
| 174 | 2 | if (isset($data['delivery-status'])) { |
|
| 175 | $event->setDeliveryStatus($data['delivery-status']); |
||
| 176 | } |
||
| 177 | 2 | if (isset($data['severity'])) { |
|
| 178 | $event->setSeverity($data['severity']); |
||
| 179 | } |
||
| 180 | 2 | if (isset($data['reason'])) { |
|
| 181 | $event->setReason($data['reason']); |
||
| 182 | } |
||
| 183 | 2 | if (isset($data['geolocation'])) { |
|
| 184 | $event->setGeolocation($data['geolocation']); |
||
| 185 | } |
||
| 186 | 2 | if (isset($data['ip'])) { |
|
| 187 | $event->setIp($data['ip']); |
||
| 188 | } |
||
| 189 | 2 | if (isset($data['client-info'])) { |
|
| 190 | $event->setClientInfo($data['client-info']); |
||
| 191 | } |
||
| 192 | 2 | if (isset($data['url'])) { |
|
| 193 | $event->setUrl($data['url']); |
||
| 194 | } |
||
| 195 | 2 | if (isset($data['storage'])) { |
|
| 196 | $event->setStorage($data['storage']); |
||
| 197 | } |
||
| 198 | |||
| 199 | 2 | return $event; |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | 1 | public function getEvent() |
|
| 206 | { |
||
| 207 | 1 | return $this->event; |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 1 | public function getId() |
|
| 214 | { |
||
| 215 | 1 | return $this->id; |
|
| 216 | } |
||
| 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 | 2 | private function setTags($tags) |
|
| 246 | { |
||
| 247 | 2 | $this->tags = $tags; |
|
| 248 | 2 | } |
|
| 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 | 2 | private function setEnvelope($envelope) |
|
| 278 | { |
||
| 279 | 2 | $this->envelope = $envelope; |
|
| 280 | 2 | } |
|
| 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 | 2 | private function setCampaigns($campaigns) |
|
| 310 | { |
||
| 311 | 2 | $this->campaigns = $campaigns; |
|
| 312 | 2 | } |
|
| 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 | 2 | private function setUserVariables($userVariables) |
|
| 374 | { |
||
| 375 | 2 | $this->userVariables = $userVariables; |
|
| 376 | 2 | } |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | public function getFlags() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param array $flags |
||
| 388 | */ |
||
| 389 | 2 | private function setFlags($flags) |
|
| 390 | { |
||
| 391 | 2 | $this->flags = $flags; |
|
| 392 | 2 | } |
|
| 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 | 2 | private function setMessage($message) |
|
| 422 | { |
||
| 423 | 2 | $this->message = $message; |
|
| 424 | 2 | } |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getRecipient() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $recipient |
||
| 436 | */ |
||
| 437 | 2 | private function setRecipient($recipient) |
|
| 438 | { |
||
| 439 | 2 | $this->recipient = $recipient; |
|
| 440 | 2 | } |
|
| 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 | 2 | private function setMethod($method) |
|
| 486 | { |
||
| 487 | 2 | $this->method = $method; |
|
| 488 | 2 | } |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getSeverity() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $severity |
||
| 500 | */ |
||
| 501 | private function setSeverity($severity) |
||
| 505 | } |
||
| 506 |