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 |
||
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 $options array |
||
39 | */ |
||
40 | 11 | public function __construct($options = []) |
|
63 | |||
64 | /** |
||
65 | * Set the bearer token |
||
66 | * |
||
67 | * @param string $token Bearer Token from Auth request |
||
68 | */ |
||
69 | 3 | public function setBearerToken($token) |
|
73 | |||
74 | /** |
||
75 | * Authenticate and get Bearer token from SmartCall |
||
76 | * |
||
77 | * @param string $username |
||
78 | * @param string $password |
||
79 | * |
||
80 | * @throws Exception |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 3 | View Code Duplication | public function auth($username, $password) |
85 | { |
||
86 | try { |
||
87 | 3 | $response = $this->post( |
|
88 | 3 | '/webservice/auth', |
|
89 | [ |
||
90 | 'headers' => [ |
||
91 | 3 | 'Authorization' => sprintf( |
|
92 | 3 | 'Basic %s', |
|
93 | 3 | base64_encode( |
|
94 | 3 | sprintf( |
|
95 | 3 | '%s:%s', |
|
96 | 3 | $username, |
|
97 | 3 | $password |
|
98 | ) |
||
99 | ) |
||
100 | ), |
||
101 | ], |
||
102 | ] |
||
103 | ); |
||
104 | |||
105 | return [ |
||
106 | 1 | 'status' => 'ok', |
|
107 | 1 | 'http_code' => $response->getStatusCode(), |
|
108 | 1 | 'body' => (string) $response->getBody(), |
|
109 | ]; |
||
110 | 2 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
|
111 | return $this->parseError($e); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Authenticate and invalidates all the user allocated tokens |
||
117 | * |
||
118 | * @param string $username |
||
119 | * @param string $password |
||
120 | * |
||
121 | * @throws Exception |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | 2 | public function authDelete() |
|
126 | { |
||
127 | try { |
||
128 | 2 | $response = $this->delete( |
|
129 | 2 | '/webservice/auth', |
|
130 | [ |
||
131 | 'headers' => [ |
||
132 | 2 | 'Authorization' => sprintf( |
|
133 | 2 | 'Bearer %s', |
|
134 | 2 | $this->options['token'] |
|
135 | ), |
||
136 | ], |
||
137 | ] |
||
138 | ); |
||
139 | |||
140 | return [ |
||
141 | 1 | 'status' => 'ok', |
|
142 | 1 | 'http_code' => $response->getStatusCode(), |
|
143 | 1 | 'body' => (string) $response->getBody(), |
|
144 | ]; |
||
145 | 1 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
|
146 | return $this->parseError($e); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Authenticate and invalidates all the user allocated tokens |
||
152 | * |
||
153 | * @param string $username |
||
154 | * @param string $password |
||
155 | * |
||
156 | * @throws Exception |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | View Code Duplication | public function authFlush($username, $password) |
|
190 | |||
191 | /** |
||
192 | * Authenticate and gets the number of available session tokens |
||
193 | * |
||
194 | * @param string $username |
||
195 | * @param string $password |
||
196 | * |
||
197 | * @throws Exception |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 3 | View Code Duplication | public function authToken($username, $password) |
202 | { |
||
203 | try { |
||
204 | 3 | $response = $this->get( |
|
205 | 3 | '/webservice/auth/token', |
|
206 | [ |
||
207 | 'headers' => [ |
||
208 | 3 | 'Authorization' => sprintf( |
|
209 | 3 | 'Basic %s', |
|
210 | 3 | base64_encode( |
|
211 | 3 | sprintf( |
|
212 | 3 | '%s:%s', |
|
213 | 3 | $username, |
|
214 | 3 | $password |
|
215 | ) |
||
216 | ) |
||
217 | ), |
||
218 | ], |
||
219 | ] |
||
220 | ); |
||
221 | |||
222 | return [ |
||
223 | 1 | 'status' => 'ok', |
|
224 | 1 | 'http_code' => $response->getStatusCode(), |
|
225 | 1 | 'body' => (string) $response->getBody(), |
|
226 | ]; |
||
227 | 2 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
|
228 | return $this->parseError($e); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Test SmartCall is responding. |
||
234 | * |
||
235 | * @throws Exception |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | 1 | public function ping() |
|
255 | } |
||
256 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..