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) | |
| 125 | |||
| 126 | /** | ||
| 127 | * Sets a location on the query for each provider. | ||
| 128 | * | ||
| 129 | * @param $location | ||
| 130 | * | ||
| 131 | * @return $this | ||
| 132 | */ | ||
| 133 | 4 | public function setLocation($location) | |
| 172 | |||
| 173 | /** | ||
| 174 | * Sets a page number and number of results per page for each provider. | ||
| 175 | * | ||
| 176 | * @param $page integer | ||
| 177 | * @param $perPage integer | ||
| 178 | * | ||
| 179 | * @return $this | ||
| 180 | */ | ||
| 181 | 2 | public function setPage($page = 1, $perPage = 10) | |
| 215 | |||
| 216 | /** | ||
| 217 | * Gets a start from count for APIs that use per_page and start_from pattern. | ||
| 218 | * | ||
| 219 | * @param int $page | ||
| 220 | * @param int $perPage | ||
| 221 | * | ||
| 222 | * @return int | ||
| 223 | */ | ||
| 224 | 2 | private function getStartFrom($page = 1, $perPage = 10) | |
| 228 | |||
| 229 | /** | ||
| 230 | * Tests whether location string follows valid convention (City, ST). | ||
| 231 | * | ||
| 232 | * @param string $location | ||
| 233 | * | ||
| 234 | * @return bool | ||
| 235 | */ | ||
| 236 | 4 | private function isValidLocation($location = null) | |
| 241 | |||
| 242 | /** | ||
| 243 | * Tests whether the method is a valid get<Provider>Jobs() method. | ||
| 244 | * | ||
| 245 | * @param $method | ||
| 246 | * | ||
| 247 | * @return bool | ||
| 248 | */ | ||
| 249 | 2 | private function isGetJobsByProviderMethod($method) | |
| 253 | |||
| 254 | /** | ||
| 255 | * Get the provider name from the method. | ||
| 256 | * | ||
| 257 | * @param $method | ||
| 258 | * | ||
| 259 | * @return string | ||
| 260 | */ | ||
| 261 | private function getProviderFromMethod($method) | ||
| 266 | } | ||
| 267 |