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() |
||
| 40 | |||
| 41 | public function getVocabIds() |
||
| 53 | |||
| 54 | public function setVocabularies($vocabs) |
||
| 58 | |||
| 59 | public function getArrayClass() |
||
| 66 | |||
| 67 | public function getSearchTerm() |
||
| 75 | |||
| 76 | public function getContentLang() |
||
| 80 | |||
| 81 | public function getConceptGroups() |
||
| 85 | |||
| 86 | public function getSearchLang() |
||
| 93 | |||
| 94 | public function getTypeLimit() |
||
| 108 | |||
| 109 | public function getGroupLimit() |
||
| 113 | |||
| 114 | public function getParentLimit() |
||
| 118 | |||
| 119 | public function getOffset() |
||
| 123 | |||
| 124 | public function getSearchLimit() |
||
| 128 | |||
| 129 | public function getUnique() { |
||
| 132 | |||
| 133 | public function setUnique($unique) { |
||
| 136 | |||
| 137 | public function getAdditionalFields() { |
||
| 140 | |||
| 141 | public function getHidden() { |
||
| 144 | |||
| 145 | public function setHidden($hidden) { |
||
| 148 | |||
| 149 | public function setAdditionalFields($fields) { |
||
| 152 | |||
| 153 | } |
||
| 154 |
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.