Complex classes like JobsMulti 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 JobsMulti, and based on these observations, apply Extract Interface, too.
1 | <?php namespace JobApis\Jobs\Client; |
||
6 | class JobsMulti |
||
7 | { |
||
8 | /** |
||
9 | * Job board API query objects |
||
10 | * |
||
11 | * @var AbstractQuery |
||
12 | */ |
||
13 | protected $queries = []; |
||
14 | |||
15 | /** |
||
16 | * Creates query objects for each provider and creates this unified client. |
||
17 | * |
||
18 | * @param array $providers |
||
19 | */ |
||
20 | 18 | public function __construct($providers = []) |
|
28 | |||
29 | /** |
||
30 | * Overrides get<Provider>Jobs() methods |
||
31 | * |
||
32 | * @param $method |
||
33 | * @param $args |
||
34 | * |
||
35 | * @return mixed |
||
36 | */ |
||
37 | 2 | public function __call($method, $args) |
|
49 | |||
50 | /** |
||
51 | * Instantiates a Query object and adds it to the queries array. |
||
52 | * |
||
53 | * @param $key string Query name |
||
54 | * @param $className string Query class name |
||
55 | * @param $options array Parameters to add to constructor |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | 18 | public function addQuery($key, $className, $options = []) |
|
64 | |||
65 | /** |
||
66 | * Gets jobs from all providers in a single go and returns an array of Collection objects. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function getAllJobs() |
||
78 | |||
79 | /** |
||
80 | * Gets jobs from a single provider and hydrates a new jobs collection. |
||
81 | * |
||
82 | * @return \JobApis\Jobs\Client\Collection |
||
83 | */ |
||
84 | 2 | public function getJobsByProvider($provider) |
|
94 | |||
95 | /** |
||
96 | * Sets a keyword on the query for each provider. |
||
97 | * |
||
98 | * @param $keyword string |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 2 | public function setKeyword($keyword) |
|
151 | |||
152 | /** |
||
153 | * Sets a location on the query for each provider. |
||
154 | * |
||
155 | * @param $location |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | 4 | public function setLocation($location) |
|
218 | |||
219 | /** |
||
220 | * Sets a page number and number of results per page for each provider. |
||
221 | * |
||
222 | * @param $page integer |
||
223 | * @param $perPage integer |
||
224 | * |
||
225 | * @return $this |
||
226 | */ |
||
227 | 2 | public function setPage($page = 1, $perPage = 10) |
|
289 | |||
290 | /** |
||
291 | * Instantiates a provider using a query object. |
||
292 | * |
||
293 | * @param null $name |
||
294 | * @param AbstractQuery $query |
||
295 | * |
||
296 | * @return AbstractProvider |
||
297 | */ |
||
298 | 2 | public static function createProvider($name, AbstractQuery $query) |
|
302 | |||
303 | /** |
||
304 | * Gets a start from count for APIs that use per_page and start_from pattern. |
||
305 | * |
||
306 | * @param int $page |
||
307 | * @param int $perPage |
||
308 | * |
||
309 | * @return int |
||
310 | */ |
||
311 | 2 | private function getStartFrom($page = 1, $perPage = 10) |
|
315 | |||
316 | /** |
||
317 | * Tests whether location string follows valid convention (City, ST). |
||
318 | * |
||
319 | * @param string $location |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 4 | private function isValidLocation($location = null) |
|
328 | |||
329 | /** |
||
330 | * Tests whether the method is a valid get<Provider>Jobs() method. |
||
331 | * |
||
332 | * @param $method |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | 2 | private function isGetJobsByProviderMethod($method) |
|
340 | |||
341 | /** |
||
342 | * Get the provider name from the method. |
||
343 | * |
||
344 | * @param $method |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | private function getProviderFromMethod($method) |
||
353 | } |
||
354 |