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) { |
||
282 | |||
283 | /** |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getParts() { |
||
289 | |||
290 | |||
291 | /** |
||
292 | * @return array |
||
293 | */ |
||
294 | public function getFields() { |
||
297 | |||
298 | /** |
||
299 | * @param array $fields |
||
300 | * |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function setFields($fields) { |
||
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) { |
||
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 array $query |
||
|
|||
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($field) { |
||
392 | |||
393 | /** |
||
394 | * @param array $query |
||
395 | * |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function addWildcardQueries($query) { |
||
403 | |||
404 | /** |
||
405 | * @return array |
||
406 | */ |
||
407 | public function getWildcardQueries() { |
||
410 | |||
411 | |||
412 | /** |
||
413 | * @param array $filter |
||
414 | * |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function addWildcardFilter($filter) { |
||
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) { |
||
452 | |||
453 | /** |
||
454 | * @param array $filters |
||
455 | * |
||
456 | * @return $this |
||
457 | */ |
||
458 | public function addRegexFilters($filters) { |
||
463 | |||
464 | /** |
||
465 | * @return array |
||
466 | */ |
||
467 | public function getRegexFilters() { |
||
470 | |||
471 | |||
472 | /** |
||
473 | * @return array |
||
474 | */ |
||
475 | public function jsonSerialize() { |
||
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) { |
||
516 | |||
517 | |||
518 | } |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.