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 |
||
15 | final class Message extends Swift_Message |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $campaignId; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $perRecipientTags; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $metadata; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $perRecipientMetadata; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $substitutionData; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $perRecipientSubstitutionData; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $options; |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 21 | public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 57 | public function __construct($subject = null, $body = null, $contentType = null, $charset = null) |
|
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | 21 | public function getCampaignId() |
|
83 | |||
84 | /** |
||
85 | * @param string $campaignId |
||
86 | * |
||
87 | * @return Message |
||
88 | */ |
||
89 | 6 | public function setCampaignId($campaignId) |
|
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | */ |
||
99 | 21 | public function getPerRecipientTags() |
|
103 | |||
104 | /** |
||
105 | * @param string $recipient |
||
106 | * @param array $tags |
||
107 | * |
||
108 | * @return Message |
||
109 | */ |
||
110 | 12 | public function setPerRecipientTags($recipient, array $tags) |
|
116 | |||
117 | /** |
||
118 | * @return array |
||
119 | */ |
||
120 | 21 | public function getMetadata() |
|
124 | |||
125 | /** |
||
126 | * @param array $metadata |
||
127 | * |
||
128 | * @return Message |
||
129 | */ |
||
130 | 12 | public function setMetadata(array $metadata) |
|
136 | |||
137 | /** |
||
138 | * @return array |
||
139 | */ |
||
140 | 21 | public function getPerRecipientMetadata() |
|
144 | |||
145 | /** |
||
146 | * @param string $recipient |
||
147 | * @param array $metadata |
||
148 | * |
||
149 | * @return Message |
||
150 | */ |
||
151 | 18 | public function setPerRecipientMetadata($recipient, array $metadata) |
|
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | 21 | public function getSubstitutionData() |
|
165 | |||
166 | /** |
||
167 | * @param array $substitutionData |
||
168 | * |
||
169 | * @return Message |
||
170 | */ |
||
171 | 6 | public function setSubstitutionData(array $substitutionData) |
|
177 | |||
178 | /** |
||
179 | * @return array |
||
180 | */ |
||
181 | 21 | public function getPerRecipientSubstitutionData() |
|
185 | |||
186 | /** |
||
187 | * @param string $recipient |
||
188 | * @param array $substitutionData |
||
189 | * |
||
190 | * @return Message |
||
191 | */ |
||
192 | 12 | public function setPerRecipientSubstitutionData($recipient, array $substitutionData) |
|
198 | |||
199 | /** |
||
200 | * @return array |
||
201 | */ |
||
202 | 21 | public function getOptions() |
|
206 | |||
207 | /** |
||
208 | * @param array $options |
||
209 | * |
||
210 | * @return Message |
||
211 | */ |
||
212 | 12 | View Code Duplication | public function setOptions(array $options) |
227 | |||
228 | /** |
||
229 | * @param array $tags |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | 12 | private function sanitizeTags(array $tags) |
|
243 | |||
244 | /** |
||
245 | * @param array $metadata |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | 24 | private function sanitizeMetadata(array $metadata) |
|
262 | |||
263 | /** |
||
264 | * @param array $substitutionData |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | 12 | private function sanitizeSubstitutionData(array $substitutionData) |
|
278 | } |
||
279 |
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.