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 | 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 | public function setup(array $options) |
||
| 176 | 9 | ||
| 177 | /** |
||
| 178 | * Handle the phpbu end event. |
||
| 179 | * |
||
| 180 | * @param \phpbu\App\Event\App\End $event |
||
| 181 | * @throws \phpbu\App\Exception |
||
| 182 | */ |
||
| 183 | public function onPhpbuEnd(Event\App\End $event) |
||
| 216 | 4 | ||
| 217 | /** |
||
| 218 | * Backup start event. |
||
| 219 | * |
||
| 220 | 4 | * @param \phpbu\App\Event\Backup\Start $event |
|
| 221 | */ |
||
| 222 | public function onBackupStart(Event\Backup\Start $event) |
||
| 226 | |||
| 227 | 4 | /** |
|
| 228 | * Check start event. |
||
| 229 | 4 | * |
|
| 230 | 4 | * @param \phpbu\App\Event\Check\Start $event |
|
| 231 | */ |
||
| 232 | public function onCheckStart(Event\Check\Start $event) |
||
| 236 | |||
| 237 | 1 | /** |
|
| 238 | * Crypt start event. |
||
| 239 | 1 | * |
|
| 240 | 1 | * @param \phpbu\App\Event\Crypt\Start $event |
|
| 241 | */ |
||
| 242 | public function onCryptStart(Event\Crypt\Start $event) |
||
| 246 | |||
| 247 | 1 | /** |
|
| 248 | * Sync start event. |
||
| 249 | 1 | * |
|
| 250 | 1 | * @param \phpbu\App\Event\Sync\Start $event |
|
| 251 | */ |
||
| 252 | public function onSyncStart(Event\Sync\Start $event) |
||
| 256 | |||
| 257 | 1 | /** |
|
| 258 | * Cleanup start event. |
||
| 259 | 1 | * |
|
| 260 | 1 | * @param \phpbu\App\Event\Cleanup\Start $event |
|
| 261 | */ |
||
| 262 | public function onCleanupStart(Event\Cleanup\Start $event) |
||
| 266 | |||
| 267 | 1 | /** |
|
| 268 | * Configure PHPMailer |
||
| 269 | 1 | * |
|
| 270 | 1 | * @param string $type |
|
| 271 | * @param array $options |
||
| 272 | * @throws \phpbu\App\Exception |
||
| 273 | */ |
||
| 274 | protected function setupMailer($type, array $options) |
||
| 295 | 4 | ||
| 296 | 4 | /** |
|
| 297 | * Should a mail be send |
||
| 298 | * |
||
| 299 | * @param \phpbu\App\Result $result |
||
| 300 | 1 | * @return bool |
|
| 301 | */ |
||
| 302 | 9 | protected function shouldMailBeSend(Result $result) : bool |
|
| 310 | |||
| 311 | 4 | /** |
|
| 312 | * Setup smtp mailing |
||
| 313 | * |
||
| 314 | * @param array $options |
||
| 315 | * @return void |
||
| 316 | * @throws \phpbu\App\Exception |
||
| 317 | 4 | */ |
|
| 318 | protected function setupSmtpMailer(array $options) |
||
| 342 | 1 | ||
| 343 | 1 | /** |
|
| 344 | * Setup the php mail transport |
||
| 345 | 1 | * |
|
| 346 | 1 | * @param array $options |
|
| 347 | * @return void |
||
| 348 | 1 | */ |
|
| 349 | protected function setupSendmailMailer(array $options) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return mail header html |
||
| 356 | * |
||
| 357 | 4 | * @return string |
|
| 358 | * @throws \phpbu\App\Exception |
||
| 359 | 4 | */ |
|
| 360 | 1 | protected function getHeaderHtml() |
|
| 365 | 3 | ||
| 366 | /** |
||
| 367 | * Return mail status html |
||
| 368 | * |
||
| 369 | * @param \phpbu\App\Result $result |
||
| 370 | * @return string |
||
| 371 | * @throws \phpbu\App\Exception |
||
| 372 | */ |
||
| 373 | 4 | protected function getStatusHtml(Result $result) |
|
| 411 | 4 | ||
| 412 | /** |
||
| 413 | 4 | * Get error information |
|
| 414 | 4 | * |
|
| 415 | 4 | * @param \phpbu\App\Result $result |
|
| 416 | 4 | * @return string |
|
| 417 | 4 | * @throws \phpbu\App\Exception |
|
| 418 | 4 | */ |
|
| 419 | 4 | protected function getErrorHtml(Result $result) |
|
| 442 | 1 | ||
| 443 | 1 | /** |
|
| 444 | 1 | * Return backup html information |
|
| 445 | * |
||
| 446 | 1 | * @param \phpbu\App\Result $result |
|
| 447 | * @return string |
||
| 448 | * @throws \phpbu\App\Exception |
||
| 449 | 1 | */ |
|
| 450 | protected function getInfoHtml(Result $result) |
||
| 534 | 3 | ||
| 535 | 3 | /** |
|
| 536 | 3 | * Return mail body footer |
|
| 537 | 3 | * |
|
| 538 | * @return string |
||
| 539 | * @throws \phpbu\App\Exception |
||
| 540 | 3 | */ |
|
| 541 | protected function getFooterHtml() |
||
| 546 | } |
||
| 547 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.