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:
| 1 | <?php |
||
| 26 | class Mailer |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $recipients = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $carbonCopies = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $blindCarbonCopies = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $from = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $replyTo = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $subject = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $content = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $isHtml = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $attachment = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $boundary = ''; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $name |
||
| 81 | */ |
||
| 82 | public function addTo(string $name) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $name |
||
| 89 | */ |
||
| 90 | public function addCc(string $name) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $name |
||
| 97 | */ |
||
| 98 | public function addBcc(string $name) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $name |
||
| 105 | */ |
||
| 106 | public function setFrom(string $name) |
||
| 110 | |||
| 111 | public function setReplyTo(string $name) |
||
| 112 | { |
||
| 113 | $this->replyTo = $name; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $subject |
||
| 118 | */ |
||
| 119 | public function setSubject(string $subject) |
||
| 123 | |||
| 124 | public function setContent(string $content) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $name |
||
| 131 | * @param string $path |
||
| 132 | * @param bool $inline |
||
| 133 | */ |
||
| 134 | public function addAttachment(string $name = '', string $path, bool $inline = false) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param bool $html |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public function send($html = true) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $headers |
||
| 157 | * @param string $message |
||
| 158 | * @return bool |
||
| 159 | * @codeCoverageIgnore |
||
| 160 | */ |
||
| 161 | protected function sendMail($headers, $message) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | protected function getBoundary() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | protected function getHeaders() |
||
| 184 | { |
||
| 185 | $boundary = $this->getBoundary(); |
||
| 186 | $headers = 'MIME-Version: 1.0' . PHP_EOL; |
||
| 187 | |||
| 188 | if (!empty($this->from)) { |
||
| 189 | $headers .= 'From: ' .$this->from . PHP_EOL; |
||
| 190 | } |
||
| 191 | |||
| 192 | if (!empty($this->replyTo)) { |
||
| 193 | $headers .= 'Reply-To: ' . $this->replyTo . PHP_EOL; |
||
| 194 | } else if (!empty($this->from)) { |
||
| 195 | $headers .= 'Reply-To: ' . $this->from . PHP_EOL; |
||
| 196 | } |
||
| 197 | |||
| 198 | View Code Duplication | if (!empty($this->carbonCopies)) { |
|
|
|
|||
| 199 | $headers .= 'CC: ' . implode(',', $this->carbonCopies) . PHP_EOL; |
||
| 200 | } |
||
| 201 | |||
| 202 | View Code Duplication | if (!empty($this->blindCarbonCopies)) { |
|
| 203 | $headers .= 'BCC: ' . implode(',', $this->blindCarbonCopies) . PHP_EOL; |
||
| 204 | } |
||
| 205 | |||
| 206 | $headers .= 'Content-Type: multipart/mixed; boundary = ' . $boundary . PHP_EOL; |
||
| 207 | |||
| 208 | return $headers; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | protected function getMessage() |
||
| 258 | |||
| 259 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.