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 $wildcardFields = []; |
||
| 62 | |||
| 63 | /** @var array */ |
||
| 64 | private $wildcardQueries = []; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | private $wildcardFilters = []; |
||
| 68 | |||
| 69 | /** @var array */ |
||
| 70 | private $regexFilters = []; |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * SearchRequest constructor. |
||
| 75 | */ |
||
| 76 | public function __construct() { |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function getProviders() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string|array $providers |
||
| 89 | */ |
||
| 90 | public function setProviders($providers) { |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getAuthor() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $author |
||
| 108 | */ |
||
| 109 | public function setAuthor($author) { |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getSearch() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $search |
||
| 123 | */ |
||
| 124 | public function setSearch($search) { |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * |
||
| 131 | */ |
||
| 132 | public function cleanSearch() { |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $word |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | private function searchQueryOptions($word) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | public function getPage() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param int $page |
||
| 181 | */ |
||
| 182 | public function setPage($page) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | public function getSize() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $size |
||
| 200 | */ |
||
| 201 | public function setSize($size) { |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function getOptions() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param array $options |
||
| 215 | */ |
||
| 216 | public function setOptions($options) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $key |
||
| 222 | * @param $value |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function addOption($key, $value) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $option |
||
| 234 | * |
||
| 235 | * @return mixed|string |
||
| 236 | */ |
||
| 237 | public function getOption($option) { |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param array $parts |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function setParts($parts) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getParts() { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | public function getFields() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param array $fields |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function setFields($fields) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param $field |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function addField($field) { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $tag |
||
| 297 | */ |
||
| 298 | public function addTag($tag) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function getTags() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param array $tags |
||
| 311 | */ |
||
| 312 | public function setTags($tags) { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param array $query |
||
|
|
|||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function addWildcardField($field) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public function getWildcardFields() { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param array $query |
||
| 338 | * |
||
| 339 | * @return $this |
||
| 340 | */ |
||
| 341 | public function addWildcardQuery($field) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param array $query |
||
| 349 | * |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | public function addWildcardQueries($query) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public function getWildcardQueries() { |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * @param array $filter |
||
| 368 | * |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function addWildcardFilter($filter) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param array $filters |
||
| 379 | * |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function addWildcardFilters($filters) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public function getWildcardFilters() { |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * @param array $filter |
||
| 398 | * |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | public function addRegexFilter($filter) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $filters |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | public function addRegexFilters($filters) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function getRegexFilters() { |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function jsonSerialize() { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $json |
||
| 445 | * |
||
| 446 | * @return SearchRequest |
||
| 447 | */ |
||
| 448 | public static function fromJSON($json) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param array $arr |
||
| 454 | * |
||
| 455 | * @return SearchRequest |
||
| 456 | */ |
||
| 457 | public static function fromArray($arr) { |
||
| 470 | |||
| 471 | |||
| 472 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.