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 namespace Camelcased\Postmark\Inbound; |
||
5 | class Email { |
||
6 | /** |
||
7 | * @var array |
||
8 | */ |
||
9 | protected $data; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $attachments = []; |
||
15 | |||
16 | public function __construct(array $data) |
||
27 | |||
28 | /** |
||
29 | * Maps undefined functions to the correct field in the email |
||
30 | * |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function __call($name, $arguments) |
||
37 | |||
38 | /** |
||
39 | * Check for if the email has any attachments |
||
40 | * |
||
41 | * @return boolean |
||
42 | */ |
||
43 | public function hasAttachments() |
||
51 | |||
52 | /** |
||
53 | * Is the body in the email plain text? |
||
54 | * |
||
55 | * @return boolean |
||
56 | */ |
||
57 | View Code Duplication | public function bodyIsText() |
|
66 | |||
67 | /** |
||
68 | * Is the body in the email HTML? |
||
69 | * |
||
70 | * @return boolean |
||
71 | */ |
||
72 | View Code Duplication | public function bodyIsHtml() |
|
81 | |||
82 | /** |
||
83 | * Return the array of Attachment objects |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | public function attachments() |
||
91 | |||
92 | /** |
||
93 | * Loops through the attachments and creates an Attachment object for each attachemnt then pushes each object into an array. |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | private function setAttachments() |
||
104 | } |
||
105 |
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.