Complex classes like Mail 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 Mail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Mail implements Listener, Logger |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Mailer instance |
||
| 30 | * |
||
| 31 | * @var Swift_Mailer |
||
| 32 | */ |
||
| 33 | protected $mailer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Mail subject |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $subject; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * From email address |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $senderMail; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * From name |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $senderName; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Transport type [mail|smtp|null] |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $transportType; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * List of mail recipients |
||
| 65 | * |
||
| 66 | * @var array<string> |
||
| 67 | */ |
||
| 68 | protected $recipients = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Amount of executed backups |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | private $numBackups = 0; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Amount of executed checks |
||
| 79 | * |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | private $numChecks = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Amount of executed Syncs |
||
| 86 | * |
||
| 87 | * @var integer |
||
| 88 | */ |
||
| 89 | private $numSyncs = 0; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Amount of executed Crypts |
||
| 93 | * |
||
| 94 | * @var integer |
||
| 95 | */ |
||
| 96 | private $numCrypts = 0; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Amount of executed Cleanups |
||
| 100 | * |
||
| 101 | * @var integer |
||
| 102 | */ |
||
| 103 | private $numCleanups = 0; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Send mail only if there was an error |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | private $sendOnlyOnError = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Send mails on simulation runs. |
||
| 114 | * |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | private $sendSimulating = true; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Is current execution a simulation. |
||
| 121 | * |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | private $isSimulation = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns an array of event names this subscriber wants to listen to. |
||
| 128 | * |
||
| 129 | * The array keys are event names and the value can be: |
||
| 130 | * |
||
| 131 | * * The method name to call (priority defaults to 0) |
||
| 132 | * * An array composed of the method name to call and the priority |
||
| 133 | * * An array of arrays composed of the method names to call and respective |
||
| 134 | * priorities, or 0 if unset |
||
| 135 | * |
||
| 136 | * @return array The event names to listen to |
||
| 137 | */ |
||
| 138 | 1 | public static function getSubscribedEvents() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Setup the Logger. |
||
| 152 | * |
||
| 153 | * @see \phpbu\App\Log\Logger::setup |
||
| 154 | * @param array $options |
||
| 155 | * @throws \phpbu\App\Exception |
||
| 156 | */ |
||
| 157 | 12 | public function setup(array $options) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Handle the phpbu end event. |
||
| 180 | * |
||
| 181 | * @param \phpbu\App\Event\App\End $event |
||
| 182 | * @throws \phpbu\App\Exception |
||
| 183 | */ |
||
| 184 | 4 | public function onPhpbuEnd(Event\App\End $event) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Backup start event. |
||
| 224 | * |
||
| 225 | * @param \phpbu\App\Event\Backup\Start $event |
||
| 226 | */ |
||
| 227 | 4 | public function onBackupStart(Event\Backup\Start $event) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Check start event. |
||
| 234 | * |
||
| 235 | * @param \phpbu\App\Event\Check\Start $event |
||
| 236 | */ |
||
| 237 | 1 | public function onCheckStart(Event\Check\Start $event) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Crypt start event. |
||
| 244 | * |
||
| 245 | * @param \phpbu\App\Event\Crypt\Start $event |
||
| 246 | */ |
||
| 247 | 1 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Sync start event. |
||
| 254 | * |
||
| 255 | * @param \phpbu\App\Event\Sync\Start $event |
||
| 256 | */ |
||
| 257 | 1 | public function onSyncStart(Event\Sync\Start $event) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Cleanup start event. |
||
| 264 | * |
||
| 265 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 266 | */ |
||
| 267 | 1 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Create a Swift_Mailer_Transport. |
||
| 274 | * |
||
| 275 | * @param string $type |
||
| 276 | * @param array $options |
||
| 277 | * @throws \phpbu\App\Exception |
||
| 278 | * @return \Swift_Transport |
||
| 279 | */ |
||
| 280 | 11 | protected function createTransport($type, array $options) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Should a mail be send. |
||
| 307 | * |
||
| 308 | * @param \phpbu\App\Result $result |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 4 | protected function shouldMailBeSend(Result $result) : bool |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Create Swift Smtp Transport. |
||
| 322 | * |
||
| 323 | * @param array $options |
||
| 324 | * @return \Swift_SmtpTransport |
||
| 325 | * @throws \phpbu\App\Exception |
||
| 326 | */ |
||
| 327 | 2 | protected function getSmtpTransport(array $options) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Create a Swift Sendmail Transport. |
||
| 353 | * |
||
| 354 | * @param array $options |
||
| 355 | * @return \Swift_SendmailTransport |
||
| 356 | */ |
||
| 357 | 4 | protected function getSendmailTransport(array $options) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Return mail header html |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | 4 | protected function getHeaderHtml() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Return mail status html |
||
| 381 | * |
||
| 382 | * @param \phpbu\App\Result $result |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 4 | protected function getStatusHtml(Result $result) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Get error information. |
||
| 426 | * |
||
| 427 | * @param \phpbu\App\Result $result |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 4 | protected function getErrorHtml(Result $result) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Return backup html information. |
||
| 456 | * |
||
| 457 | * @param \phpbu\App\Result $result |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 4 | protected function getInfoHtml(Result $result) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Return mail body footer. |
||
| 547 | * |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | 4 | protected function getFooterHtml() |
|
| 555 | } |
||
| 556 |