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 |
||
7 | View Code Duplication | trait StackExchangeTrait |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * StackExchange username. |
||
11 | * |
||
12 | * @var string|null |
||
13 | * |
||
14 | * @ODM\String |
||
15 | */ |
||
16 | private $stackExchangeUsername; |
||
17 | |||
18 | /** |
||
19 | * StackExchange nickname. |
||
20 | * |
||
21 | * @var string|null |
||
22 | * |
||
23 | * @ODM\String |
||
24 | */ |
||
25 | private $stackExchangeNickname; |
||
26 | |||
27 | /** |
||
28 | * StackExchange first name. |
||
29 | * |
||
30 | * @var string|null |
||
31 | * |
||
32 | * @ODM\String |
||
33 | */ |
||
34 | private $stackExchangeFirstName; |
||
35 | |||
36 | /** |
||
37 | * StackExchange last name. |
||
38 | * |
||
39 | * @var string|null |
||
40 | * |
||
41 | * @ODM\String |
||
42 | */ |
||
43 | private $stackExchangeLastName; |
||
44 | |||
45 | /** |
||
46 | * StackExchange real name. |
||
47 | * |
||
48 | * @var string|null |
||
49 | * |
||
50 | * @ODM\String |
||
51 | */ |
||
52 | private $stackExchangeRealName; |
||
53 | |||
54 | /** |
||
55 | * StackExchange profilePicture. |
||
56 | * |
||
57 | * @var string|null |
||
58 | * |
||
59 | * @ODM\String |
||
60 | */ |
||
61 | private $stackExchangeEmail; |
||
62 | |||
63 | /** |
||
64 | * StackExchange profile picture. |
||
65 | * |
||
66 | * @var string|null |
||
67 | * |
||
68 | * @ODM\String |
||
69 | */ |
||
70 | private $stackExchangeProfilePicture; |
||
71 | |||
72 | /** |
||
73 | * StackExchange access token. |
||
74 | * |
||
75 | * @var string|null |
||
76 | * |
||
77 | * @ODM\String |
||
78 | */ |
||
79 | private $stackExchangeAccessToken; |
||
80 | |||
81 | /** |
||
82 | * StackExchange refresh token. |
||
83 | * |
||
84 | * @var string|null |
||
85 | * |
||
86 | * @ODM\String |
||
87 | */ |
||
88 | private $stackExchangeRefreshToken; |
||
89 | |||
90 | /** |
||
91 | * StackExchange expires in. |
||
92 | * |
||
93 | * @var string|null |
||
94 | * |
||
95 | * @ODM\String |
||
96 | */ |
||
97 | private $stackExchangeExpiresIn; |
||
98 | |||
99 | /** |
||
100 | * StackExchange data. |
||
101 | * |
||
102 | * @var array|null |
||
103 | * |
||
104 | * @ODM\Hash |
||
105 | */ |
||
106 | private $stackExchangeData; |
||
107 | |||
108 | /** |
||
109 | * Get StackExchange access token. |
||
110 | * |
||
111 | * @return string|null |
||
112 | */ |
||
113 | 3 | public function getStackExchangeAccessToken() |
|
117 | |||
118 | /** |
||
119 | * Gets the StackExchange data. |
||
120 | * |
||
121 | * @return array|null |
||
122 | */ |
||
123 | 3 | public function getStackExchangeData() |
|
127 | |||
128 | /** |
||
129 | * Get StackExchange email. |
||
130 | * |
||
131 | * @return string|null |
||
132 | */ |
||
133 | 3 | public function getStackExchangeEmail() |
|
137 | |||
138 | /** |
||
139 | * Get StackExchange expires in. |
||
140 | * |
||
141 | * @return string|null |
||
142 | */ |
||
143 | 3 | public function getStackExchangeExpiresIn() |
|
147 | |||
148 | /** |
||
149 | * Get StackExchange first name. |
||
150 | * |
||
151 | * @return string|null |
||
152 | */ |
||
153 | 3 | public function getStackExchangeFirstName() |
|
157 | |||
158 | /** |
||
159 | * Get StackExchange last name. |
||
160 | * |
||
161 | * @return string|null |
||
162 | */ |
||
163 | 3 | public function getStackExchangeLastName() |
|
167 | |||
168 | /** |
||
169 | * Get StackExchange nickname. |
||
170 | * |
||
171 | * @return string|null |
||
172 | */ |
||
173 | 3 | public function getStackExchangeNickname() |
|
177 | |||
178 | /** |
||
179 | * Get StackExchange profile picture. |
||
180 | * |
||
181 | * @return string|null |
||
182 | */ |
||
183 | 3 | public function getStackExchangeProfilePicture() |
|
187 | |||
188 | /** |
||
189 | * Get StackExchange real name. |
||
190 | * |
||
191 | * @return string|null |
||
192 | */ |
||
193 | 3 | public function getStackExchangeRealName() |
|
197 | |||
198 | /** |
||
199 | * Get StackExchange refresh token. |
||
200 | * |
||
201 | * @return string|null |
||
202 | */ |
||
203 | 3 | public function getStackExchangeRefreshToken() |
|
207 | |||
208 | /** |
||
209 | * Get StackExchange username. |
||
210 | * |
||
211 | * @return string|null |
||
212 | */ |
||
213 | 3 | public function getStackExchangeUsername() |
|
217 | |||
218 | /** |
||
219 | * Set StackExchange access token. |
||
220 | * |
||
221 | * @param string|null $accessToken |
||
222 | * |
||
223 | * @return self |
||
224 | */ |
||
225 | 3 | public function setStackExchangeAccessToken($accessToken) |
|
231 | |||
232 | /** |
||
233 | * Sets the StackExchange data. |
||
234 | * |
||
235 | * @param array|null $data |
||
236 | * |
||
237 | * @return self |
||
238 | */ |
||
239 | 3 | public function setStackExchangeData(array $data = null) |
|
245 | |||
246 | /** |
||
247 | * Set StackExchange email. |
||
248 | * |
||
249 | * @param string|null $email |
||
250 | * |
||
251 | * @return self |
||
252 | */ |
||
253 | 3 | public function setStackExchangeEmail($email) |
|
259 | |||
260 | /** |
||
261 | * Set StackExchange expires in. |
||
262 | * |
||
263 | * @param string|null $expiresIn |
||
264 | * |
||
265 | * @return self |
||
266 | */ |
||
267 | 3 | public function setStackExchangeExpiresIn($expiresIn) |
|
273 | |||
274 | /** |
||
275 | * Set StackExchange first name. |
||
276 | * |
||
277 | * @param string|null $firstName |
||
278 | * |
||
279 | * @return self |
||
280 | */ |
||
281 | 3 | public function setStackExchangeFirstName($firstName) |
|
287 | |||
288 | /** |
||
289 | * Set StackExchange last name. |
||
290 | * |
||
291 | * @param string|null $lastName |
||
292 | * |
||
293 | * @return self |
||
294 | */ |
||
295 | 3 | public function setStackExchangeLastName($lastName) |
|
301 | |||
302 | /** |
||
303 | * Set StackExchange nickname. |
||
304 | * |
||
305 | * @param string|null $nickname |
||
306 | * |
||
307 | * @return self |
||
308 | */ |
||
309 | 3 | public function setStackExchangeNickname($nickname) |
|
315 | |||
316 | /** |
||
317 | * Set StackExchange profile picture. |
||
318 | * |
||
319 | * @param string|null $profilePicture |
||
320 | * |
||
321 | * @return self |
||
322 | */ |
||
323 | 3 | public function setStackExchangeProfilePicture($profilePicture) |
|
329 | |||
330 | /** |
||
331 | * Set StackExchange real name. |
||
332 | * |
||
333 | * @param string|null $realName |
||
334 | * |
||
335 | * @return self |
||
336 | */ |
||
337 | 3 | public function setStackExchangeRealName($realName) |
|
343 | |||
344 | /** |
||
345 | * Set StackExchange refresh token. |
||
346 | * |
||
347 | * @param string|null $refreshToken |
||
348 | * |
||
349 | * @return self |
||
350 | */ |
||
351 | 3 | public function setStackExchangeRefreshToken($refreshToken) |
|
357 | |||
358 | /** |
||
359 | * Set StackExchange username. |
||
360 | * |
||
361 | * @param string|null $username |
||
362 | * |
||
363 | * @return self |
||
364 | */ |
||
365 | 3 | public function setStackExchangeUsername($username) |
|
371 | } |
||
372 |
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.