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; |
||
5 | class JobsMulti |
||
6 | { |
||
7 | /** |
||
8 | * Job board API query objects |
||
9 | * |
||
10 | * @var AbstractQuery |
||
11 | */ |
||
12 | protected $queries = []; |
||
13 | |||
14 | /** |
||
15 | * Creates query objects for each provider and creates this unified client. |
||
16 | * |
||
17 | * @param array $providers |
||
18 | */ |
||
19 | 14 | public function __construct($providers = []) |
|
27 | |||
28 | /** |
||
29 | * Overrides get<Provider>Jobs() methods |
||
30 | * |
||
31 | * @param $method |
||
32 | * @param $args |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 2 | public function __call($method, $args) |
|
48 | |||
49 | /** |
||
50 | * Instantiates a Query object and adds it to the queries array. |
||
51 | * |
||
52 | * @param $key string Query name |
||
53 | * @param $className string Query class name |
||
54 | * @param $options array Parameters to add to constructor |
||
55 | * |
||
56 | * @return $this |
||
57 | */ |
||
58 | 14 | public function addQuery($key, $className, $options = []) |
|
63 | |||
64 | /** |
||
65 | * Gets jobs from all providers in a single go and returns an array of Collection objects. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | public function getAllJobs() |
||
77 | |||
78 | /** |
||
79 | * Gets jobs from a single provider and hydrates a new jobs collection. |
||
80 | * |
||
81 | * @return \JobApis\Jobs\Client\Collection |
||
82 | */ |
||
83 | public function getJobsByProvider($provider) |
||
89 | |||
90 | /** |
||
91 | * Sets a keyword on the query for each provider. |
||
92 | * |
||
93 | * @param $keyword string |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | 2 | public function setKeyword($keyword) |
|
131 | |||
132 | /** |
||
133 | * Sets a location on the query for each provider. |
||
134 | * |
||
135 | * @param $location |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | 4 | public function setLocation($location) |
|
184 | |||
185 | /** |
||
186 | * Sets a page number and number of results per page for each provider. |
||
187 | * |
||
188 | * @param $page integer |
||
189 | * @param $perPage integer |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | 2 | public function setPage($page = 1, $perPage = 10) |
|
235 | |||
236 | /** |
||
237 | * Gets a start from count for APIs that use per_page and start_from pattern. |
||
238 | * |
||
239 | * @param int $page |
||
240 | * @param int $perPage |
||
241 | * |
||
242 | * @return int |
||
243 | */ |
||
244 | 2 | private function getStartFrom($page = 1, $perPage = 10) |
|
248 | |||
249 | /** |
||
250 | * Tests whether location string follows valid convention (City, ST). |
||
251 | * |
||
252 | * @param string $location |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | 4 | private function isValidLocation($location = null) |
|
261 | |||
262 | /** |
||
263 | * Tests whether the method is a valid get<Provider>Jobs() method. |
||
264 | * |
||
265 | * @param $method |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | 2 | private function isGetJobsByProviderMethod($method) |
|
273 | |||
274 | /** |
||
275 | * Get the provider name from the method. |
||
276 | * |
||
277 | * @param $method |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | private function getProviderFromMethod($method) |
||
286 | } |
||
287 |