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 |
||
| 25 | class Mail implements Listener, Logger |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Mailer instance |
||
| 29 | * |
||
| 30 | * @var \PHPMailer\PHPMailer\PHPMailer; |
||
| 31 | */ |
||
| 32 | protected $mailer; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Mail subject |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $subject; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * From email address |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $senderMail; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * From name |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $senderName; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Transport type [mail|smtp|null] |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $transportType; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * List of mail recipients |
||
| 64 | * |
||
| 65 | * @var array<string> |
||
| 66 | */ |
||
| 67 | protected $recipients = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Amount of executed backups |
||
| 71 | * |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | private $numBackups = 0; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Amount of executed checks |
||
| 78 | * |
||
| 79 | * @var integer |
||
| 80 | */ |
||
| 81 | private $numChecks = 0; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Amount of executed Syncs |
||
| 85 | * |
||
| 86 | * @var integer |
||
| 87 | */ |
||
| 88 | private $numSyncs = 0; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Amount of executed Crypts |
||
| 92 | * |
||
| 93 | * @var integer |
||
| 94 | */ |
||
| 95 | private $numCrypts = 0; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Amount of executed Cleanups |
||
| 99 | * |
||
| 100 | * @var integer |
||
| 101 | */ |
||
| 102 | private $numCleanups = 0; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Send mail only if there was an error |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | private $sendOnlyOnError = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Send mails on simulation runs. |
||
| 113 | * |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | private $sendSimulating = true; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Is current execution a simulation. |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | private $isSimulation = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns an array of event names this subscriber wants to listen to. |
||
| 127 | * |
||
| 128 | * The array keys are event names and the value can be: |
||
| 129 | * |
||
| 130 | * * The method name to call (priority defaults to 0) |
||
| 131 | * * An array composed of the method name to call and the priority |
||
| 132 | * * An array of arrays composed of the method names to call and respective |
||
| 133 | * priorities, or 0 if unset |
||
| 134 | * |
||
| 135 | * @return array The event names to listen to |
||
| 136 | */ |
||
| 137 | 1 | public static function getSubscribedEvents() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Setup the Logger. |
||
| 151 | * |
||
| 152 | * @see \phpbu\App\Log\Logger::setup |
||
| 153 | * @param array $options |
||
| 154 | * @throws \phpbu\App\Exception |
||
| 155 | */ |
||
| 156 | 12 | public function setup(array $options) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Handle the phpbu end event. |
||
| 179 | * |
||
| 180 | * @param \phpbu\App\Event\App\End $event |
||
| 181 | * @throws \phpbu\App\Exception |
||
| 182 | */ |
||
| 183 | 4 | public function onPhpbuEnd(Event\App\End $event) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Backup start event. |
||
| 218 | * |
||
| 219 | * @param \phpbu\App\Event\Backup\Start $event |
||
| 220 | */ |
||
| 221 | 4 | public function onBackupStart(Event\Backup\Start $event) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Check start event. |
||
| 228 | * |
||
| 229 | * @param \phpbu\App\Event\Check\Start $event |
||
| 230 | */ |
||
| 231 | 1 | public function onCheckStart(Event\Check\Start $event) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Crypt start event. |
||
| 238 | * |
||
| 239 | * @param \phpbu\App\Event\Crypt\Start $event |
||
| 240 | */ |
||
| 241 | 1 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Sync start event. |
||
| 248 | * |
||
| 249 | * @param \phpbu\App\Event\Sync\Start $event |
||
| 250 | */ |
||
| 251 | 1 | public function onSyncStart(Event\Sync\Start $event) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Cleanup start event. |
||
| 258 | * |
||
| 259 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 260 | */ |
||
| 261 | 1 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Configure PHPMailer |
||
| 268 | * |
||
| 269 | * @param string $type |
||
| 270 | * @param array $options |
||
| 271 | * @throws \phpbu\App\Exception |
||
| 272 | */ |
||
| 273 | 11 | protected function setupMailer($type, array $options) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Should a mail be send |
||
| 297 | * |
||
| 298 | * @param \phpbu\App\Result $result |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 4 | protected function shouldMailBeSend(Result $result) : bool |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Setup smtp mailing |
||
| 312 | * |
||
| 313 | * @param array $options |
||
| 314 | * @return void |
||
| 315 | * @throws \phpbu\App\Exception |
||
| 316 | */ |
||
| 317 | 2 | protected function setupSmtpMailer(array $options) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Setup the php mail transport |
||
| 344 | * |
||
| 345 | * @param array $options |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | 4 | protected function setupSendmailMailer(array $options) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Send the email |
||
| 355 | * |
||
| 356 | * @throws \phpbu\App\Exception |
||
| 357 | */ |
||
| 358 | protected function sendMail() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return mail header html |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | * @throws \phpbu\App\Exception |
||
| 374 | */ |
||
| 375 | 4 | protected function getHeaderHtml() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Return mail status html |
||
| 383 | * |
||
| 384 | * @param \phpbu\App\Result $result |
||
| 385 | * @return string |
||
| 386 | * @throws \phpbu\App\Exception |
||
| 387 | */ |
||
| 388 | 4 | protected function getStatusHtml(Result $result) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get error information |
||
| 429 | * |
||
| 430 | * @param \phpbu\App\Result $result |
||
| 431 | * @return string |
||
| 432 | * @throws \phpbu\App\Exception |
||
| 433 | */ |
||
| 434 | 4 | protected function getErrorHtml(Result $result) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Return backup html information |
||
| 460 | * |
||
| 461 | * @param \phpbu\App\Result $result |
||
| 462 | * @return string |
||
| 463 | * @throws \phpbu\App\Exception |
||
| 464 | */ |
||
| 465 | 4 | protected function getInfoHtml(Result $result) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Return mail body footer |
||
| 552 | * |
||
| 553 | * @return string |
||
| 554 | * @throws \phpbu\App\Exception |
||
| 555 | */ |
||
| 556 | 4 | protected function getFooterHtml() |
|
| 561 | } |
||
| 562 |