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 |
||
11 | class Client |
||
12 | { |
||
13 | protected $guzzleClient; |
||
14 | |||
15 | protected $method = 'get'; |
||
16 | |||
17 | protected $uri = ''; |
||
18 | |||
19 | protected $options = [ ]; |
||
20 | |||
21 | /** |
||
22 | * instantiate Guzzle client (unless one is injected). |
||
23 | * |
||
24 | * @param GuzzleClient $client |
||
25 | * @return void |
||
26 | */ |
||
27 | public function __construct($client = null) |
||
32 | |||
33 | /** |
||
34 | * Setter for request method. |
||
35 | * |
||
36 | * @param string $method |
||
37 | * @return Client |
||
38 | */ |
||
39 | public function setMethod(string $method) |
||
44 | |||
45 | /** |
||
46 | * Setter for request end point URI. |
||
47 | * |
||
48 | * @param string $endPoint |
||
49 | * @return Client |
||
50 | */ |
||
51 | public function setEndPoint(string $endPoint) |
||
56 | |||
57 | /** |
||
58 | * Setter for request headers. |
||
59 | * |
||
60 | * @param array $header |
||
61 | * @return Client |
||
62 | */ |
||
63 | public function addHeader(array $header) |
||
64 | { |
||
65 | $this->options[ 'headers' ] += $header; |
||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Setter for authentication headers. |
||
71 | * |
||
72 | * @param array $headers |
||
73 | * @return Client |
||
74 | */ |
||
75 | public function authHeaders(array $headers = [ ]) |
||
83 | |||
84 | /** |
||
85 | * Setter for request form data. |
||
86 | * |
||
87 | * @param array $formData |
||
88 | * @return Client |
||
89 | */ |
||
90 | public function setFormData(array $formData) |
||
95 | |||
96 | /** |
||
97 | * Setter for request filter(s). mandatory, but can be empty |
||
98 | * |
||
99 | * @param array $filter |
||
100 | * @return Client |
||
101 | */ |
||
102 | public function setFilter($filter = null) |
||
107 | |||
108 | /** |
||
109 | * Setter for marketId(s). |
||
110 | * This param is sometime mandatory, but othertimes breaks the API if an empty value is provided |
||
111 | * |
||
112 | * @param array $marketIds |
||
113 | * @param boolean $mandatory |
||
114 | * @return Client |
||
115 | */ |
||
116 | public function setMarketIds($marketIds = null, $mandatory = false) |
||
123 | |||
124 | /** |
||
125 | * Setter for optional betId(s). |
||
126 | * |
||
127 | * @param array $betIds |
||
128 | * @return Client |
||
129 | */ |
||
130 | public function setBetIds($betIds = null) |
||
137 | |||
138 | /** |
||
139 | * Setter for optional market projection, i.e. what market-related data should be returned. |
||
140 | * |
||
141 | * @param string $name |
||
142 | * @param array $marketProjection |
||
143 | * @return Client |
||
144 | */ |
||
145 | public function setProjection($name, $projection = null) |
||
152 | |||
153 | /** |
||
154 | * Setter for optional request sort. |
||
155 | * |
||
156 | * @param string $sort |
||
157 | * @return Client |
||
158 | */ |
||
159 | public function setSort($sort) |
||
166 | |||
167 | /** |
||
168 | * Setter for mandatory request max results limiter. |
||
169 | * |
||
170 | * @param integer $maxResults |
||
171 | * @return Client |
||
172 | */ |
||
173 | public function setMaxresults($maxResults = 100) |
||
180 | |||
181 | /** |
||
182 | * Setter for optional request locale. |
||
183 | * |
||
184 | * @param string $locale |
||
185 | * @return Client |
||
186 | */ |
||
187 | public function setLocale($locale) |
||
194 | |||
195 | /** |
||
196 | * Setter for mandatory time granularity. |
||
197 | * |
||
198 | * @param string $granularity |
||
199 | * @return Client |
||
200 | */ |
||
201 | public function setTimeGranularity($granularity) |
||
206 | |||
207 | /** |
||
208 | * Setter for optional currency code. |
||
209 | * |
||
210 | * @param string $currencyCode |
||
211 | * @return Client |
||
212 | */ |
||
213 | public function setCurrencyCode($currencyCode) |
||
220 | |||
221 | /** |
||
222 | * Setter for optional flag. |
||
223 | * |
||
224 | * @param string $name |
||
225 | * @param boolean $flag |
||
226 | * @return Client |
||
227 | */ |
||
228 | public function setFlag($name, $flag) |
||
235 | |||
236 | /** |
||
237 | * Setter for date range. |
||
238 | * |
||
239 | * @param string $name |
||
240 | * @param array $range |
||
241 | * @return Client |
||
242 | */ |
||
243 | public function setDateRange($name, $range) |
||
250 | |||
251 | /** |
||
252 | * Setter for sort order. |
||
253 | * |
||
254 | * @param string $by |
||
255 | * @param string $direction |
||
256 | * @return Client |
||
257 | */ |
||
258 | public function setOrder($by, $direction = null) |
||
269 | |||
270 | /** |
||
271 | * Setter for record range. |
||
272 | * |
||
273 | * @param string $from |
||
274 | * @param string $count |
||
275 | * @return Client |
||
276 | */ |
||
277 | public function setRecordRange($from = null, $count = null) |
||
289 | |||
290 | /** |
||
291 | * Setter for optional wallet. |
||
292 | * |
||
293 | * @param string $wallet |
||
294 | * @return Client |
||
295 | */ |
||
296 | public function setwallet($wallet) |
||
303 | |||
304 | /** |
||
305 | * Dispatch the request and provide hooks for error handling for the response. |
||
306 | * |
||
307 | * @return object stdClass |
||
308 | */ |
||
309 | public function send() |
||
323 | |||
324 | /** |
||
325 | * Get status code from http response. |
||
326 | * |
||
327 | * @param Response $response |
||
328 | * @return integer |
||
329 | */ |
||
330 | protected function getStatus(Response $response) |
||
334 | |||
335 | /** |
||
336 | * Get http response body, cast to json and decode. |
||
337 | * |
||
338 | * @param Response $response |
||
339 | * @return array |
||
340 | */ |
||
341 | protected function getBody(Response $response) |
||
345 | |||
346 | /** |
||
347 | * Stub for API exception handling. |
||
348 | * |
||
349 | * @param String $exception |
||
350 | * @return void |
||
351 | */ |
||
352 | protected function handleApiException($exception) { |
||
355 | } |
||
356 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: