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) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Sets a location on the query for each provider. |
||
| 142 | * |
||
| 143 | * @param $location |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | 4 | public function setLocation($location) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Sets a page number and number of results per page for each provider. |
||
| 198 | * |
||
| 199 | * @param $page integer |
||
| 200 | * @param $perPage integer |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | 2 | public function setPage($page = 1, $perPage = 10) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Instantiates a provider using a query object. |
||
| 253 | * |
||
| 254 | * @param null $name |
||
| 255 | * @param AbstractQuery $query |
||
| 256 | * |
||
| 257 | * @return AbstractProvider |
||
| 258 | */ |
||
| 259 | 2 | public static function createProvider($name = null, AbstractQuery $query) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Gets a start from count for APIs that use per_page and start_from pattern. |
||
| 266 | * |
||
| 267 | * @param int $page |
||
| 268 | * @param int $perPage |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | 2 | private function getStartFrom($page = 1, $perPage = 10) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Tests whether location string follows valid convention (City, ST). |
||
| 279 | * |
||
| 280 | * @param string $location |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 4 | private function isValidLocation($location = null) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Tests whether the method is a valid get<Provider>Jobs() method. |
||
| 292 | * |
||
| 293 | * @param $method |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 2 | private function isGetJobsByProviderMethod($method) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get the provider name from the method. |
||
| 304 | * |
||
| 305 | * @param $method |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | private function getProviderFromMethod($method) |
||
| 314 | } |
||
| 315 |