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; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Field to order results by |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $orderBy; |
||
| 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 | 16 | public function __construct($providers = []) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Gets jobs from all providers in a single go and returns a MultiCollection |
||
| 90 | * |
||
| 91 | * @return Collection |
||
| 92 | */ |
||
| 93 | 2 | public function getAllJobs($options = []) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Gets jobs from a single provider and hydrates a new jobs collection. |
||
| 110 | * |
||
| 111 | * @var $name string Provider name. |
||
| 112 | * |
||
| 113 | * @return \JobApis\Jobs\Client\Collection |
||
| 114 | */ |
||
| 115 | 6 | public function getJobsByProvider($name = null, $options = []) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Sets a keyword on the query. |
||
| 136 | * |
||
| 137 | * @param $keyword string |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | 6 | public function setKeyword($keyword = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Sets a location on the query for each provider. |
||
| 150 | * |
||
| 151 | * @param $location |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | 8 | public function setLocation($location = null) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Sets the options used for the resulting collection |
||
| 167 | * |
||
| 168 | * @param array $options |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 6 | public function setOptions($options = []) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Sets a page number and number of results per page for each provider. |
||
| 192 | * |
||
| 193 | * @param $pageNumber integer |
||
| 194 | * @param $perPage integer |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | 6 | public function setPage($pageNumber = 1, $perPage = 10) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Sets an array of providers. |
||
| 208 | * |
||
| 209 | * @param $providers array |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 16 | public function setProviders($providers = []) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Apply the options for this JobsMulti object to the Collection |
||
| 222 | * |
||
| 223 | * @param Collection $collection |
||
| 224 | * |
||
| 225 | * @return Collection |
||
| 226 | */ |
||
| 227 | 4 | protected function applyOptions(Collection $collection) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Gets an array of options from a translator array |
||
| 253 | * |
||
| 254 | * @param array $translator |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 4 | protected function getOptionsFromTranslator($translator = []) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Gets the options array based on the provider name. |
||
| 276 | * |
||
| 277 | * @param $name |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 4 | protected function getTranslatorForProvider($name) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Instantiates a provider using a query object. |
||
| 392 | * |
||
| 393 | * @param null $name |
||
| 394 | * @param AbstractQuery $query |
||
| 395 | * |
||
| 396 | * @return AbstractProvider |
||
| 397 | */ |
||
| 398 | 4 | protected function instantiateProvider($name, AbstractQuery $query) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Instantiates a query using a client name. |
||
| 407 | * |
||
| 408 | * @param null $name |
||
| 409 | * |
||
| 410 | * @return AbstractQuery |
||
| 411 | */ |
||
| 412 | 6 | protected function instantiateQuery($name) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Get the city and state as an array from a location string. |
||
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | 3 | private function getCityAndState() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Gets a from value. |
||
| 443 | * |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | 2 | private function getFrom() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Gets page number minus 1. |
||
| 458 | * |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | 3 | private function getPageMinusOne() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get the query with keyword and location. |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | 2 | private function getQueryWithKeywordAndLocation() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Gets a start at value. |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | private function getStart() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Tests whether location string follows valid convention (City, ST). |
||
| 506 | * |
||
| 507 | * @param string $location |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | 8 | private function isValidLocation($location = null) |
|
| 516 | } |
||
| 517 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: