Complex classes like SearchRequest 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 SearchRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class SearchRequest implements \JsonSerializable { |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $providers; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | private $search; |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | private $page = 1; |
||
| 41 | |||
| 42 | /** @var int */ |
||
| 43 | private $size = 10; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | private $author; |
||
| 47 | |||
| 48 | /** @var array */ |
||
| 49 | private $tags; |
||
| 50 | |||
| 51 | /** @var array */ |
||
| 52 | private $options; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | private $parts = []; |
||
| 56 | |||
| 57 | /** @var array */ |
||
| 58 | private $fields = []; |
||
| 59 | |||
| 60 | /** @var array */ |
||
| 61 | private $wildcardQueries = []; |
||
| 62 | |||
| 63 | /** @var array */ |
||
| 64 | private $wildcardFilters = []; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | private $regexFilters = []; |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * SearchRequest constructor. |
||
| 72 | */ |
||
| 73 | public function __construct() { |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function getProviders() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string|array $providers |
||
| 86 | */ |
||
| 87 | public function setProviders($providers) { |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function getAuthor() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $author |
||
| 105 | */ |
||
| 106 | public function setAuthor($author) { |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getSearch() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $search |
||
| 120 | */ |
||
| 121 | public function setSearch($search) { |
||
| 124 | |||
| 125 | |||
| 126 | public function cleanSearch() { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $word |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | private function searchQueryOptions($word) { |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * @return int |
||
| 168 | */ |
||
| 169 | public function getPage() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $page |
||
| 175 | */ |
||
| 176 | public function setPage($page) { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | public function getSize() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $size |
||
| 194 | */ |
||
| 195 | public function setSize($size) { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function getOptions() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param array $options |
||
| 209 | */ |
||
| 210 | public function setOptions($options) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param $key |
||
| 216 | * @param $value |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function addOption($key, $value) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $option |
||
| 228 | * |
||
| 229 | * @return mixed|string |
||
| 230 | */ |
||
| 231 | public function getOption($option) { |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param array $parts |
||
| 242 | * |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | public function setParts($parts) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function getParts() { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getFields() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param array $fields |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function setFields($fields) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param $field |
||
| 279 | * |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function addField($field) { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $tag |
||
| 291 | */ |
||
| 292 | public function addTag($tag) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | public function getTags() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param array $tags |
||
| 305 | */ |
||
| 306 | public function setTags($tags) { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param array $query |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function addWildcardQuery($query) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $query |
||
| 324 | * |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | public function addWildcardQueries($query) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function getWildcardQueries() { |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * @param array $filter |
||
| 343 | * |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | public function addWildcardFilter($filter) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param array $filters |
||
| 354 | * |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function addWildcardFilters($filters) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function getWildcardFilters() { |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * @param array $filter |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function addRegexFilter($filter) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param array $filters |
||
| 384 | * |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function addRegexFilters($filters) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | public function getRegexFilters() { |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * @return array |
||
|
|
|||
| 403 | */ |
||
| 404 | public function jsonSerialize() { |
||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * @param string $json |
||
| 420 | * |
||
| 421 | * @return SearchRequest |
||
| 422 | */ |
||
| 423 | public static function fromJSON($json) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param array $arr |
||
| 429 | * |
||
| 430 | * @return SearchRequest |
||
| 431 | */ |
||
| 432 | public static function fromArray($arr) { |
||
| 445 | |||
| 446 | |||
| 447 | } |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.