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 |
||
| 16 | class EmojiController |
||
| 17 | { |
||
| 18 | public $authController; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * This is a constructor; a default method that will be called automatically during class instantiation. |
||
| 22 | */ |
||
| 23 | public function __construct() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get all emojis. |
||
| 30 | * |
||
| 31 | * @param Slim\Http\Request $request |
||
| 32 | * @param Slim\Http\Response $response |
||
| 33 | * @param array $args |
||
|
|
|||
| 34 | * |
||
| 35 | * @return Slim\Http\Response |
||
| 36 | */ |
||
| 37 | public function getAllEmojis($request, $response) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get a single emoji. |
||
| 71 | * |
||
| 72 | * @param Slim\Http\Request $request |
||
| 73 | * @param Slim\Http\Response $response |
||
| 74 | * @param array $args |
||
| 75 | * |
||
| 76 | * @return Slim\Http\Response |
||
| 77 | */ |
||
| 78 | public function getSingleEmoji($request, $response, $args) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * This method creates emoji and keywords associated with it. |
||
| 103 | * |
||
| 104 | * @param $request |
||
| 105 | * @param $response |
||
| 106 | * @param $requestParams |
||
| 107 | * |
||
| 108 | * @return json response |
||
| 109 | */ |
||
| 110 | public function CreateEmoji($request, $response, $requestParams) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * This method updates an emoji by using Patch. |
||
| 148 | * |
||
| 149 | * @param $request |
||
| 150 | * @param $response |
||
| 151 | * @param $args |
||
| 152 | * |
||
| 153 | * @return json $response |
||
| 154 | */ |
||
| 155 | public function updateEmojiByPatch($request, $response, $args) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * This method updates an emoji by using put. |
||
| 178 | * |
||
| 179 | * @param $request |
||
| 180 | * @param $response |
||
| 181 | * @param $args |
||
| 182 | * |
||
| 183 | * @return json $response |
||
| 184 | */ |
||
| 185 | public function updateEmojiByPut($request, $response, $args) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * This method deletes an emoji. |
||
| 208 | * |
||
| 209 | * @param Slim\Http\Request $request |
||
| 210 | * @param Slim\Http\Response $response |
||
| 211 | * @param array $args |
||
| 212 | * |
||
| 213 | * @return json response |
||
| 214 | */ |
||
| 215 | public function deleteEmoji($request, $response, $args) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * This method authenticate and returns user id. |
||
| 229 | * |
||
| 230 | * @param $response |
||
| 231 | * @param $request |
||
| 232 | * |
||
| 233 | * @return user id |
||
| 234 | */ |
||
| 235 | private function getUsername($request, $response) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * This method solves for rightful owner of a record. |
||
| 256 | * |
||
| 257 | * @param $response |
||
| 258 | * @param $request |
||
| 259 | * @param $args |
||
| 260 | * |
||
| 261 | * @return user id |
||
| 262 | */ |
||
| 263 | private function getTheOwner($request, $response, $args) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * This method creates emoji keywords. |
||
| 272 | * |
||
| 273 | * @param $emoji_id |
||
| 274 | * @param $keywords |
||
| 275 | * |
||
| 276 | * @return keyword id |
||
| 277 | */ |
||
| 278 | public function createEmojiKeywords($emoji_id, $keywords) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * This method checks for empty input from user. |
||
| 302 | * |
||
| 303 | * @param $inputName |
||
| 304 | * @param $inputChars |
||
| 305 | * @param $inputCategory |
||
| 306 | * @param $inputKeywords |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function checkEmptyInput($inputName, $inputChars, $inputCategory, $inputKeywords) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * This method checks for duplicate emoji. |
||
| 321 | * |
||
| 322 | * @param $inputName |
||
| 323 | * @param $inputChars |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function checkDuplicateEmoji($inputName, $inputChars) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * This method checks for empty input from user. |
||
| 342 | * |
||
| 343 | * @param $request |
||
| 344 | * @param $response |
||
| 345 | * @param $args |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | public function validateArgs($request, $response, $args) |
||
| 367 | } |
||
| 368 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.