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 |
||
12 | class MessageOptions extends AbstractOptions |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $from = ''; |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $fromName = ''; |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $replyTo = ''; |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $replyToName = ''; |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $to = []; |
||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $cc = []; |
||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $bcc = []; |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $subject = ''; |
||
46 | /** |
||
47 | * @var BodyOptions |
||
48 | */ |
||
49 | protected $body; |
||
50 | /** |
||
51 | * @var AttachmentsOptions |
||
52 | */ |
||
53 | protected $attachments; |
||
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | 18 | public function getFrom() |
|
62 | |||
63 | /** |
||
64 | * @param string $from |
||
65 | * @return $this |
||
66 | */ |
||
67 | 6 | public function setFrom($from) |
|
68 | { |
||
69 | 6 | $this->from = $from; |
|
70 | 6 | return $this; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 2 | public function getFromName() |
|
80 | |||
81 | /** |
||
82 | * @param string $fromName |
||
83 | * @return $this |
||
84 | */ |
||
85 | 1 | public function setFromName($fromName) |
|
86 | { |
||
87 | 1 | $this->fromName = $fromName; |
|
88 | 1 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | 14 | public function getReplyTo() |
|
98 | |||
99 | /** |
||
100 | * @param string $replyTo |
||
101 | * @return $this |
||
102 | */ |
||
103 | 1 | public function setReplyTo($replyTo) |
|
104 | { |
||
105 | 1 | $this->replyTo = $replyTo; |
|
106 | |||
107 | 1 | return $this; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | 2 | public function getReplyToName() |
|
117 | |||
118 | /** |
||
119 | * @param string $replyToName |
||
120 | * @return $this |
||
121 | */ |
||
122 | 1 | public function setReplyToName($replyToName) |
|
123 | { |
||
124 | 1 | $this->replyToName = $replyToName; |
|
125 | |||
126 | 1 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return array |
||
131 | */ |
||
132 | 18 | public function getTo() |
|
136 | |||
137 | /** |
||
138 | * @param array|string $to |
||
139 | * @return $this |
||
140 | */ |
||
141 | 6 | public function setTo($to) |
|
142 | { |
||
143 | 6 | $this->to = (array) $to; |
|
144 | 6 | return $this; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return array |
||
149 | */ |
||
150 | 15 | public function getCc() |
|
154 | |||
155 | /** |
||
156 | * @param array|string $cc |
||
157 | * @return $this |
||
158 | */ |
||
159 | 1 | public function setCc($cc) |
|
160 | { |
||
161 | 1 | $this->cc = (array) $cc; |
|
162 | 1 | return $this; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return array |
||
167 | */ |
||
168 | 15 | public function getBcc() |
|
172 | |||
173 | /** |
||
174 | * @param string|array $bcc |
||
175 | * @return $this |
||
176 | */ |
||
177 | 1 | public function setBcc($bcc) |
|
178 | { |
||
179 | 1 | $this->bcc = (array) $bcc; |
|
180 | 1 | return $this; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | 13 | public function getSubject() |
|
190 | |||
191 | /** |
||
192 | * @param string $subject |
||
193 | * @return $this |
||
194 | */ |
||
195 | 2 | public function setSubject($subject) |
|
196 | { |
||
197 | 2 | $this->subject = $subject; |
|
198 | 2 | return $this; |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return BodyOptions |
||
203 | */ |
||
204 | 13 | public function getBody() |
|
205 | { |
||
206 | 13 | if (! isset($this->body)) { |
|
207 | 9 | $this->setBody([]); |
|
208 | 9 | } |
|
209 | |||
210 | 13 | return $this->body; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param BodyOptions|array $body |
||
215 | * @return $this |
||
216 | */ |
||
217 | 14 | View Code Duplication | public function setBody($body) |
232 | |||
233 | /** |
||
234 | * @return AttachmentsOptions |
||
235 | */ |
||
236 | 13 | public function getAttachments() |
|
237 | { |
||
238 | 13 | if (! isset($this->attachments)) { |
|
239 | 11 | $this->setAttachments([]); |
|
240 | 11 | } |
|
241 | |||
242 | 13 | return $this->attachments; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param AttachmentsOptions|array $attachments |
||
247 | * @return $this |
||
248 | */ |
||
249 | 14 | View Code Duplication | public function setAttachments($attachments) |
264 | } |
||
265 |
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.