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 | 20 | public function __construct($providers = []) |
|
| 84 | { |
||
| 85 | 20 | $this->setProviders($providers); |
|
| 86 | 20 | } |
|
| 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 | 8 | 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 | 8 | public function setKeyword($keyword = null) |
|
| 142 | { |
||
| 143 | 8 | $this->keyword = $keyword; |
|
| 144 | |||
| 145 | 8 | return $this; |
|
| 146 | } |
||
| 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) |
|
| 156 | { |
||
| 157 | 8 | if (!$this->isValidLocation($location)) { |
|
| 158 | 2 | throw new \OutOfBoundsException("Location parameter must follow the pattern 'City, ST'."); |
|
| 159 | } |
||
| 160 | 6 | $this->location = $location; |
|
| 161 | |||
| 162 | 6 | return $this; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Sets the options used for the resulting collection |
||
| 167 | * |
||
| 168 | * @param array $options |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 10 | public function setOptions($options = []) |
|
| 173 | { |
||
| 174 | 10 | if (isset($options['maxAge'])) { |
|
| 175 | 4 | $this->maxAge = $options['maxAge']; |
|
| 176 | 4 | } |
|
| 177 | 10 | if (isset($options['maxResults'])) { |
|
| 178 | 4 | $this->maxResults = $options['maxResults']; |
|
| 179 | 4 | } |
|
| 180 | 10 | if (isset($options['order'])) { |
|
| 181 | 4 | $this->order = $options['order']; |
|
| 182 | 4 | } |
|
| 183 | 10 | if (isset($options['orderBy'])) { |
|
| 184 | 4 | $this->orderBy = $options['orderBy']; |
|
| 185 | 4 | } |
|
| 186 | |||
| 187 | 10 | return $this; |
|
| 188 | } |
||
| 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) |
|
| 199 | { |
||
| 200 | 6 | $this->pageNumber = $pageNumber; |
|
| 201 | 6 | $this->perPage = $perPage; |
|
| 202 | |||
| 203 | 6 | return $this; |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Sets an array of providers. |
||
| 208 | * |
||
| 209 | * @param $providers array |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 20 | 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 | 6 | protected function applyOptions(Collection $collection) |
|
| 228 | { |
||
| 229 | // Order the results |
||
| 230 | 6 | if ($this->orderBy && $this->order) { |
|
| 231 | 2 | $collection->orderBy($this->orderBy, $this->order); |
|
| 232 | 2 | } |
|
| 233 | |||
| 234 | // Filter older listings out |
||
| 235 | 6 | if ($this->maxAge) { |
|
| 236 | 2 | $collection->filter( |
|
| 237 | 2 | 'datePosted', |
|
| 238 | 2 | new \DateTime($this->maxAge.' days ago'), |
|
|
|
|||
| 239 | '>' |
||
| 240 | 2 | ); |
|
| 241 | 2 | } |
|
| 242 | |||
| 243 | // Truncate to the maximum results (all by default) |
||
| 244 | 6 | if ($this->maxResults) { |
|
| 245 | 2 | $collection->truncate($this->maxResults); |
|
| 246 | 2 | } |
|
| 247 | |||
| 248 | 6 | return $collection; |
|
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Gets an array of options from a translator array |
||
| 253 | * |
||
| 254 | * @param array $translator |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 6 | 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 | 6 | 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 | 6 | 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 | 8 | 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 | 2 | private function getCityAndState() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Gets a from value. |
||
| 443 | * |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | 3 | private function getFrom() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Gets page number minus 1. |
||
| 458 | * |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | 2 | private function getPageMinusOne() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get the query with keyword and location. |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | 3 | 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: