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:
1 | <?php |
||
18 | class Client extends \GuzzleHttp\Client |
||
19 | { |
||
20 | use SmartLoad; |
||
21 | use SmartRica; |
||
22 | |||
23 | /** |
||
24 | * @const string Version number |
||
25 | */ |
||
26 | const VERSION = '0.0.1'; |
||
27 | |||
28 | /** |
||
29 | * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
||
30 | * (127.0.0.1). |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $options = [ |
||
35 | 'scheme' => 'https', |
||
36 | 'hostname' => 'localhost', |
||
37 | 'port' => '8080', |
||
38 | 'token' => null, |
||
39 | 'username' => null, |
||
40 | 'password' => null, |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * @param array $options |
||
45 | */ |
||
46 | 22 | public function __construct($options = []) |
|
69 | |||
70 | /** |
||
71 | * Set the bearer token. |
||
72 | * |
||
73 | * @param string $token Bearer Token from Auth request |
||
74 | */ |
||
75 | 11 | public function setBearerToken($token) |
|
79 | |||
80 | /** |
||
81 | * Set the password for basic authentication. |
||
82 | * |
||
83 | * @param string $password Password for use with basic authentication |
||
84 | */ |
||
85 | 1 | public function setPassword($password) |
|
89 | |||
90 | /** |
||
91 | * Set the username for basic authentication. |
||
92 | * |
||
93 | * @param string $username Username for use with basic authentication |
||
94 | */ |
||
95 | 1 | public function setUsername($username) |
|
99 | |||
100 | /** |
||
101 | * Authenticate and get Bearer token from SmartCall. |
||
102 | * |
||
103 | * @throws Exception |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | 3 | View Code Duplication | public function auth() |
130 | |||
131 | /** |
||
132 | * Authenticate and invalidates all the user allocated tokens. |
||
133 | * |
||
134 | * @throws Exception |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | 2 | View Code Duplication | public function authDelete() |
161 | |||
162 | /** |
||
163 | * Authenticate and invalidates all the user allocated tokens. |
||
164 | * |
||
165 | * @throws Exception |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | 3 | View Code Duplication | public function authFlush() |
192 | |||
193 | /** |
||
194 | * Authenticate and gets the number of available session tokens. |
||
195 | * |
||
196 | * @throws Exception |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 3 | View Code Duplication | public function authToken() |
223 | |||
224 | /** |
||
225 | * Test SmartCall is responding. |
||
226 | * |
||
227 | * @throws Exception |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 1 | public function ping() |
|
249 | |||
250 | /** |
||
251 | * Parse the java exception that we receive from Smartcall's Tomcat's. |
||
252 | * |
||
253 | * @param \GuzzleHttp\Exception\ClientException $exception |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 9 | View Code Duplication | private function clientError(\GuzzleHttp\Exception\ClientException $exception) |
267 | |||
268 | /** |
||
269 | * Parse the java exception that we receive from Smartcall's Tomcat's. |
||
270 | * |
||
271 | * @param \GuzzleHttp\Exception\ServerException $exception |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | View Code Duplication | private function parseError(\GuzzleHttp\Exception\ServerException $exception) |
|
286 | |||
287 | /** |
||
288 | * Use basic authentication header content if bearer token is not set. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | 17 | private function bearerOrBasic() |
|
324 | } |
||
325 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.