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 | * Search keyword |
||
| 10 | * |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $keyword; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Search location |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $location; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Maximum age of results (in days) |
||
| 24 | * |
||
| 25 | * @var integer |
||
| 26 | */ |
||
| 27 | protected $maxAge; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Maximum number of results to return in all results |
||
| 31 | * |
||
| 32 | * @var integer |
||
| 33 | */ |
||
| 34 | protected $maxResults; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Order of results |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $order = 'desc'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Field to order results by |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $orderBy = 'datePosted'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Results page number |
||
| 52 | * |
||
| 53 | * @var integer |
||
| 54 | */ |
||
| 55 | protected $pageNumber; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Results per page |
||
| 59 | * |
||
| 60 | * @var integer |
||
| 61 | */ |
||
| 62 | protected $perPage; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Job board API providers |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $providers = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Job board API query objects |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $queries = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Creates query objects for each provider and creates this unified client. |
||
| 80 | * |
||
| 81 | * @param array $providers |
||
| 82 | */ |
||
| 83 | 12 | public function __construct($providers = []) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Gets jobs from all providers in a single go and returns a MultiCollection |
||
| 90 | * |
||
| 91 | * @return MultiCollection |
||
| 92 | */ |
||
| 93 | public function getAllJobs($options = []) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Gets jobs from a single provider and hydrates a new jobs collection. |
||
| 126 | * |
||
| 127 | * @var $name string Provider name. |
||
| 128 | * |
||
| 129 | * @return \JobApis\Jobs\Client\Collection |
||
| 130 | */ |
||
| 131 | 2 | public function getJobsByProvider($name = null) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Sets a keyword on the query. |
||
| 149 | * |
||
| 150 | * @param $keyword string |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | 2 | public function setKeyword($keyword = null) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Sets a location on the query for each provider. |
||
| 163 | * |
||
| 164 | * @param $location |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | 4 | public function setLocation($location = null) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the options used for the resulting collection |
||
| 180 | * |
||
| 181 | * @param array $options |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function setOptions($options = []) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets a page number and number of results per page for each provider. |
||
| 205 | * |
||
| 206 | * @param $pageNumber integer |
||
| 207 | * @param $perPage integer |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 2 | public function setPage($pageNumber = 1, $perPage = 10) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Sets an array of providers. |
||
| 221 | * |
||
| 222 | * @param $providers array |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | 12 | public function setProviders($providers = []) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Gets an array of options from a translator array |
||
| 235 | * |
||
| 236 | * @param array $translator |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | protected function getOptionsFromTranslator($translator = []) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the options array based on the provider name. |
||
| 258 | * |
||
| 259 | * @param $name |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | protected function getTranslatorForProvider($name) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Instantiates a provider using a query object. |
||
| 374 | * |
||
| 375 | * @param null $name |
||
| 376 | * @param AbstractQuery $query |
||
| 377 | * |
||
| 378 | * @return AbstractProvider |
||
| 379 | */ |
||
| 380 | protected function instantiateProvider($name, AbstractQuery $query) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Instantiates a query using a client name. |
||
| 389 | * |
||
| 390 | * @param null $name |
||
| 391 | * |
||
| 392 | * @return AbstractQuery |
||
| 393 | */ |
||
| 394 | 2 | protected function instantiateQuery($name) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Get the city and state as an array from a location string. |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | private function getCityAndState() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Gets a from value. |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | private function getFrom() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Gets page number minus 1. |
||
| 440 | * |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | private function getPageMinusOne() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get the provider name from the method. |
||
| 455 | * |
||
| 456 | * @param $method |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | private function getProviderFromMethod($method) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the query with keyword and location. |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | private function getQueryWithKeywordAndLocation() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Gets a start at value. |
||
| 486 | * |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | private function getStart() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Tests whether location string follows valid convention (City, ST). |
||
| 501 | * |
||
| 502 | * @param string $location |
||
| 503 | * |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | 4 | private function isValidLocation($location = null) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Tests whether the method is a valid get<Provider>Jobs() method. |
||
| 514 | * |
||
| 515 | * @param $method |
||
| 516 | * |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | private function isGetJobsByProviderMethod($method) |
||
| 523 | } |
||
| 524 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: