Complex classes like AbstractProvider 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 AbstractProvider, and based on these observations, apply Extract Interface, too.
1 | <?php namespace JobApis\Jobs\Client\Providers; |
||
8 | abstract class AbstractProvider |
||
9 | { |
||
10 | use AttributeTrait; |
||
11 | |||
12 | /** |
||
13 | * Base API Url |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $baseUrl; |
||
18 | |||
19 | /** |
||
20 | * HTTP Client |
||
21 | * |
||
22 | * @var HttpClient |
||
23 | */ |
||
24 | protected $client; |
||
25 | |||
26 | /** |
||
27 | * Query params |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $queryParams = []; |
||
32 | |||
33 | /** |
||
34 | * Create new client |
||
35 | * |
||
36 | * @param array $parameters |
||
37 | */ |
||
38 | 14 | public function __construct($parameters = []) |
|
46 | |||
47 | /** |
||
48 | * Get query param as properties, if exists |
||
49 | * |
||
50 | * @param string $name |
||
|
|||
51 | * |
||
52 | * @return mixed |
||
53 | * @throws \OutOfRangeException |
||
54 | */ |
||
55 | 12 | public function __get($key) |
|
67 | |||
68 | /** |
||
69 | * Set query param as properties, if exists |
||
70 | * |
||
71 | * @param string $name |
||
72 | * |
||
73 | * @return mixed |
||
74 | * @throws \OutOfRangeException |
||
75 | */ |
||
76 | 10 | public function __set($key, $value) |
|
89 | |||
90 | /** |
||
91 | * Returns the standardized job object |
||
92 | * |
||
93 | * @param array|object $payload |
||
94 | * |
||
95 | * @return \JobApis\Jobs\Client\Job |
||
96 | */ |
||
97 | abstract public function createJobObject($payload); |
||
98 | |||
99 | /** |
||
100 | * Get default parameters and values |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | abstract public function defaultParameters(); |
||
105 | |||
106 | /** |
||
107 | * Job object default keys that must be set. |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | abstract public function defaultResponseFields(); |
||
112 | |||
113 | /** |
||
114 | * Get listings path |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | abstract public function getListingsPath(); |
||
119 | |||
120 | /** |
||
121 | * Get parameters that MUST be set in order to satisfy the APIs requirements |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | abstract public function requiredParameters(); |
||
126 | |||
127 | /** |
||
128 | * Get parameters that CAN be set |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | abstract public function validParameters(); |
||
133 | |||
134 | // Public methods |
||
135 | |||
136 | /** |
||
137 | * Get format |
||
138 | * |
||
139 | * @return string Currently only 'json' and 'xml' supported |
||
140 | */ |
||
141 | 2 | public function getFormat() |
|
145 | |||
146 | /** |
||
147 | * Get http client options based on current client |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function getHttpClientOptions() |
||
160 | |||
161 | /** |
||
162 | * Makes the api call and returns a collection of job objects |
||
163 | * |
||
164 | * @return JobApis\Jobs\Client\Collection |
||
165 | * @throws MissingParameterException |
||
166 | */ |
||
167 | public function getJobs() |
||
188 | |||
189 | /** |
||
190 | * Get source attribution |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | 2 | public function getSource() |
|
198 | |||
199 | /** |
||
200 | * Get query string for client based on properties |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getQueryString() |
||
208 | |||
209 | /** |
||
210 | * Get url |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getUrl() |
||
221 | |||
222 | /** |
||
223 | * Get http verb to use when making request |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getVerb() |
||
231 | |||
232 | /** |
||
233 | * Check whether a key is valid for this client |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 10 | public function isValidParameter($key = null) |
|
241 | |||
242 | /** |
||
243 | * Parse job attributes against defaults |
||
244 | * |
||
245 | * @param array $attributes |
||
246 | * @param array $defaults |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | 2 | public static function parseAttributeDefaults(array $attributes, array $defaults = array()) |
|
259 | |||
260 | /** |
||
261 | * Parse location string into components. |
||
262 | * |
||
263 | * @param string $location |
||
264 | * |
||
265 | * @return array |
||
266 | **/ |
||
267 | public static function parseLocation($location, $separator = ', ') |
||
271 | |||
272 | /** |
||
273 | * Determines if all required parameters have been set |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 2 | public function requiredParamsIncluded() |
|
286 | |||
287 | /** |
||
288 | * Sets http client |
||
289 | * |
||
290 | * @param HttpClient $client |
||
291 | * |
||
292 | * @return AbstractProvider |
||
293 | */ |
||
294 | 14 | public function setClient(HttpClient $client) |
|
300 | |||
301 | // Protected methods |
||
302 | |||
303 | /** |
||
304 | * Create and get collection of jobs from given listings |
||
305 | * |
||
306 | * @param array $listings |
||
307 | * |
||
308 | * @return Collection |
||
309 | */ |
||
310 | 2 | protected function getJobsCollectionFromListings(array $listings = []) |
|
324 | |||
325 | /** |
||
326 | * Get raw listings from payload |
||
327 | * |
||
328 | * @param array $payload |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | 2 | protected function getRawListings(array $payload = array()) |
|
344 | |||
345 | /** |
||
346 | * Navigate through a payload array looking for a particular index |
||
347 | * |
||
348 | * @param array $index The index sequence we are navigating down |
||
349 | * @param array $value The portion of the config array to process |
||
350 | * |
||
351 | * @return mixed |
||
352 | */ |
||
353 | 2 | protected static function getValue($index, $value) |
|
370 | |||
371 | /** |
||
372 | * Attempt to parse string as given format |
||
373 | * |
||
374 | * @param string $string |
||
375 | * @param string $format |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | 2 | protected function parseAsFormat($string, $format) |
|
389 | |||
390 | /** |
||
391 | * Attempts to update current query parameters. |
||
392 | * |
||
393 | * @param string $value |
||
394 | * @param string $key |
||
395 | * |
||
396 | * @return AbstractProvider |
||
397 | */ |
||
398 | 14 | protected function updateQuery($value, $key) |
|
405 | |||
406 | // Private methods |
||
407 | |||
408 | /** |
||
409 | * Get value current index |
||
410 | * |
||
411 | * @param mixed $index |
||
412 | * |
||
413 | * @return array|null |
||
414 | */ |
||
415 | 2 | private static function getValueCurrentIndex(&$index) |
|
419 | |||
420 | /** |
||
421 | * Checks if given value is an array and that it has contents |
||
422 | * |
||
423 | * @param mixed $array |
||
424 | * |
||
425 | * @return boolean |
||
426 | */ |
||
427 | 2 | private static function isArrayNotEmpty($array) |
|
431 | |||
432 | /** |
||
433 | * Attempt to parse as Json |
||
434 | * |
||
435 | * @param string $string |
||
436 | * |
||
437 | * @return array |
||
438 | */ |
||
439 | 2 | private function parseAsJson($string) |
|
455 | |||
456 | /** |
||
457 | * Attempt to parse as XML |
||
458 | * |
||
459 | * @param string $string |
||
460 | * |
||
461 | * @return array |
||
462 | */ |
||
463 | private function parseAsXml($string) |
||
482 | |||
483 | /** |
||
484 | * Get short name of a given or current class |
||
485 | * |
||
486 | * @param object $object Optional object |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | 2 | private function getShortName($object = null) |
|
500 | } |
||
501 |
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.