Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Message extends BaseMessage |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string|array from |
||
| 41 | */ |
||
| 42 | protected $from; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $to = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string|array reply to |
||
| 51 | */ |
||
| 52 | protected $replyTo; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $cc = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $bcc = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $subject; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $textBody; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $htmlBody; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $attachments = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string temporary attachment directory |
||
| 86 | */ |
||
| 87 | protected $attachmentsTmdDir; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $uniqueArguments = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $headers = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $templateId; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $templateModel; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array substitution pairs used to mark expandable vars in template mode https://github.com/sendgrid/sendgrid-php#setsubstitutions |
||
| 111 | */ |
||
| 112 | public $substitutionsPairs = ['{', '}']; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @inheritdoc |
||
| 116 | */ |
||
| 117 | 1 | public function getCharset() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | 1 | public function setCharset($charset) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | */ |
||
| 133 | 1 | public function getFrom() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return string|null extract and return the name associated with from |
||
| 148 | * @since XXX |
||
| 149 | */ |
||
| 150 | 1 | public function getFromName() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | 3 | public function setFrom($from) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @inheritdoc |
||
| 175 | */ |
||
| 176 | 3 | public function getTo() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @inheritdoc |
||
| 183 | */ |
||
| 184 | 3 | public function setTo($to) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @inheritdoc |
||
| 192 | */ |
||
| 193 | 1 | public function getReplyTo() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @inheritdoc |
||
| 210 | */ |
||
| 211 | 1 | public function setReplyTo($replyTo) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritdoc |
||
| 222 | */ |
||
| 223 | 1 | public function getCc() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | 1 | public function setCc($cc) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @inheritdoc |
||
| 239 | */ |
||
| 240 | 1 | public function getBcc() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @inheritdoc |
||
| 247 | */ |
||
| 248 | 1 | public function setBcc($bcc) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | 3 | public function getSubject() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritdoc |
||
| 264 | */ |
||
| 265 | 2 | public function setSubject($subject) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @return string|null text body of the message |
||
| 273 | * @since XXX |
||
| 274 | */ |
||
| 275 | 1 | public function getTextBody() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @inheritdoc |
||
| 282 | */ |
||
| 283 | 2 | public function setTextBody($text) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @return string|null html body of the message |
||
| 291 | * @since XXX |
||
| 292 | */ |
||
| 293 | 1 | public function getHtmlBody() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @inheritdoc |
||
| 300 | */ |
||
| 301 | 1 | public function setHtmlBody($html) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @return array list of unique arguments attached to the email |
||
| 309 | * @since XXX |
||
| 310 | */ |
||
| 311 | public function getUniqueArguments() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $key key of the unique argument |
||
| 318 | * @param string $value value of the unique argument which will be added to the mail |
||
| 319 | * @return $this |
||
| 320 | * @since XXX |
||
| 321 | */ |
||
| 322 | public function addUniqueArgument($key, $value) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $templateId template Id used. in this case, Subject / HtmlBody / TextBody are discarded |
||
| 330 | * @return $this |
||
| 331 | * @since XXX |
||
| 332 | */ |
||
| 333 | 2 | public function setTemplateId($templateId) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @return string|null current templateId |
||
| 341 | * @since XXX |
||
| 342 | */ |
||
| 343 | 1 | public function getTemplateId() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param array $templateModel model associated with the template |
||
| 350 | * @return $this |
||
| 351 | * @since XXX |
||
| 352 | */ |
||
| 353 | 2 | public function setTemplateModel($templateModel) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @return array current template model |
||
| 361 | * @since XXX |
||
| 362 | */ |
||
| 363 | 1 | public function getTemplateModel() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param array $header add custom header to the mail |
||
| 378 | * @since XXX |
||
| 379 | */ |
||
| 380 | 1 | public function addHeader($header) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @return array|null headers which should be added to the mail |
||
| 387 | * @since XXX |
||
| 388 | */ |
||
| 389 | 1 | public function getHeaders() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @return array|null list of attachments |
||
| 396 | * @since XXX |
||
| 397 | */ |
||
| 398 | 1 | public function getAttachments() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @inheritdoc |
||
| 405 | */ |
||
| 406 | 1 | public function attach($fileName, array $options = []) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @return string temporary directory to store contents |
||
| 422 | * @since XXX |
||
| 423 | * @throws InvalidConfigException |
||
| 424 | */ |
||
| 425 | 1 | protected function getTempDir() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @inheritdoc |
||
| 443 | */ |
||
| 444 | 2 | public function attachContent($content, array $options = []) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @inheritdoc |
||
| 459 | */ |
||
| 460 | 1 | public function embed($fileName, array $options = []) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * @inheritdoc |
||
| 477 | */ |
||
| 478 | 1 | public function embedContent($content, array $options = []) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @inheritdoc |
||
| 493 | * @todo make real serialization to make message compliant with PostmarkAPI |
||
| 494 | */ |
||
| 495 | public function toString() |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * @param array|string $emailsData email can be defined as string. In this case no transformation is done |
||
| 503 | * or as an array ['[email protected]', '[email protected]' => 'Email 2'] |
||
| 504 | * @return string|null |
||
| 505 | * @since XXX |
||
| 506 | */ |
||
| 507 | public static function stringifyEmails($emailsData) |
||
| 529 | |||
| 530 | 3 | public static function normalizeEmails($emailsData) |
|
| 548 | |||
| 549 | 2 | public function send(MailerInterface $mailer = null) |
|
| 557 | |||
| 558 | |||
| 559 | } |
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.