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) |
||
128 | |||
129 | /** |
||
130 | * This method updates an emoji. |
||
131 | * |
||
132 | * @param $request |
||
133 | * @param $response |
||
134 | * |
||
135 | * @return json |
||
136 | */ |
||
137 | public function updateEmojiByPutVerb(Request $request, Response $response, $args) |
||
169 | |||
170 | /** |
||
171 | * This method updates an emoji partially. |
||
172 | * |
||
173 | * @param $request |
||
174 | * @param $response |
||
175 | * |
||
176 | * @return json |
||
177 | */ |
||
178 | public function updateEmojiByPatchVerb(Request $request, Response $response, $args) |
||
202 | |||
203 | /** |
||
204 | * This method deletes an emoji. |
||
205 | * |
||
206 | * @param $request |
||
207 | * @param $response |
||
208 | * @param $args |
||
209 | * |
||
210 | * @return json |
||
211 | */ |
||
212 | public function deleteEmoji(Request $request, Response $response, $args) |
||
225 | |||
226 | /** |
||
227 | * This method creates emoji keywords. |
||
228 | * |
||
229 | * @param $request |
||
230 | * @param $response |
||
231 | * @param $args |
||
232 | * |
||
233 | * @return $id |
||
234 | */ |
||
235 | View Code Duplication | public function createEmojiKeywords($emoji_id, $keywords) |
|
253 | |||
254 | /** |
||
255 | * This method format emoji result |
||
256 | * |
||
257 | * @param $emojis |
||
258 | * |
||
259 | * @return array $emojis |
||
260 | */ |
||
261 | public function formatEmoji(array $emojis) |
||
271 | |||
272 | /** |
||
273 | * This method authenticate and return user id. |
||
274 | */ |
||
275 | public function getCurrentUserId($request, $response) |
||
300 | |||
301 | /** |
||
302 | * This method checks for duplicate emoji |
||
303 | * |
||
304 | * @param $name |
||
305 | * |
||
306 | * @return boolean true |
||
307 | */ |
||
308 | public function checkForDuplicateEmoji($emojiName) |
||
324 | |||
325 | /** |
||
326 | * This method will |
||
327 | * verify the fields supplied by the user when posting to the API |
||
328 | * and also validate their input for empty values |
||
329 | * |
||
330 | * @param $expectedFields |
||
331 | * @param $suppliedFields |
||
332 | * |
||
333 | * @return json response |
||
334 | */ |
||
335 | public function validateUserInput(array $expectedFields, array $suppliedFields) |
||
368 | |||
369 | } |
||
370 |
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.