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) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Sets a location on the query for each provider. |
||
| 137 | * |
||
| 138 | * @param $location |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | 4 | public function setLocation($location) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Sets a page number and number of results per page for each provider. |
||
| 193 | * |
||
| 194 | * @param $page integer |
||
| 195 | * @param $perPage integer |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | 2 | public function setPage($page = 1, $perPage = 10) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Gets a start from count for APIs that use per_page and start_from pattern. |
||
| 248 | * |
||
| 249 | * @param int $page |
||
| 250 | * @param int $perPage |
||
| 251 | * |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | 2 | private function getStartFrom($page = 1, $perPage = 10) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Tests whether location string follows valid convention (City, ST). |
||
| 261 | * |
||
| 262 | * @param string $location |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 4 | private function isValidLocation($location = null) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Tests whether the method is a valid get<Provider>Jobs() method. |
||
| 274 | * |
||
| 275 | * @param $method |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | 2 | private function isGetJobsByProviderMethod($method) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Get the provider name from the method. |
||
| 286 | * |
||
| 287 | * @param $method |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private function getProviderFromMethod($method) |
||
| 296 | } |
||
| 297 |