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 |
||
13 | class Message extends Message\Part |
||
14 | { |
||
15 | private $headers; |
||
16 | private $attachments; |
||
17 | |||
18 | /** |
||
19 | * @var boolean |
||
20 | */ |
||
21 | private $keepUnseen = false; |
||
22 | |||
23 | /** |
||
24 | * Constructor |
||
25 | * |
||
26 | * @param resource $stream IMAP stream |
||
27 | * @param int $messageNumber Message number |
||
28 | */ |
||
29 | public function __construct($stream, $messageNumber) { |
||
35 | |||
36 | /** |
||
37 | * Get message id |
||
38 | * |
||
39 | * A unique message id in the form <...> |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | public function getId() |
||
47 | |||
48 | /** |
||
49 | * Get message sender (from headers) |
||
50 | * |
||
51 | * @return EmailAddress |
||
52 | */ |
||
53 | public function getFrom() |
||
57 | |||
58 | /** |
||
59 | * Get To recipients |
||
60 | * |
||
61 | * @return EmailAddress[] Empty array in case message has no To: recipients |
||
62 | */ |
||
63 | public function getTo() |
||
67 | |||
68 | /** |
||
69 | * Get Cc recipients |
||
70 | * |
||
71 | * @return EmailAddress[] Empty array in case message has no CC: recipients |
||
72 | */ |
||
73 | public function getCc() |
||
77 | |||
78 | /** |
||
79 | * Get message number (from headers) |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | public function getNumber() |
||
87 | |||
88 | /** |
||
89 | * Get date (from headers) |
||
90 | * |
||
91 | * @return \DateTime |
||
92 | */ |
||
93 | public function getDate() |
||
97 | |||
98 | /** |
||
99 | * Get message size (from headers) |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getSize() |
||
107 | |||
108 | /** |
||
109 | * Get raw part content |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getContent($keepUnseen = false) |
||
121 | |||
122 | /** |
||
123 | * Get message answered flag value (from headers) |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public function isAnswered() |
||
131 | |||
132 | /** |
||
133 | * Get message deleted flag value (from headers) |
||
134 | * |
||
135 | * @return boolean |
||
136 | */ |
||
137 | public function isDeleted() |
||
141 | |||
142 | /** |
||
143 | * Get message draft flag value (from headers) |
||
144 | * |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function isDraft() |
||
151 | |||
152 | /** |
||
153 | * Has the message been marked as read? |
||
154 | * |
||
155 | * @return boolean |
||
156 | */ |
||
157 | public function isSeen() |
||
161 | |||
162 | /** |
||
163 | * Get message subject (from headers) |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getSubject() |
||
171 | |||
172 | /** |
||
173 | * Get message headers |
||
174 | * |
||
175 | * @return Message\Headers |
||
176 | */ |
||
177 | public function getHeaders() |
||
189 | |||
190 | /** |
||
191 | * Get body HTML |
||
192 | * |
||
193 | * @return string | null Null if message has no HTML message part |
||
194 | */ |
||
195 | View Code Duplication | public function getBodyHtml() |
|
204 | |||
205 | /** |
||
206 | * Get body text |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | View Code Duplication | public function getBodyText() |
|
222 | |||
223 | /** |
||
224 | * Get attachments (if any) linked to this e-mail |
||
225 | * |
||
226 | * @return Message\Attachment[] |
||
227 | */ |
||
228 | public function getAttachments() |
||
248 | |||
249 | /** |
||
250 | * Does this message have attachments? |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function hasAttachments() |
||
258 | |||
259 | /** |
||
260 | * Delete message |
||
261 | * |
||
262 | * @throws MessageDeleteException |
||
263 | */ |
||
264 | public function delete() |
||
275 | |||
276 | /** |
||
277 | * Move message to another mailbox |
||
278 | * @param Mailbox $mailbox |
||
279 | * |
||
280 | * @throws MessageMoveException |
||
281 | * @return Message |
||
282 | */ |
||
283 | public function move(Mailbox $mailbox) |
||
293 | |||
294 | /** |
||
295 | * Prevent the message from being marked as seen |
||
296 | * |
||
297 | * Defaults to true, so messages that are read will be still marked as unseen. |
||
298 | * |
||
299 | * @param bool $bool |
||
300 | * |
||
301 | * @return Message |
||
302 | */ |
||
303 | public function keepUnseen($bool = true) |
||
309 | |||
310 | /** |
||
311 | * Load message structure |
||
312 | */ |
||
313 | private function loadStructure() |
||
334 | } |
||
335 |
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.