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 VocabularyConfig 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 VocabularyConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class VocabularyConfig extends DataObject |
||
|
|
|||
| 7 | { |
||
| 8 | public function __construct($resource) |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 15 | * @param string $property the property to query |
||
| 16 | * @param boolean $default the default value if the value is not set in configuration |
||
| 17 | */ |
||
| 18 | private function getBoolean($property, $default = false) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 30 | * @param string $property the property to query |
||
| 31 | * @param string $lang preferred language for the literal, |
||
| 32 | */ |
||
| 33 | private function getLiteral($property, $lang=null) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the default language of this vocabulary |
||
| 52 | * @return string default language, e.g. 'en' |
||
| 53 | */ |
||
| 54 | |||
| 55 | public function getDefaultLanguage() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Wether the alphabetical index is small enough to be shown all at once. |
||
| 73 | * @return boolean true if all concepts can be shown at once. |
||
| 74 | */ |
||
| 75 | public function getAlphabeticalFull() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
| 82 | * using vocabId as a fallback. |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | public function getShortName() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the vocabulary feedback e-mail address and return it. |
||
| 97 | * |
||
| 98 | * @return string e-mail address or null if not defined. |
||
| 99 | */ |
||
| 100 | public function getFeedbackRecipient() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the human readable vocabulary title. |
||
| 108 | * @return string the title of the vocabulary |
||
| 109 | */ |
||
| 110 | public function getTitle($lang = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 117 | * @return boolean |
||
| 118 | */ |
||
| 119 | public function sortByNotation() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 126 | * @return boolean |
||
| 127 | */ |
||
| 128 | public function showChangeList() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * get the URLs from which the vocabulary data can be downloaded |
||
| 135 | * @return array Array with MIME type as key, URL as value |
||
| 136 | */ |
||
| 137 | public function getDataURLs() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the class URI used for concept groups in this vocabulary, |
||
| 167 | * or null if not set. |
||
| 168 | * @return string group class URI or null |
||
| 169 | */ |
||
| 170 | |||
| 171 | public function getGroupClassURI() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
| 183 | * or null if not set. |
||
| 184 | * @return string array class URI or null |
||
| 185 | */ |
||
| 186 | |||
| 187 | public function getArrayClassURI() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns custom properties displayed on the search page if configured. |
||
| 199 | * @return string array class URI or null |
||
| 200 | */ |
||
| 201 | |||
| 202 | public function getAdditionalSearchProperties() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Queries whether the property should be shown with all the label language variations. |
||
| 220 | * @param string $property |
||
| 221 | * @return boolean |
||
| 222 | */ |
||
| 223 | public function hasMultiLingualProperty($property) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | public function getShowHierarchy() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | public function showConceptSchemesInHierarchy() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 261 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
| 262 | */ |
||
| 263 | public function getExternalResourcesLoading() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 270 | * @return boolean |
||
| 271 | */ |
||
| 272 | public function getShowLangCodes() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 279 | * @return array array of concept class URIs (can be empty) |
||
| 280 | */ |
||
| 281 | public function getIndexClasses() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the languages supported by this vocabulary |
||
| 293 | * @return array languages supported by this vocabulary (as language tag strings) |
||
| 294 | */ |
||
| 295 | public function getLanguages() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the vocabulary default sidebar view. |
||
| 310 | * @return string name of the view |
||
| 311 | */ |
||
| 312 | public function getDefaultSidebarView() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
| 327 | * @return string identifier eg. 'mesh'. |
||
| 328 | */ |
||
| 329 | public function getId() |
||
| 343 | } |
||
| 344 |
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.