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 Envelope 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 Envelope, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Envelope { |
||
| 16 | |||
| 17 | protected $uid, |
||
|
|
|||
| 18 | $to, |
||
| 19 | $from, |
||
| 20 | $cc, |
||
| 21 | $bcc, |
||
| 22 | $replyTo, |
||
| 23 | $subject, |
||
| 24 | $message, |
||
| 25 | $contentType = 'text/html; charset="utf-8"', |
||
| 26 | $attachments, |
||
| 27 | $compiled_head, |
||
| 28 | $compiled_body; |
||
| 29 | |||
| 30 | public function __construct($email=null){ |
||
| 45 | |||
| 46 | protected function add_emails(&$pool, $emails, $append=true){ |
||
| 63 | |||
| 64 | public function from($value=null){ |
||
| 70 | |||
| 71 | public function to($value=null){ |
||
| 77 | |||
| 78 | public function cc($value=null){ |
||
| 84 | |||
| 85 | public function bcc($value=null){ |
||
| 91 | |||
| 92 | public function replyTo($value=null){ |
||
| 98 | |||
| 99 | public function subject($value=null){ |
||
| 106 | |||
| 107 | public function contentType($value=null){ |
||
| 115 | |||
| 116 | public function message($value=null){ |
||
| 123 | |||
| 124 | public function attach($file){ |
||
| 132 | |||
| 133 | public function attachments($file=null){ |
||
| 134 | if ($file!==null && $file) $this->attach($file); |
||
| 135 | return $this->attachments ?: []; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function head($recompile = false){ |
||
| 139 | if ($recompile || (null === $this->compiled_head)){ |
||
| 140 | $head = []; |
||
| 141 | $head[] = "Subject: {$this->subject}"; |
||
| 142 | if($this->from) $head[] = "From: {$this->from}"; |
||
| 143 | if(is_array($this->to) && !empty($this->to)) $head[] = "To: " . implode(', ',$this->to); |
||
| 144 | View Code Duplication | if(is_array($this->cc) && !empty($this->cc)) $head[] = "Cc: " . implode(', ',$this->cc); |
|
| 145 | View Code Duplication | if(is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
| 146 | if($this->replyTo) $head[] = "Reply-To: {$this->replyTo}"; |
||
| 147 | $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->uid}\""; |
||
| 148 | $head[] = 'MIME-Version: 1.0'; |
||
| 149 | $head[] = ''; |
||
| 150 | $this->compiled_head = implode("\r\n", $head); |
||
| 151 | } |
||
| 152 | return \Filter::with( 'core.email.source.head', $this->compiled_head); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function body($recompile = false){ |
||
| 156 | if ($recompile || (null === $this->compiled_body)){ |
||
| 157 | $body = []; |
||
| 158 | $body[] = "--{$this->uid}"; |
||
| 159 | $body[] = "Content-Type: {$this->contentType}"; |
||
| 160 | $body[] = "Content-Transfer-Encoding: quoted-printable"; |
||
| 161 | $body[] = ''; |
||
| 162 | $body[] = quoted_printable_encode($this->message); |
||
| 163 | $body[] = ''; |
||
| 164 | |||
| 165 | if (!empty($this->attachments)) foreach ((array)$this->attachments as $file) { |
||
| 166 | |||
| 167 | if (is_string($file)) { |
||
| 168 | $name = basename($file); |
||
| 169 | $data = file_get_contents($file); |
||
| 170 | } else { |
||
| 171 | $name = isset($file['name']) ? $file['name'] : 'untitled'; |
||
| 172 | $data = isset($file['content']) ? $file['content'] : ''; |
||
| 173 | } |
||
| 174 | |||
| 175 | $body[] = "--{$this->uid}"; |
||
| 176 | $body[] = "Content-Type: application/octet-stream; name=\"{$name}\""; |
||
| 177 | $body[] = "Content-Transfer-Encoding: base64"; |
||
| 178 | $body[] = "Content-Disposition: attachment; filename=\"{$name}\""; |
||
| 179 | $body[] = ''; |
||
| 180 | $body[] = chunk_split(base64_encode($data)); |
||
| 181 | $body[] = ''; |
||
| 182 | } |
||
| 183 | |||
| 184 | $body[] = "--{$this->uid}"; |
||
| 185 | |||
| 186 | $this->compiled_body = implode("\r\n", $body); |
||
| 187 | } |
||
| 188 | return \Filter::with( 'core.email.source.body', $this->compiled_body); |
||
| 189 | } |
||
| 190 | |||
| 191 | public function build(){ |
||
| 194 | |||
| 195 | } |
||
| 196 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.