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:
Complex classes like EmojiController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EmojiController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class EmojiController |
||
15 | { |
||
16 | private $auth; |
||
17 | |||
18 | public function __construct(Oauth $auth) |
||
22 | |||
23 | /** |
||
24 | * This method list all emojis. |
||
25 | * |
||
26 | * @param $response |
||
27 | * |
||
28 | * @return json $emojis |
||
29 | */ |
||
30 | public function listAllEmoji(Response $response) |
||
43 | |||
44 | /** |
||
45 | * This method get a single emoji. |
||
46 | * |
||
47 | * @param $response |
||
48 | * @param $args |
||
49 | * |
||
50 | * @return json $emoji |
||
51 | */ |
||
52 | public function getSingleEmoji(Response $response, $args) |
||
77 | |||
78 | /** |
||
79 | * This method creates a new emoji. |
||
80 | * |
||
81 | * @param $args |
||
82 | * |
||
83 | * @return json $response; |
||
84 | */ |
||
85 | public function createEmoji(Request $request, Response $response) |
||
104 | |||
105 | /** |
||
106 | * |
||
107 | * This method creates emoji and keywords associated with it |
||
108 | * @param $emoji |
||
109 | * @param $request |
||
110 | * @param $response |
||
111 | * @param $requestParams |
||
112 | * |
||
113 | * @return json response |
||
114 | */ |
||
115 | public function runCreateEmoji($request, $response, $requestParams) |
||
130 | |||
131 | /** |
||
132 | * This method updates an emoji. |
||
133 | * |
||
134 | * @param $request |
||
135 | * @param $response |
||
136 | * |
||
137 | * @return json |
||
138 | */ |
||
139 | public function updateEmojiByPutVerb(Request $request, Response $response, $args) |
||
159 | |||
160 | /** |
||
161 | * This method updates an emoji |
||
162 | * |
||
163 | * @param $emoji |
||
164 | * @param $response |
||
165 | * @param $updateParams |
||
166 | * |
||
167 | * @return json $response |
||
168 | */ |
||
169 | public function runUpdateEmoji($emoji, $response, $updateParams) |
||
179 | |||
180 | /** |
||
181 | * This method updates an emoji partially. |
||
182 | * |
||
183 | * @param $request |
||
184 | * @param $response |
||
185 | * |
||
186 | * @return json |
||
187 | */ |
||
188 | public function updateEmojiByPatchVerb(Request $request, Response $response, $args) |
||
213 | |||
214 | /** |
||
215 | * This method deletes an emoji. |
||
216 | * |
||
217 | * @param $request |
||
218 | * @param $response |
||
219 | * @param $args |
||
220 | * |
||
221 | * @return json |
||
222 | */ |
||
223 | public function deleteEmoji(Request $request, Response $response, $args) |
||
248 | |||
249 | /** |
||
250 | * This method creates emoji keywords. |
||
251 | * |
||
252 | * @param $request |
||
253 | * @param $response |
||
254 | * @param $args |
||
255 | * |
||
256 | * @return $id |
||
257 | */ |
||
258 | View Code Duplication | public function createEmojiKeywords($emoji_id, $keywords) |
|
276 | |||
277 | /** |
||
278 | * This method format emoji result. |
||
279 | * |
||
280 | * @param $emojis |
||
281 | * |
||
282 | * @return array $emojis |
||
283 | */ |
||
284 | public function formatEmoji(array $emojis) |
||
294 | |||
295 | /** |
||
296 | * This method authenticate and return user id. |
||
297 | */ |
||
298 | public function getCurrentUserId($request, $response) |
||
323 | |||
324 | /** |
||
325 | * This method checks for duplicate emoji. |
||
326 | * |
||
327 | * @param $name |
||
328 | * |
||
329 | * @return bool true |
||
330 | */ |
||
331 | public function checkForDuplicateEmoji($emojiName) |
||
345 | |||
346 | /** |
||
347 | * This method will |
||
348 | * verify the fields supplied by the user when posting to the API |
||
349 | * and also validate their input for empty values. |
||
350 | * |
||
351 | * @param $expectedFields |
||
352 | * @param $suppliedFields |
||
353 | * |
||
354 | * @return json response |
||
355 | */ |
||
356 | public function validateUserInput(array $expectedFields, array $suppliedFields) |
||
386 | } |
||
387 |
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.