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) |
||
78 | |||
79 | /** |
||
80 | * This method creates a new emoji. |
||
81 | * |
||
82 | * @param $args |
||
83 | * |
||
84 | * @return json $response; |
||
85 | */ |
||
86 | public function createEmoji(Request $request, Response $response) |
||
106 | |||
107 | /** |
||
108 | * This method creates emoji and keywords associated with it. |
||
109 | * |
||
110 | * @param $emoji |
||
111 | * @param $request |
||
112 | * @param $response |
||
113 | * @param $requestParams |
||
114 | * |
||
115 | * @return json response |
||
116 | */ |
||
117 | public function runCreateEmoji($request, $response, $requestParams) |
||
133 | |||
134 | /** |
||
135 | * This method updates an emoji. |
||
136 | * |
||
137 | * @param $request |
||
138 | * @param $response |
||
139 | * |
||
140 | * @return json |
||
141 | */ |
||
142 | public function updateEmojiByPutVerb(Request $request, Response $response, $args) |
||
170 | |||
171 | /** |
||
172 | * This method updates an emoji. |
||
173 | * |
||
174 | * @param $emoji |
||
175 | * @param $response |
||
176 | * @param $updateParams |
||
177 | * |
||
178 | * @return json $response |
||
179 | */ |
||
180 | public function runUpdateEmoji($emoji, $response, $updateParams) |
||
190 | |||
191 | /** |
||
192 | * This method updates an emoji partially. |
||
193 | * |
||
194 | * @param $request |
||
195 | * @param $response |
||
196 | * |
||
197 | * @return json |
||
198 | */ |
||
199 | public function updateEmojiByPatchVerb(Request $request, Response $response, $args) |
||
230 | |||
231 | /** |
||
232 | * This method deletes an emoji. |
||
233 | * |
||
234 | * @param $request |
||
235 | * @param $response |
||
236 | * @param $args |
||
237 | * |
||
238 | * @return json |
||
239 | */ |
||
240 | public function deleteEmoji(Request $request, Response $response, $args) |
||
266 | |||
267 | /** |
||
268 | * This method creates emoji keywords. |
||
269 | * |
||
270 | * @param $request |
||
271 | * @param $response |
||
272 | * @param $args |
||
273 | * |
||
274 | * @return $id |
||
275 | */ |
||
276 | View Code Duplication | public function createEmojiKeywords($emoji_id, $keywords) |
|
294 | |||
295 | /** |
||
296 | * This method format emoji result. |
||
297 | * |
||
298 | * @param $emojis |
||
299 | * |
||
300 | * @return array $emojis |
||
301 | */ |
||
302 | public function formatEmoji(array $emojis) |
||
312 | |||
313 | /** |
||
314 | * This method authenticate and return user id. |
||
315 | */ |
||
316 | public function getCurrentUserId($request, $response) |
||
341 | |||
342 | /** |
||
343 | * This method checks for duplicate emoji. |
||
344 | * |
||
345 | * @param $name |
||
346 | * |
||
347 | * @return bool true |
||
348 | */ |
||
349 | public function checkForDuplicateEmoji($emojiName) |
||
363 | |||
364 | /** |
||
365 | * This method solves for rightful of a record |
||
366 | */ |
||
367 | public function findTheOwner($args, $request, $response) |
||
373 | |||
374 | /** |
||
375 | * This method will |
||
376 | * verify the fields supplied by the user when posting to the API |
||
377 | * and also validate their input for empty values. |
||
378 | * |
||
379 | * @param $expectedFields |
||
380 | * @param $suppliedFields |
||
381 | * |
||
382 | * @return json response |
||
383 | */ |
||
384 | public function validateUserInput(array $expectedFields, array $suppliedFields) |
||
414 | } |
||
415 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.