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 |
||
25 | class MessageBuilder |
||
26 | { |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $message = []; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $variables = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $files = []; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $counters = [ |
||
46 | 'recipients' => [ |
||
47 | 'to' => 0, |
||
48 | 'cc' => 0, |
||
49 | 'bcc' => 0, |
||
50 | ], |
||
51 | 'attributes' => [ |
||
52 | 'attachment' => 0, |
||
53 | 'campaign_id' => 0, |
||
54 | 'custom_option' => 0, |
||
55 | 'tag' => 0, |
||
56 | ], |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @param array $params |
||
61 | * @param string $key |
||
62 | * @param mixed $default |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 21 | protected function safeGet($params, $key, $default) |
|
67 | { |
||
68 | 21 | if (array_key_exists($key, $params)) { |
|
69 | 17 | return $params[$key]; |
|
70 | } |
||
71 | |||
72 | 5 | return $default; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param array $params |
||
77 | * |
||
78 | * @return mixed|string |
||
79 | */ |
||
80 | 21 | protected function getFullName($params) |
|
81 | { |
||
82 | 21 | if (array_key_exists('first', $params)) { |
|
83 | 17 | $first = $this->safeGet($params, 'first', ''); |
|
84 | 17 | $last = $this->safeGet($params, 'last', ''); |
|
85 | |||
86 | 17 | return trim("$first $last"); |
|
87 | } |
||
88 | |||
89 | 5 | return $this->safeGet($params, 'full_name', ''); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $address |
||
94 | * @param array $variables |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 21 | protected function parseAddress($address, $variables) |
|
110 | |||
111 | /** |
||
112 | * @param string $headerName |
||
113 | * @param string $address |
||
114 | * @param array $variables |
||
115 | */ |
||
116 | 11 | protected function addRecipient($headerName, $address, $variables) |
|
117 | { |
||
118 | 11 | $compiledAddress = $this->parseAddress($address, $variables); |
|
119 | |||
120 | 11 | if ($headerName === 'h:reply-to') { |
|
121 | 1 | $this->message[$headerName] = $compiledAddress; |
|
122 | 11 | } elseif (isset($this->message[$headerName])) { |
|
123 | 1 | array_push($this->message[$headerName], $compiledAddress); |
|
124 | 1 | } else { |
|
125 | 10 | $this->message[$headerName] = [$compiledAddress]; |
|
126 | } |
||
127 | 11 | if (array_key_exists($headerName, $this->counters['recipients'])) { |
|
128 | 9 | $this->counters['recipients'][$headerName] += 1; |
|
129 | 9 | } |
|
130 | 11 | } |
|
131 | |||
132 | /** |
||
133 | * @param string $address |
||
134 | * @param array|null $variables |
||
135 | * |
||
136 | * @throws TooManyParameters |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | 13 | View Code Duplication | public function addToRecipient($address, $variables = null) |
152 | |||
153 | /** |
||
154 | * @param string $address |
||
155 | * @param array|null $variables |
||
156 | * |
||
157 | * @throws TooManyParameters |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | 4 | View Code Duplication | public function addCcRecipient($address, $variables = null) |
173 | |||
174 | /** |
||
175 | * @param string $address |
||
176 | * @param array|null $variables |
||
177 | * |
||
178 | * @throws TooManyParameters |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | 4 | View Code Duplication | public function addBccRecipient($address, $variables = null) |
194 | |||
195 | /** |
||
196 | * @param string $address |
||
197 | * @param array|null $variables |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | 7 | View Code Duplication | public function setFromAddress($address, $variables = null) |
209 | |||
210 | /** |
||
211 | * @param string $address |
||
212 | * @param array|null $variables |
||
213 | * |
||
214 | * @return mixed |
||
215 | */ |
||
216 | 1 | View Code Duplication | public function setReplyToAddress($address, $variables = null) |
224 | |||
225 | /** |
||
226 | * @param string $subject |
||
227 | * |
||
228 | * @return mixed |
||
229 | */ |
||
230 | 7 | public function setSubject($subject = '') |
|
239 | |||
240 | /** |
||
241 | * @param string $headerName |
||
242 | * @param mixed $headerData |
||
243 | * |
||
244 | * @return mixed |
||
245 | */ |
||
246 | 3 | public function addCustomHeader($headerName, $headerData) |
|
264 | |||
265 | /** |
||
266 | * @param string $textBody |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 7 | public function setTextBody($textBody) |
|
279 | |||
280 | /** |
||
281 | * @param string $htmlBody |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | 1 | public function setHtmlBody($htmlBody) |
|
294 | |||
295 | /** |
||
296 | * @param string $attachmentPath |
||
297 | * @param string|null $attachmentName |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | 3 | public function addAttachment($attachmentPath, $attachmentName = null) |
|
320 | |||
321 | /** |
||
322 | * @param string $inlineImagePath |
||
323 | * @param string|null $inlineImageName |
||
324 | * |
||
325 | * @throws InvalidParameter |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 4 | public function addInlineImage($inlineImagePath, $inlineImageName = null) |
|
342 | |||
343 | /** |
||
344 | * @param bool $testMode |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | 1 | View Code Duplication | public function setTestMode($testMode) |
359 | |||
360 | /** |
||
361 | * @param string|int $campaignId |
||
362 | * |
||
363 | * @throws TooManyParameters |
||
364 | * |
||
365 | * @return string|int |
||
366 | */ |
||
367 | 1 | View Code Duplication | public function addCampaignId($campaignId) |
382 | |||
383 | /** |
||
384 | * @param string $tag |
||
385 | * |
||
386 | * @throws TooManyParameters |
||
387 | */ |
||
388 | View Code Duplication | public function addTag($tag) |
|
403 | |||
404 | /** |
||
405 | * @param bool $enabled |
||
406 | * |
||
407 | * @return mixed |
||
408 | */ |
||
409 | 1 | View Code Duplication | public function setDkim($enabled) |
420 | |||
421 | /** |
||
422 | * @param bool $enabled |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | 1 | View Code Duplication | public function setOpenTracking($enabled) |
437 | |||
438 | /** |
||
439 | * @param bool $enabled |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 1 | public function setClickTracking($enabled) |
|
456 | |||
457 | /** |
||
458 | * @param string $timeDate |
||
459 | * @param string|null $timeZone |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | 1 | public function setDeliveryTime($timeDate, $timeZone = null) |
|
477 | |||
478 | /** |
||
479 | * @param string $customName |
||
480 | * @param mixed $data |
||
481 | */ |
||
482 | 1 | public function addCustomData($customName, $data) |
|
486 | |||
487 | /** |
||
488 | * @param string $parameterName |
||
489 | * @param mixed $data |
||
490 | * |
||
491 | * @return mixed |
||
492 | */ |
||
493 | 2 | public function addCustomParameter($parameterName, $data) |
|
505 | |||
506 | /** |
||
507 | * @param array $message |
||
508 | */ |
||
509 | 1 | public function setMessage($message) |
|
513 | |||
514 | /** |
||
515 | * @return array |
||
516 | */ |
||
517 | 31 | public function getMessage() |
|
521 | |||
522 | /** |
||
523 | * @return array |
||
524 | */ |
||
525 | 6 | public function getFiles() |
|
529 | } |
||
530 |
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.