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 |
||
11 | class RemServerController implements ContainerInjectableInterface |
||
12 | { |
||
13 | use ContainerInjectableTrait; |
||
14 | |||
15 | |||
16 | |||
17 | /** |
||
18 | * Initiate the REM server before each action, if it has not already |
||
19 | * some dataset(s). |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | 23 | public function initialize() : void |
|
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * Init or re-init the REM Server. |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | 9 | View Code Duplication | public function initActionGet() : array |
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Get a dataset $key or parts of it by using the querystring. |
||
54 | * |
||
55 | * @param array $args variadic argument containg all parts of the |
||
56 | * request. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | 12 | public function catchAllGet(...$args) : array |
|
76 | |||
77 | |||
78 | |||
79 | /** |
||
80 | * Get a dataset $key or parts of it by using the querystring. |
||
81 | * |
||
82 | * @param array $key to the dataset to get. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 5 | public function getDataset($key) : array |
|
100 | |||
101 | |||
102 | |||
103 | /** |
||
104 | * Get one item from the dataset. |
||
105 | * |
||
106 | * @param string $key for the dataset |
||
107 | * @param int $itemId for the item to get |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 7 | View Code Duplication | public function getItem(string $key, int $itemId) : array |
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * Create a new item by getting the entry from the request body and add |
||
124 | * to the dataset. |
||
125 | * |
||
126 | * @param string $key for the dataset |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 4 | public function catchAllPost(...$args) : array |
|
150 | |||
151 | |||
152 | /** |
||
153 | * Upsert/replace an item in the dataset, entry is taken from request body. |
||
154 | * |
||
155 | * @param string $key for the dataset |
||
156 | * @param string $itemId where to save the entry |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | 4 | public function catchAllPut(...$args) : array |
|
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * Delete an item from the dataset. |
||
189 | * |
||
190 | * @param string $key for the dataset |
||
191 | * @param string $itemId for the item to delete |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | 5 | public function catchAllDelete(...$args) : array |
|
216 | |||
217 | |||
218 | |||
219 | /** |
||
220 | * Get the request body from the HTTP request and treat it as |
||
221 | * JSON data. |
||
222 | * |
||
223 | * @throws Exception when request body is invalid JSON. |
||
224 | * |
||
225 | * @return mixed as the JSON converted content. |
||
226 | */ |
||
227 | 8 | protected function getRequestBody() |
|
238 | |||
239 | |||
240 | |||
241 | /** |
||
242 | * Show a message that the route is unsupported, a local 404. |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | 3 | public function catchAll() |
|
250 | } |
||
251 |
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.