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:
1 | <?php |
||
29 | class CompositeSearch extends AbstractSearch |
||
30 | { |
||
31 | /** |
||
32 | * The list of search providers. |
||
33 | * |
||
34 | * @var SearchInterface[] |
||
35 | */ |
||
36 | protected $searchers; |
||
37 | |||
38 | /** |
||
39 | * Create a new instance. |
||
40 | * |
||
41 | * @param array $searchers The list of search providers. |
||
42 | */ |
||
43 | public function __construct(array $searchers) |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | View Code Duplication | public function search($keywords, $filters = []) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | View Code Duplication | public function searchAndDecorate($keywords, $filters = []) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function searchFully($keywords, $filters = []) |
||
106 | |||
107 | /** |
||
108 | * Retrieve the list of search providers. |
||
109 | * |
||
110 | * @return SearchInterface[] |
||
111 | */ |
||
112 | public function getSearchers() |
||
116 | |||
117 | /** |
||
118 | * Add a search providers, |
||
119 | * |
||
120 | * @param SearchInterface $searcher The provider to add. |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function addSearcher(SearchInterface $searcher) |
||
138 | |||
139 | /** |
||
140 | * Add the passed search providers to the own list. |
||
141 | * |
||
142 | * @param array $searchers The providers to add. |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function addSearchers(array $searchers) |
||
154 | } |
||
155 |
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.