Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MessageBuilder 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 MessageBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class MessageBuilder |
||
| 24 | { |
||
| 25 | const RECIPIENT_COUNT_LIMIT = 1000; |
||
| 26 | |||
| 27 | const CAMPAIGN_ID_LIMIT = 3; |
||
| 28 | |||
| 29 | const TAG_LIMIT = 3; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $message = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $variables = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $counters = [ |
||
| 45 | 'recipients' => [ |
||
| 46 | 'to' => 0, |
||
| 47 | 'cc' => 0, |
||
| 48 | 'bcc' => 0, |
||
| 49 | ], |
||
| 50 | 'attributes' => [ |
||
| 51 | 'attachment' => 0, |
||
| 52 | 'campaign_id' => 0, |
||
| 53 | 'custom_option' => 0, |
||
| 54 | 'tag' => 0, |
||
| 55 | ], |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param array $params |
||
| 60 | * @param string $key |
||
| 61 | * @param mixed $default |
||
| 62 | * |
||
| 63 | * @return mixed |
||
| 64 | */ |
||
| 65 | private function get($params, $key, $default) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $params { |
||
| 76 | * |
||
| 77 | * @var string $full_name |
||
| 78 | * @var string $first |
||
| 79 | * @var string $last |
||
| 80 | * } |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | private function getFullName(array $params) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $address |
||
| 95 | * @param array $params { |
||
|
|
|||
| 96 | * |
||
| 97 | * @var string $full_name |
||
| 98 | * @var string $first |
||
| 99 | * @var string $last |
||
| 100 | * } |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | protected function parseAddress($address, array $variables) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $headerName |
||
| 116 | * @param string $address |
||
| 117 | * @param array $variables { |
||
| 118 | * |
||
| 119 | * @var string $full_name |
||
| 120 | * @var string $first |
||
| 121 | * @var string $last |
||
| 122 | * } |
||
| 123 | */ |
||
| 124 | View Code Duplication | protected function addRecipient($headerName, $address, array $variables) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $address |
||
| 142 | * @param array $variables { |
||
| 143 | * |
||
| 144 | * @var string $id If used with BatchMessage |
||
| 145 | * @var string $full_name |
||
| 146 | * @var string $first |
||
| 147 | * @var string $last |
||
| 148 | * } |
||
| 149 | * |
||
| 150 | * @throws TooManyRecipients |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function addToRecipient($address, array $variables = []) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $address |
||
| 162 | * @param array $variables { |
||
| 163 | * |
||
| 164 | * @var string $id If used with BatchMessage |
||
| 165 | * @var string $full_name |
||
| 166 | * @var string $first |
||
| 167 | * @var string $last |
||
| 168 | * } |
||
| 169 | * |
||
| 170 | * @throws TooManyRecipients |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function addCcRecipient($address, array $variables = []) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $address |
||
| 183 | * @param array $variables { |
||
| 184 | * |
||
| 185 | * @var string $id If used with BatchMessage |
||
| 186 | * @var string $full_name |
||
| 187 | * @var string $first |
||
| 188 | * @var string $last |
||
| 189 | * } |
||
| 190 | * |
||
| 191 | * @throws TooManyRecipients |
||
| 192 | */ |
||
| 193 | public function addBccRecipient($address, array $variables) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $address |
||
| 204 | * @param array $variables { |
||
| 205 | * |
||
| 206 | * @var string $id If used with BatchMessage |
||
| 207 | * @var string $full_name |
||
| 208 | * @var string $first |
||
| 209 | * @var string $last |
||
| 210 | * } |
||
| 211 | */ |
||
| 212 | public function setFromAddress($address, array $variables = []) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $address |
||
| 219 | * @param array $variables { |
||
| 220 | * |
||
| 221 | * @var string $id If used with BatchMessage |
||
| 222 | * @var string $full_name |
||
| 223 | * @var string $first |
||
| 224 | * @var string $last |
||
| 225 | * } |
||
| 226 | */ |
||
| 227 | public function setReplyToAddress($address, array $variables = []) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $subject |
||
| 234 | */ |
||
| 235 | public function setSubject($subject) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $headerName |
||
| 242 | * @param mixed $headerData |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function addCustomHeader($headerName, $headerData) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $textBody |
||
| 263 | */ |
||
| 264 | public function setTextBody($textBody) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $htmlBody |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function setHtmlBody($htmlBody) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $attachmentPath |
||
| 281 | * @param string|null $attachmentName |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function addAttachment($attachmentPath, $attachmentName = null) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $inlineImagePath |
||
| 297 | * @param string|null $inlineImageName |
||
| 298 | */ |
||
| 299 | View Code Duplication | public function addInlineImage($inlineImagePath, $inlineImageName = null) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param bool $enabled |
||
| 313 | */ |
||
| 314 | public function setTestMode($enabled) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $campaignId |
||
| 321 | * |
||
| 322 | * @throws LimitExceeded |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function addCampaignId($campaignId) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $tag |
||
| 339 | * |
||
| 340 | * @throws LimitExceeded |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function addTag($tag) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @param bool $enabled |
||
| 358 | */ |
||
| 359 | public function setDkim($enabled) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param bool $enabled |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function setOpenTracking($enabled) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param bool $enabled |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function setClickTracking($enabled) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $timeDate |
||
| 386 | * @param string|null $timeZone |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | View Code Duplication | public function setDeliveryTime($timeDate, $timeZone = null) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $customName |
||
| 407 | * @param mixed $data |
||
| 408 | */ |
||
| 409 | public function addCustomData($customName, $data) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $parameterName |
||
| 416 | * @param mixed $data |
||
| 417 | * |
||
| 418 | * @return mixed |
||
| 419 | */ |
||
| 420 | View Code Duplication | public function addCustomParameter($parameterName, $data) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @param array $message |
||
| 433 | */ |
||
| 434 | public function setMessage($message) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public function getMessage() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param $enabled |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | private function boolToString($enabled) |
||
| 464 | } |
||
| 465 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.