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 | private $plugins; |
||
| 9 | |||
| 10 | public function __construct($resource, $globalPlugins=array()) |
||
| 11 | { |
||
| 12 | $this->resource = $resource; |
||
| 13 | $plugins = $this->resource->allLiterals('skosmos:usePlugin'); |
||
| 14 | $pluginArray = array(); |
||
| 15 | if ($plugins) { |
||
| 16 | foreach ($plugins as $pluginlit) { |
||
| 17 | $pluginArray[] = $pluginlit->getValue(); |
||
| 18 | } |
||
| 19 | } |
||
| 20 | $this->plugins = new PluginRegister(array_merge($globalPlugins, $pluginArray)); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 25 | * @param string $property the property to query |
||
| 26 | * @param boolean $default the default value if the value is not set in configuration |
||
| 27 | */ |
||
| 28 | private function getBoolean($property, $default = false) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns an array of URIs based on a property from the vocabularies.ttl configuration. |
||
| 40 | * @param string $property the property to query |
||
| 41 | * @return string[] List of URIs |
||
| 42 | */ |
||
| 43 | private function getResources($property) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 56 | * @param string $property the property to query |
||
| 57 | * @param string $lang preferred language for the literal, |
||
| 58 | */ |
||
| 59 | private function getLiteral($property, $lang=null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the default language of this vocabulary |
||
| 78 | * @return string default language, e.g. 'en' |
||
| 79 | */ |
||
| 80 | |||
| 81 | public function getDefaultLanguage() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Wether the alphabetical index is small enough to be shown all at once. |
||
| 99 | * @return boolean true if all concepts can be shown at once. |
||
| 100 | */ |
||
| 101 | public function getAlphabeticalFull() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
| 108 | * using vocabId as a fallback. |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getShortName() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the vocabulary feedback e-mail address and return it. |
||
| 123 | * |
||
| 124 | * @return string e-mail address or null if not defined. |
||
| 125 | */ |
||
| 126 | public function getFeedbackRecipient() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the human readable vocabulary title. |
||
| 134 | * @return string the title of the vocabulary |
||
| 135 | */ |
||
| 136 | public function getTitle($lang = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 143 | * @return boolean |
||
| 144 | */ |
||
| 145 | public function sortByNotation() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 152 | * @return boolean |
||
| 153 | */ |
||
| 154 | public function showChangeList() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * get the URLs from which the vocabulary data can be downloaded |
||
| 161 | * @return array Array with MIME type as key, URL as value |
||
| 162 | */ |
||
| 163 | public function getDataURLs() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the main Concept Scheme URI of that Vocabulary, |
||
| 193 | * or null if not set. |
||
| 194 | * @return string concept scheme URI or null |
||
| 195 | */ |
||
| 196 | |||
| 197 | public function getMainConceptSchemeURI() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the class URI used for concept groups in this vocabulary, |
||
| 209 | * or null if not set. |
||
| 210 | * @return string group class URI or null |
||
| 211 | */ |
||
| 212 | |||
| 213 | public function getGroupClassURI() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
| 225 | * or null if not set. |
||
| 226 | * @return string array class URI or null |
||
| 227 | */ |
||
| 228 | |||
| 229 | public function getArrayClassURI() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns custom properties displayed on the search page if configured. |
||
| 241 | * @return string array class URI or null |
||
| 242 | */ |
||
| 243 | |||
| 244 | View Code Duplication | public function getAdditionalSearchProperties() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Queries whether the property should be shown with all the label language variations. |
||
| 262 | * @param string $property |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | public function hasMultiLingualProperty($property) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 285 | * @return boolean |
||
| 286 | */ |
||
| 287 | public function getShowHierarchy() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | public function showConceptSchemesInHierarchy() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 303 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
| 304 | */ |
||
| 305 | public function getExternalResourcesLoading() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 312 | * @return boolean |
||
| 313 | */ |
||
| 314 | public function getShowLangCodes() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 321 | * @return array array of concept class URIs (can be empty) |
||
| 322 | */ |
||
| 323 | public function getIndexClasses() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Returns skosmos:externalProperty values set in the vocabularies.ttl config. |
||
| 330 | * @return array array of external property URIs (can be empty) |
||
| 331 | */ |
||
| 332 | public function getExtProperties() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the languages supported by this vocabulary |
||
| 339 | * @return array languages supported by this vocabulary (as language tag strings) |
||
| 340 | */ |
||
| 341 | public function getLanguages() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns the vocabulary default sidebar view. |
||
| 356 | * @return string name of the view |
||
| 357 | */ |
||
| 358 | public function getDefaultSidebarView() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
| 380 | * @return string identifier eg. 'mesh'. |
||
| 381 | */ |
||
| 382 | public function getId() |
||
| 396 | |||
| 397 | public function getShowStatistics() { |
||
| 400 | |||
| 401 | public function getPlugins() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns the property/properties used for visualizing concept hierarchies. |
||
| 408 | * @return string array class URI or null |
||
| 409 | */ |
||
| 410 | |||
| 411 | View Code Duplication | public function getHierarchyProperty() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 429 | * @return boolean |
||
| 430 | */ |
||
| 431 | public function showNotation() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 438 | * @return boolean |
||
| 439 | */ |
||
| 440 | public function showAlphabeticalIndex() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 447 | * @return boolean |
||
| 448 | */ |
||
| 449 | public function getShowDeprecated() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
| 456 | * @return array of objects or an empty array |
||
| 457 | */ |
||
| 458 | public function getTypes($lang = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns an array of fallback languages that is ordered by priority and |
||
| 472 | * defined in the vocabulary configuration as a collection. |
||
| 473 | * Additionally, the chosen content language is inserted with the highest priority |
||
| 474 | * and the vocab default language is inserted with the lowest priority. |
||
| 475 | * @param string $clang |
||
| 476 | * @return array of language code strings |
||
| 477 | */ |
||
| 478 | public function getLanguageOrder($clang) |
||
| 492 | } |
||
| 493 |
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.