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 |
||
9 | class NotificationHub |
||
10 | { |
||
11 | const API_VERSION = '?api-version=2013-08'; |
||
12 | |||
13 | const METHOD_GET = 'GET'; |
||
14 | const METHOD_POST = 'POST'; |
||
15 | const METHOD_PUT = 'PUT'; |
||
16 | const METHOD_DELETE = 'DELETE'; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $endpoint; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $hubPath; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $sasKeyName; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sasKeyValue; |
||
37 | |||
38 | /** |
||
39 | * Initializes a new NotificationHub. |
||
40 | * |
||
41 | * @param string $connectionString |
||
42 | * @param string $hubPath |
||
43 | */ |
||
44 | public function __construct($connectionString, $hubPath) |
||
49 | |||
50 | /** |
||
51 | * Send a Notification. |
||
52 | * |
||
53 | * @param NotificationInterface $notification |
||
54 | */ |
||
55 | public function sendNotification(NotificationInterface $notification) |
||
64 | |||
65 | /** |
||
66 | * Create Registration. |
||
67 | * |
||
68 | * @param RegistrationInterface $registration |
||
69 | * |
||
70 | * @throws \RuntimeException |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | View Code Duplication | public function createRegistration(RegistrationInterface $registration) |
|
89 | |||
90 | /** |
||
91 | * Create or Update Registration. |
||
92 | * |
||
93 | * @param RegistrationInterface $registration |
||
94 | * |
||
95 | * @throws \RuntimeException |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | View Code Duplication | public function updateRegistration(RegistrationInterface $registration) |
|
114 | |||
115 | /** |
||
116 | * Delete Registration. |
||
117 | * |
||
118 | * @param ApiContentInterface $registration |
||
119 | * |
||
120 | * @throws \RuntimeException |
||
121 | */ |
||
122 | View Code Duplication | public function deleteRegistration(RegistrationInterface $registration) |
|
135 | |||
136 | /** |
||
137 | * Read Registration. |
||
138 | * |
||
139 | * @param RegistrationInterface $registration |
||
140 | * |
||
141 | * @throws \RuntimeException |
||
142 | * |
||
143 | * @return mixed |
||
144 | */ |
||
145 | View Code Duplication | public function readRegistration(RegistrationInterface $registration) |
|
160 | |||
161 | /** |
||
162 | * Read All Registrations of a Channel. |
||
163 | * |
||
164 | * @param RegistrationInterface $registration |
||
165 | * |
||
166 | * @throws \RuntimeException |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function readAllRegistrationsOfAChannel(RegistrationInterface $registration) |
||
194 | |||
195 | /** |
||
196 | * Create Registration ID. |
||
197 | * |
||
198 | * @return string Registration ID |
||
199 | */ |
||
200 | public function createRegistrationId() |
||
220 | |||
221 | /** |
||
222 | * Send the request to API. |
||
223 | * |
||
224 | * @param string $method |
||
225 | * @param string $uri |
||
226 | * @param array $headers |
||
227 | * @param string $payload |
||
228 | * @param bool $responseHeader |
||
229 | * |
||
230 | * @throws \RuntimeException |
||
231 | * |
||
232 | * @return string |
||
233 | * |
||
234 | * @codeCoverageIgnore |
||
235 | */ |
||
236 | protected function request($method, $uri, $headers, $payload = null, $responseHeader = false) |
||
268 | |||
269 | /** |
||
270 | * Parses the connection string. |
||
271 | * |
||
272 | * @param string $connectionString |
||
273 | * |
||
274 | * @throws \RuntimeException |
||
275 | */ |
||
276 | private function parseConnectionString($connectionString) |
||
297 | |||
298 | /** |
||
299 | * Generates the SAS token. |
||
300 | * |
||
301 | * @param string $uri |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | private function generateSasToken($uri) |
||
317 | } |
||
318 |
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.