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 $limitFields = []; |
||
| 62 | |||
| 63 | /** @var array */ |
||
| 64 | private $wildcardFields = []; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | private $wildcardQueries = []; |
||
|
|
|||
| 68 | |||
| 69 | /** @var array */ |
||
| 70 | private $wildcardFilters = []; |
||
| 71 | |||
| 72 | /** @var array */ |
||
| 73 | private $regexFilters = []; |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * SearchRequest constructor. |
||
| 78 | */ |
||
| 79 | public function __construct() { |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function getProviders() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string|array $providers |
||
| 92 | */ |
||
| 93 | public function setProviders($providers) { |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getAuthor() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $author |
||
| 111 | */ |
||
| 112 | public function setAuthor($author) { |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getSearch() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $search |
||
| 126 | */ |
||
| 127 | public function setSearch($search) { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | public function cleanSearch() { |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @param $word |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | private function searchQueryOptions($word) { |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function getPage() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param int $page |
||
| 191 | */ |
||
| 192 | public function setPage($page) { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | public function getSize() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param int $size |
||
| 210 | */ |
||
| 211 | public function setSize($size) { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public function getOptions() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param array $options |
||
| 225 | */ |
||
| 226 | public function setOptions($options) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param $key |
||
| 232 | * @param $value |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function addOption($key, $value) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param $key |
||
| 244 | * @param $value |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function addMultipleOption($key, $value) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $option |
||
| 260 | * |
||
| 261 | * @return mixed|string |
||
| 262 | */ |
||
| 263 | public function getOption($option) { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param array $parts |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function setParts($parts) { |
||
| 278 | $this->parts = $parts; |
||
| 279 | |||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getParts() { |
||
| 287 | return $this->parts; |
||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getFields() { |
||
| 295 | return $this->fields; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $fields |
||
| 300 | * |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | public function setFields($fields) { |
||
| 304 | $this->fields = $fields; |
||
| 305 | |||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @param $field |
||
| 312 | * |
||
| 313 | * @return $this |
||
| 314 | */ |
||
| 315 | public function limitToField($field) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function getLimitFields() { |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $field |
||
| 331 | * |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function addField($field) { |
||
| 335 | $this->fields[] = $field; |
||
| 336 | |||
| 337 | return $this; |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $tag |
||
| 343 | */ |
||
| 344 | public function addTag($tag) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public function getTags() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $tags |
||
| 357 | */ |
||
| 358 | public function setTags($tags) { |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $field |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function addWildcardField($field) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public function getWildcardFields() { |
||
| 380 | |||
| 381 | // |
||
| 382 | // /** |
||
| 383 | // * @param array $query |
||
| 384 | // * |
||
| 385 | // * @return $this |
||
| 386 | // */ |
||
| 387 | // public function addWildcardQuery($query) { |
||
| 388 | // $this->addWildcardQueries([$query]); |
||
| 389 | // |
||
| 390 | // return $this; |
||
| 391 | // } |
||
| 392 | // |
||
| 393 | // /** |
||
| 394 | // * @param array $query |
||
| 395 | // * |
||
| 396 | // * @return $this |
||
| 397 | // */ |
||
| 398 | // public function addWildcardQueries($query) { |
||
| 399 | // array_push($this->wildcardQueries, $query); |
||
| 400 | // |
||
| 401 | // return $this; |
||
| 402 | // } |
||
| 403 | // |
||
| 404 | // /** |
||
| 405 | // * @return array |
||
| 406 | // */ |
||
| 407 | // public function getWildcardQueries() { |
||
| 408 | // return $this->wildcardQueries; |
||
| 409 | // } |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * @param array $filter |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function addWildcardFilter($filter) { |
||
| 418 | $this->addWildcardFilters([$filter]); |
||
| 419 | |||
| 420 | return $this; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param array $filters |
||
| 425 | * |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | public function addWildcardFilters($filters) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | public function getWildcardFilters() { |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * @param array $filter |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | public function addRegexFilter($filter) { |
||
| 448 | $this->addRegexFilters([$filter]); |
||
| 449 | |||
| 450 | return $this; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param array $filters |
||
| 455 | * |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function addRegexFilters($filters) { |
||
| 459 | array_push($this->regexFilters, $filters); |
||
| 460 | |||
| 461 | return $this; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getRegexFilters() { |
||
| 468 | return $this->regexFilters; |
||
| 469 | } |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | public function jsonSerialize() { |
||
| 476 | return [ |
||
| 477 | 'providers' => $this->getProviders(), |
||
| 478 | 'author' => $this->getAuthor(), |
||
| 479 | 'search' => $this->getSearch(), |
||
| 480 | 'page' => $this->getPage(), |
||
| 481 | 'size' => $this->getSize(), |
||
| 482 | 'parts' => $this->getParts(), |
||
| 483 | 'options' => $this->getOptions(), |
||
| 484 | 'tags' => $this->getTags() |
||
| 485 | ]; |
||
| 486 | } |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $json |
||
| 491 | * |
||
| 492 | * @return SearchRequest |
||
| 493 | */ |
||
| 494 | public static function fromJSON($json) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param array $arr |
||
| 500 | * |
||
| 501 | * @return SearchRequest |
||
| 502 | */ |
||
| 503 | public static function fromArray($arr) { |
||
| 504 | $request = new SearchRequest(); |
||
| 505 | $request->setProviders($arr['providers']); |
||
| 506 | $request->setAuthor(MiscService::get($arr, 'author', '')); |
||
| 507 | $request->setSearch(MiscService::get($arr, 'search', '')); |
||
| 516 | |||
| 517 | |||
| 518 | } |
This check marks private properties in classes that are never used. Those properties can be removed.