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 Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Client extends \GuzzleHttp\Client |
||
16 | { |
||
17 | /** |
||
18 | * @const string Version number |
||
19 | */ |
||
20 | const VERSION = '0.0.1'; |
||
21 | |||
22 | /** |
||
23 | * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
||
24 | * (127.0.0.1). |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $options = [ |
||
29 | 'scheme' => 'https', |
||
30 | 'hostname' => 'localhost', |
||
31 | 'port' => '8080', |
||
32 | 'token' => null, |
||
33 | 'username' => null, |
||
34 | 'password' => null, |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * @param array $options |
||
39 | */ |
||
40 | 20 | public function __construct($options = []) |
|
63 | |||
64 | /** |
||
65 | * Set the bearer token. |
||
66 | * |
||
67 | * @param string $token Bearer Token from Auth request |
||
68 | */ |
||
69 | 11 | public function setBearerToken($token) |
|
73 | |||
74 | /** |
||
75 | * Set the password for basic authentication. |
||
76 | * |
||
77 | * @param string $password Password for use with basic authentication |
||
|
|||
78 | */ |
||
79 | public function setPassword($token) |
||
83 | |||
84 | /** |
||
85 | * Set the username for basic authentication. |
||
86 | * |
||
87 | * @param string $username Username for use with basic authentication |
||
88 | */ |
||
89 | public function setUsername($username) |
||
93 | |||
94 | /** |
||
95 | * Authenticate and get Bearer token from SmartCall. |
||
96 | * |
||
97 | * @throws Exception |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 3 | View Code Duplication | public function auth() |
124 | |||
125 | /** |
||
126 | * Authenticate and invalidates all the user allocated tokens. |
||
127 | * |
||
128 | * @throws Exception |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 2 | View Code Duplication | public function authDelete() |
155 | |||
156 | /** |
||
157 | * Authenticate and invalidates all the user allocated tokens. |
||
158 | * |
||
159 | * @throws Exception |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 3 | View Code Duplication | public function authFlush() |
186 | |||
187 | /** |
||
188 | * Authenticate and gets the number of available session tokens. |
||
189 | * |
||
190 | * @throws Exception |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | 3 | View Code Duplication | public function authToken() |
217 | |||
218 | /** |
||
219 | * Authenticate and request period based cashup reports. |
||
220 | * |
||
221 | * @param string $dealerMsisdn |
||
222 | * @param string $start |
||
223 | * @param string $end |
||
224 | * |
||
225 | * @throws Exception |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | View Code Duplication | public function cashup($dealerMsisdn, $start, $end) |
|
257 | |||
258 | /** |
||
259 | * Authenticate and retrieves the dealer balance in Rands. |
||
260 | * |
||
261 | * @param string $dealerMsisdn |
||
262 | * |
||
263 | * @throws Exception |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | View Code Duplication | public function balance($dealerMsisdn) |
|
293 | |||
294 | /** |
||
295 | * Authenticate and request current day cashup report. |
||
296 | * |
||
297 | * @param string $dealerMsisdn |
||
298 | * |
||
299 | * @throws Exception |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | View Code Duplication | public function cashupToday($dealerMsisdn) |
|
329 | |||
330 | /** |
||
331 | * Authenticate and request to transfer funds from one Smartload account to another. |
||
332 | * |
||
333 | * @param string $fromDealerMsisdn |
||
334 | * @param string $toDealerMsisdn |
||
335 | * @param string $amount |
||
336 | * @param string $sendSms |
||
337 | * |
||
338 | * @throws Exception |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | View Code Duplication | public function fundstransfer($fromDealerMsisdn, $toDealerMsisdn, $amount, $sendSms) |
|
371 | |||
372 | /** |
||
373 | * Authenticate and retrieves a list of all available networks. |
||
374 | * |
||
375 | * @throws Exception |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | 2 | View Code Duplication | public function network($id) |
405 | |||
406 | /** |
||
407 | * Authenticate and retrieves a list of all available networks. |
||
408 | * |
||
409 | * @throws Exception |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | 2 | View Code Duplication | public function networks() |
436 | |||
437 | /** |
||
438 | * Test SmartCall is responding. |
||
439 | * |
||
440 | * @throws Exception |
||
441 | * |
||
442 | * @return array |
||
443 | */ |
||
444 | 1 | public function ping() |
|
462 | |||
463 | /** |
||
464 | * Authenticate and retrieves a list of all available networks. |
||
465 | * |
||
466 | * @throws Exception |
||
467 | * |
||
468 | * @return array |
||
469 | */ |
||
470 | 2 | View Code Duplication | public function products($id) |
496 | |||
497 | /** |
||
498 | * Authenticate and checks if the provided ID (MSISDN) is registered with Smartload. |
||
499 | * |
||
500 | * @param string $dealerMsisdn |
||
501 | * |
||
502 | * @throws Exception |
||
503 | * |
||
504 | * @return array |
||
505 | */ |
||
506 | View Code Duplication | public function registered($dealerMsisdn) |
|
532 | |||
533 | /** |
||
534 | * Parse the java exception that we receive from Smartcall's Tomcat's. |
||
535 | * |
||
536 | * @param \GuzzleHttp\Exception\ClientException $exception |
||
537 | * |
||
538 | * @return array |
||
539 | */ |
||
540 | 9 | View Code Duplication | private function clientError(\GuzzleHttp\Exception\ClientException $exception) |
550 | |||
551 | /** |
||
552 | * Parse the java exception that we receive from Smartcall's Tomcat's. |
||
553 | * |
||
554 | * @param \GuzzleHttp\Exception\ServerException $exception |
||
555 | * |
||
556 | * @return array |
||
557 | */ |
||
558 | View Code Duplication | private function parseError(\GuzzleHttp\Exception\ServerException $exception) |
|
569 | |||
570 | /** |
||
571 | * Use basic authentication header content if bearer token is not set. |
||
572 | * |
||
573 | * @param string $username |
||
574 | * @param string $password |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | 17 | private function bearerOrBasic($username = null, $password = null) |
|
610 | } |
||
611 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.