Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | public $metaTags = []; |
||
53 | |||
54 | /** @var array */ |
||
55 | public $subTags = []; |
||
56 | |||
57 | /** @var array */ |
||
58 | private $options; |
||
59 | |||
60 | /** @var array */ |
||
61 | private $parts = []; |
||
62 | |||
63 | /** @var array */ |
||
64 | private $fields = []; |
||
65 | |||
66 | /** @var array */ |
||
67 | private $limitFields = []; |
||
68 | |||
69 | /** @var array */ |
||
70 | private $wildcardFields = []; |
||
71 | |||
72 | // /** @var array */ |
||
73 | // private $wildcardQueries = []; |
||
74 | |||
75 | /** @var array */ |
||
76 | private $wildcardFilters = []; |
||
77 | |||
78 | /** @var array */ |
||
79 | private $regexFilters = []; |
||
80 | |||
81 | |||
82 | /** |
||
83 | * SearchRequest constructor. |
||
84 | */ |
||
85 | public function __construct() { |
||
87 | |||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getProviders() { |
||
95 | |||
96 | /** |
||
97 | * @param array $providers |
||
98 | */ |
||
99 | public function setProviders($providers) { |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | public function getAuthor() { |
||
110 | |||
111 | /** |
||
112 | * @param string $author |
||
113 | */ |
||
114 | public function setAuthor($author) { |
||
117 | |||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getSearch() { |
||
125 | |||
126 | /** |
||
127 | * @param string $search |
||
128 | */ |
||
129 | public function setSearch($search) { |
||
132 | |||
133 | |||
134 | /** |
||
135 | * |
||
136 | */ |
||
137 | public function cleanSearch() { |
||
152 | |||
153 | |||
154 | /** |
||
155 | * @param $word |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | private function searchQueryOptions($word) { |
||
182 | |||
183 | |||
184 | /** |
||
185 | * @return int |
||
186 | */ |
||
187 | public function getPage() { |
||
190 | |||
191 | /** |
||
192 | * @param int $page |
||
193 | */ |
||
194 | public function setPage($page) { |
||
201 | |||
202 | |||
203 | /** |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getSize() { |
||
209 | |||
210 | /** |
||
211 | * @param int $size |
||
212 | */ |
||
213 | public function setSize($size) { |
||
216 | |||
217 | |||
218 | /** |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getOptions() { |
||
224 | |||
225 | /** |
||
226 | * @param array $options |
||
227 | */ |
||
228 | public function setOptions($options) { |
||
231 | |||
232 | /** |
||
233 | * @param $key |
||
234 | * @param $value |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function addOption($key, $value) { |
||
243 | |||
244 | /** |
||
245 | * @param string $k |
||
246 | * @param array $array |
||
247 | * |
||
248 | * @return SearchRequest |
||
249 | */ |
||
250 | public function addOptionArray($k, $array) { |
||
255 | |||
256 | /** |
||
257 | * @param string $k |
||
258 | * @param bool $bool |
||
259 | * |
||
260 | * @return SearchRequest |
||
261 | */ |
||
262 | public function addOptionBool($k, $bool) { |
||
267 | |||
268 | /** |
||
269 | * @param $key |
||
270 | * @param $value |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function addMultipleOption($key, $value) { |
||
283 | |||
284 | /** |
||
285 | * @param string $option |
||
286 | * @param string $default |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getOption($option, $default = '') { |
||
297 | |||
298 | |||
299 | /** |
||
300 | * @param string $option |
||
301 | * @param array $default |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | View Code Duplication | public function getOptionArray($option, $default = []) { |
|
315 | |||
316 | |||
317 | /** |
||
318 | * @param array $parts |
||
319 | * |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function setParts($parts) { |
||
327 | |||
328 | /** |
||
329 | * @return array |
||
330 | */ |
||
331 | public function getParts() { |
||
334 | |||
335 | |||
336 | /** |
||
337 | * @return array |
||
338 | */ |
||
339 | public function getFields() { |
||
342 | |||
343 | /** |
||
344 | * @param array $fields |
||
345 | * |
||
346 | * @return $this |
||
347 | */ |
||
348 | public function setFields($fields) { |
||
353 | |||
354 | |||
355 | /** |
||
356 | * @param $field |
||
357 | * |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function limitToField($field) { |
||
365 | |||
366 | /** |
||
367 | * @return array |
||
368 | */ |
||
369 | public function getLimitFields() { |
||
372 | |||
373 | |||
374 | /** |
||
375 | * @param $field |
||
376 | * |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function addField($field) { |
||
384 | |||
385 | |||
386 | /** |
||
387 | * @param string $tag |
||
388 | */ |
||
389 | public function addTag($tag) { |
||
392 | |||
393 | /** |
||
394 | * @return array |
||
395 | */ |
||
396 | public function getTags() { |
||
399 | |||
400 | /** |
||
401 | * @param array $tags |
||
402 | */ |
||
403 | public function setTags($tags) { |
||
406 | |||
407 | |||
408 | /** |
||
409 | * @param array $tags |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function setMetaTags($tags) { |
||
418 | |||
419 | /** |
||
420 | * @return array |
||
421 | */ |
||
422 | public function getMetaTags() { |
||
425 | |||
426 | /** |
||
427 | * @param string $tags |
||
428 | * |
||
429 | * @return $this |
||
430 | */ |
||
431 | public function addMetaTag($tags) { |
||
436 | |||
437 | |||
438 | /** |
||
439 | * @param array $tags |
||
440 | * |
||
441 | * @return $this |
||
442 | */ |
||
443 | public function setSubTags($tags) { |
||
448 | |||
449 | /** |
||
450 | * @param bool $formatted |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | View Code Duplication | public function getSubTags($formatted = false) { |
|
470 | |||
471 | /** |
||
472 | * @param string $source |
||
473 | * @param string $tag |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | public function addSubTag($source, $tag) { |
||
486 | |||
487 | |||
488 | /** |
||
489 | * @param string $field |
||
490 | * |
||
491 | * @return $this |
||
492 | */ |
||
493 | public function addWildcardField($field) { |
||
498 | |||
499 | |||
500 | /** |
||
501 | * @return array |
||
502 | */ |
||
503 | public function getWildcardFields() { |
||
506 | |||
507 | // |
||
508 | // /** |
||
509 | // * @param array $query |
||
510 | // * |
||
511 | // * @return $this |
||
512 | // */ |
||
513 | // public function addWildcardQuery($query) { |
||
514 | // $this->addWildcardQueries([$query]); |
||
515 | // |
||
516 | // return $this; |
||
517 | // } |
||
518 | // |
||
519 | // /** |
||
520 | // * @param array $query |
||
521 | // * |
||
522 | // * @return $this |
||
523 | // */ |
||
524 | // public function addWildcardQueries($query) { |
||
525 | // array_push($this->wildcardQueries, $query); |
||
526 | // |
||
527 | // return $this; |
||
528 | // } |
||
529 | // |
||
530 | // /** |
||
531 | // * @return array |
||
532 | // */ |
||
533 | // public function getWildcardQueries() { |
||
534 | // return $this->wildcardQueries; |
||
535 | // } |
||
536 | |||
537 | |||
538 | /** |
||
539 | * @param array $filter |
||
540 | * |
||
541 | * @return $this |
||
542 | */ |
||
543 | public function addWildcardFilter($filter) { |
||
548 | |||
549 | /** |
||
550 | * @param array $filters |
||
551 | * |
||
552 | * @return $this |
||
553 | */ |
||
554 | public function addWildcardFilters($filters) { |
||
559 | |||
560 | /** |
||
561 | * @return array |
||
562 | */ |
||
563 | public function getWildcardFilters() { |
||
566 | |||
567 | |||
568 | /** |
||
569 | * @param array $filter |
||
570 | * |
||
571 | * @return $this |
||
572 | */ |
||
573 | public function addRegexFilter($filter) { |
||
578 | |||
579 | /** |
||
580 | * @param array $filters |
||
581 | * |
||
582 | * @return $this |
||
583 | */ |
||
584 | public function addRegexFilters($filters) { |
||
589 | |||
590 | /** |
||
591 | * @return array |
||
592 | */ |
||
593 | public function getRegexFilters() { |
||
596 | |||
597 | |||
598 | /** |
||
599 | * @return array |
||
600 | */ |
||
601 | public function jsonSerialize() { |
||
615 | |||
616 | |||
617 | /** |
||
618 | * @param string $json |
||
619 | * |
||
620 | * @return SearchRequest |
||
621 | */ |
||
622 | public static function fromJSON($json) { |
||
625 | |||
626 | /** |
||
627 | * @param array $arr |
||
628 | * |
||
629 | * @return SearchRequest |
||
630 | */ |
||
631 | public static function fromArray($arr) { |
||
651 | |||
652 | |||
653 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.