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 ConceptSearchParameters 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 ConceptSearchParameters, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class ConceptSearchParameters |
||
|
|
|||
| 7 | { |
||
| 8 | |||
| 9 | private $config; |
||
| 10 | private $request; |
||
| 11 | private $vocabs; |
||
| 12 | private $rest; |
||
| 13 | private $hidden; |
||
| 14 | private $unique; |
||
| 15 | |||
| 16 | public function __construct($request, $config, $rest = false) |
||
| 24 | |||
| 25 | public function getLang() |
||
| 32 | |||
| 33 | public function getVocabs() |
||
| 43 | |||
| 44 | public function getVocabIds() |
||
| 60 | |||
| 61 | public function setVocabularies($vocabs) |
||
| 65 | |||
| 66 | public function getArrayClass() |
||
| 74 | |||
| 75 | public function getSearchTerm() |
||
| 83 | |||
| 84 | public function getContentLang() |
||
| 88 | |||
| 89 | public function getSearchLang() |
||
| 96 | |||
| 97 | public function getTypeLimit() |
||
| 117 | |||
| 118 | public function getGroupLimit() |
||
| 122 | |||
| 123 | public function getParentLimit() |
||
| 127 | |||
| 128 | public function getOffset() |
||
| 132 | |||
| 133 | public function getSearchLimit() |
||
| 137 | |||
| 138 | public function getUnique() { |
||
| 141 | |||
| 142 | public function setUnique($unique) { |
||
| 145 | |||
| 146 | public function getAdditionalFields() { |
||
| 149 | |||
| 150 | public function getHidden() { |
||
| 153 | |||
| 154 | public function setHidden($hidden) { |
||
| 157 | |||
| 158 | public function setAdditionalFields($fields) { |
||
| 161 | |||
| 162 | } |
||
| 163 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.