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 XoopsMailer 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 XoopsMailer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class XoopsMailer |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * reference to a {@link XoopsMultiMailer} |
||
| 40 | * |
||
| 41 | * @var XoopsMultiMailer |
||
| 42 | * @access protected |
||
| 43 | * @since 21.02.2003 14:14:13 |
||
| 44 | */ |
||
| 45 | protected $multimailer; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * sender email address |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $fromEmail; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * sender name |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $fromName; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * sender UID |
||
| 63 | * |
||
| 64 | * @var XoopsUser |
||
| 65 | */ |
||
| 66 | private $fromUser; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * array of user class objects |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $toUsers; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * array of email addresses |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private $toEmails; |
||
| 81 | |||
| 82 | // private |
||
| 83 | /** |
||
| 84 | * custom headers |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private $headers; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * subjet of mail |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private $subject; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * body of mail |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $body; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * error messages |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private $errors; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * messages upon success |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | private $success; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var bool |
||
| 120 | */ |
||
| 121 | private $isMail; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | private $isPM; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | private $assignedTags; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | private $template; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | private $templatedir; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | protected $charSet = 'iso-8859-1'; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $encoding = '8bit'; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var string |
||
| 155 | */ |
||
| 156 | private $priority; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var string |
||
| 160 | */ |
||
| 161 | private $LE; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Constructor |
||
| 165 | * |
||
| 166 | * @return XoopsMailer |
||
| 167 | */ |
||
| 168 | 27 | public function __construct() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * reset all properties to default |
||
| 176 | * |
||
| 177 | * @param bool $value |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | 1 | public function setHTML($value = true) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * reset all properties to default |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | 27 | public function reset() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $value |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | 1 | public function setTemplateDir($value = null) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return bool|string |
||
| 229 | */ |
||
| 230 | private function getTemplatePath() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $value |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | 1 | public function setTemplate($value) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $value |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | 1 | public function setFromEmail($value) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $value |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | 1 | public function setFromName($value) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param XoopsUser $user |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | 1 | public function setFromUser(XoopsUser $user) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $value |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | 1 | public function setPriority($value) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $value |
||
| 298 | * @return void |
||
| 299 | */ |
||
| 300 | 1 | public function setSubject($value) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $value |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 1 | public function setBody($value) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | 1 | public function useMail() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | 1 | public function usePM() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param bool $debug |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function send($debug = false) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param int $uid |
||
| 440 | * @param string $subject |
||
| 441 | * @param string $body |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | private function sendPM($uid, $subject, $body) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Send email |
||
| 464 | * |
||
| 465 | * Uses the new XoopsMultiMailer |
||
| 466 | * |
||
| 467 | * @param string $email |
||
| 468 | * @param string $subject |
||
| 469 | * @param string $body |
||
| 470 | * @param array $headers |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | private function sendMail($email, $subject, $body, $headers) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param bool $ashtml |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | 1 | public function getErrors($ashtml = true) |
|
| 520 | |||
| 521 | // public |
||
| 522 | 1 | function getSuccess($ashtml = true) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * @param string|array $tag |
||
| 539 | * @param null $value |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | 1 | public function assign($tag, $value = null) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * @param string $value |
||
| 562 | * @return void |
||
| 563 | */ |
||
| 564 | 1 | public function addHeaders($value) |
|
| 568 | |||
| 569 | /** |
||
| 570 | * @param $email |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | 1 | public function setToEmails($email) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * @param XoopsUser|array $user |
||
| 589 | * @return void |
||
| 590 | */ |
||
| 591 | 2 | public function setToUsers($users) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * @param XoopsGroup $group |
||
| 604 | * @return void |
||
| 605 | */ |
||
| 606 | 1 | public function setToGroups($groups) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * abstract, to be overridden by lang specific mail class, if needed |
||
| 622 | * |
||
| 623 | * @param $text |
||
| 624 | * @return |
||
| 625 | */ |
||
| 626 | 1 | public function encodeFromName($text) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * abstract, to be overridden by lang specific mail class, if needed |
||
| 633 | * |
||
| 634 | * @param string $text |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | 1 | public function encodeSubject($text) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * abstract, to be overridden by lang specific mail class, if needed |
||
| 644 | * |
||
| 645 | * @param string $text |
||
| 646 | * @return void |
||
| 647 | */ |
||
| 648 | 1 | public function encodeBody(&$text) |
|
| 651 | } |
||
| 652 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: