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 Api 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 Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Api |
||
| 11 | { |
||
| 12 | |||
| 13 | private $baseUrl = "https://api.console.eyowo.com/"; |
||
| 14 | private $appKey; |
||
| 15 | private $appSecret; |
||
| 16 | public $client; |
||
| 17 | protected $output; |
||
| 18 | protected $factors = ['sms']; |
||
| 19 | protected $providers = ['etisalat', 'glo', 'airtel', 'mtn']; |
||
| 20 | protected $errorCodes = [ |
||
| 21 | 400 => 'Bad Request -- Your request is invalid.', |
||
| 22 | 401 => 'Unauthorized -- Your API key is wrong.', |
||
| 23 | 403 => 'Forbidden -- The kitten requested is hidden for administrators only.', |
||
| 24 | 404 => 'Not Found -- The specified kitten could not be found.', |
||
| 25 | 405 => 'Method Not Allowed -- You tried to access a kitten with an invalid method.', |
||
| 26 | 406 => "Not Acceptable -- You requested a format that isn't json.", |
||
| 27 | 410 => 'Gone -- The kitten requested has been removed from our servers.', |
||
| 28 | 418 => "I'm a teapot.", |
||
| 29 | 429 => "Too Many Requests -- You're requesting too many kittens! Slow down!", |
||
| 30 | 500 => "Internal Server Error -- We had a problem with our server. Try again later.", |
||
| 31 | 503 => "Service Unavailable -- We're temporarily offline for maintenance. Please try again later." |
||
| 32 | ]; |
||
| 33 | |||
| 34 | protected $errorMessage; |
||
| 35 | protected $statusCode; |
||
| 36 | |||
| 37 | 14 | public function __construct($appKey = null) |
|
| 49 | |||
| 50 | 2 | public function resolveResponse(MessageInterface $response) |
|
| 57 | |||
| 58 | 9 | public function handleException(ClientException $exception) |
|
| 70 | |||
| 71 | public function getStatusCode() |
||
| 75 | |||
| 76 | 1 | public function getError() |
|
| 80 | |||
| 81 | 1 | View Code Duplication | public function validate($phone) |
| 96 | |||
| 97 | 2 | public function initiateAuthorization($phone, $factor = 'sms') |
|
| 116 | |||
| 117 | 1 | View Code Duplication | public function generateToken($phone, $passcode, $factor = 'sms') |
| 137 | |||
| 138 | 1 | View Code Duplication | public function refreshToken($refreshToken) |
| 153 | |||
| 154 | 1 | public function transferToPhone($walletToken, $amount, $mobile) |
|
| 171 | |||
| 172 | 1 | public function transferToBank($walletToken, $amount, $accountName, $accountNumber, $bankCode) |
|
| 191 | |||
| 192 | 2 | View Code Duplication | public function vtu($walletToken, $mobile, $amount, $provider) |
| 214 | |||
| 215 | 2 | View Code Duplication | public function balance($walletToken) |
| 228 | |||
| 229 | 2 | public function banks() |
|
| 239 | |||
| 240 | 1 | public function getBanks() |
|
| 244 | |||
| 245 | 1 | public function getWalletBalance() |
|
| 249 | |||
| 250 | 1 | public function getTokenData() |
|
| 254 | |||
| 255 | 1 | public function getAccessToken() |
|
| 260 | |||
| 261 | 1 | public function getRefreshToken() |
|
| 266 | |||
| 267 | public function getUser() |
||
| 271 | |||
| 272 | public function mobileIsValid() |
||
| 276 | |||
| 277 | } |
||
| 278 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: