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) |
||
| 29 | { |
||
| 30 | $val = $this->resource->getLiteral($property); |
||
| 31 | if ($val) { |
||
| 32 | return filter_var($val->getValue(), FILTER_VALIDATE_BOOLEAN); |
||
| 33 | } |
||
| 34 | |||
| 35 | return $default; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 40 | * @param string $property the property to query |
||
| 41 | * @param string $lang preferred language for the literal, |
||
| 42 | */ |
||
| 43 | private function getLiteral($property, $lang=null) |
||
| 44 | { |
||
| 45 | if (!isset($lang)) {; |
||
| 46 | $lang = $this->getEnvLang(); |
||
| 47 | } |
||
| 48 | |||
| 49 | $literal = $this->resource->getLiteral($property, $lang); |
||
| 50 | if ($literal) { |
||
| 51 | return $literal->getValue(); |
||
| 52 | } |
||
| 53 | |||
| 54 | // not found with selected language, try any language |
||
| 55 | $literal = $this->resource->getLiteral($property); |
||
| 56 | if ($literal) |
||
| 57 | return $literal->getValue(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the default language of this vocabulary |
||
| 62 | * @return string default language, e.g. 'en' |
||
| 63 | */ |
||
| 64 | |||
| 65 | public function getDefaultLanguage() |
||
| 66 | { |
||
| 67 | $deflang = $this->resource->getLiteral('skosmos:defaultLanguage'); |
||
| 68 | if ($deflang) { |
||
| 69 | return $deflang->getValue(); |
||
| 70 | } |
||
| 71 | |||
| 72 | $langs = $this->getLanguages(); |
||
| 73 | $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric |
||
| 74 | if (sizeof($langs) > 1) { |
||
| 75 | trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $deflang; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Wether the alphabetical index is small enough to be shown all at once. |
||
| 83 | * @return boolean true if all concepts can be shown at once. |
||
| 84 | */ |
||
| 85 | public function getAlphabeticalFull() |
||
| 86 | { |
||
| 87 | return $this->getBoolean('skosmos:fullAlphabeticalIndex'); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
| 92 | * using vocabId as a fallback. |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function getShortName() |
||
| 96 | { |
||
| 97 | $shortname = $this->getLiteral('skosmos:shortName'); |
||
| 98 | if ($shortname) |
||
| 99 | return $shortname; |
||
| 100 | |||
| 101 | // if no shortname exists fall back to the id |
||
| 102 | return $this->getId(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the vocabulary feedback e-mail address and return it. |
||
| 107 | * |
||
| 108 | * @return string e-mail address or null if not defined. |
||
| 109 | */ |
||
| 110 | public function getFeedbackRecipient() |
||
| 111 | { |
||
| 112 | $email = $this->resource->get('skosmos:feedbackRecipient'); |
||
| 113 | return isset($email) ? $email->getValue() : null; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the human readable vocabulary title. |
||
| 118 | * @return string the title of the vocabulary |
||
| 119 | */ |
||
| 120 | public function getTitle($lang = null) |
||
| 121 | { |
||
| 122 | return $this->getLiteral('dc:title', $lang); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | public function sortByNotation() |
||
| 130 | { |
||
| 131 | return $this->getBoolean('skosmos:sortByNotation'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 136 | * @return boolean |
||
| 137 | */ |
||
| 138 | public function showChangeList() |
||
| 139 | { |
||
| 140 | return $this->getBoolean('skosmos:showChangeList'); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * get the URLs from which the vocabulary data can be downloaded |
||
| 145 | * @return array Array with MIME type as key, URL as value |
||
| 146 | */ |
||
| 147 | public function getDataURLs() |
||
| 148 | { |
||
| 149 | $ret = array(); |
||
| 150 | $urls = $this->resource->allResources("void:dataDump"); |
||
| 151 | foreach ($urls as $url) { |
||
| 152 | // first try dc:format and dc11:format |
||
| 153 | $mimetypelit = $url->getLiteral('dc:format'); |
||
| 154 | if ($mimetypelit === null) { |
||
| 155 | $mimetypelit = $url->getLiteral('dc11:format'); |
||
| 156 | } |
||
| 157 | |||
| 158 | // if still not found, guess MIME type using file extension |
||
| 159 | if ($mimetypelit !== null) { |
||
| 160 | $mimetype = $mimetypelit->getValue(); |
||
| 161 | } else { |
||
| 162 | $format = EasyRdf_Format::guessFormat(null, $url->getURI()); |
||
| 163 | if ($format === null) { |
||
| 164 | trigger_error("Could not guess format for <$url>.", E_USER_WARNING); |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | $mimetypes = array_keys($format->getMimeTypes()); |
||
| 168 | $mimetype = $mimetypes[0]; |
||
| 169 | } |
||
| 170 | $ret[$mimetype] = $url->getURI(); |
||
| 171 | } |
||
| 172 | return $ret; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the class URI used for concept groups in this vocabulary, |
||
| 177 | * or null if not set. |
||
| 178 | * @return string group class URI or null |
||
| 179 | */ |
||
| 180 | |||
| 181 | public function getGroupClassURI() |
||
| 182 | { |
||
| 183 | $val = $this->resource->getResource("skosmos:groupClass"); |
||
| 184 | if ($val) { |
||
| 185 | return $val->getURI(); |
||
| 186 | } |
||
| 187 | |||
| 188 | return null; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
| 193 | * or null if not set. |
||
| 194 | * @return string array class URI or null |
||
| 195 | */ |
||
| 196 | |||
| 197 | public function getArrayClassURI() |
||
| 198 | { |
||
| 199 | $val = $this->resource->getResource("skosmos:arrayClass"); |
||
| 200 | if ($val) { |
||
| 201 | return $val->getURI(); |
||
| 202 | } |
||
| 203 | |||
| 204 | return null; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns custom properties displayed on the search page if configured. |
||
| 209 | * @return string array class URI or null |
||
| 210 | */ |
||
| 211 | |||
| 212 | View Code Duplication | public function getAdditionalSearchProperties() |
|
| 213 | { |
||
| 214 | $resources = $this->resource->allResources("skosmos:showPropertyInSearch"); |
||
| 215 | $ret = array(); |
||
| 216 | foreach ($resources as $res) { |
||
| 217 | $prop = $res->getURI(); |
||
| 218 | if (EasyRdf_Namespace::shorten($prop) !== null) // shortening property labels if possible |
||
| 219 | { |
||
| 220 | $prop = EasyRdf_Namespace::shorten($prop); |
||
| 221 | } |
||
| 222 | |||
| 223 | $ret[] = $prop; |
||
| 224 | } |
||
| 225 | return $ret; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Queries whether the property should be shown with all the label language variations. |
||
| 230 | * @param string $property |
||
| 231 | * @return boolean |
||
| 232 | */ |
||
| 233 | public function hasMultiLingualProperty($property) |
||
| 234 | { |
||
| 235 | $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty"); |
||
| 236 | foreach ($resources as $res) { |
||
| 237 | $prop = $res->getURI(); |
||
| 238 | if (EasyRdf_Namespace::shorten($prop) !== null) // shortening property labels if possible |
||
| 239 | { |
||
| 240 | $prop = EasyRdf_Namespace::shorten($prop); |
||
| 241 | } |
||
| 242 | |||
| 243 | if ($prop === $property) { |
||
| 244 | return true; |
||
| 245 | } |
||
| 246 | |||
| 247 | } |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 253 | * @return boolean |
||
| 254 | */ |
||
| 255 | public function getShowHierarchy() |
||
| 256 | { |
||
| 257 | return $this->getBoolean('skosmos:showTopConcepts'); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 262 | * @return boolean |
||
| 263 | */ |
||
| 264 | public function showConceptSchemesInHierarchy() |
||
| 265 | { |
||
| 266 | return $this->getBoolean('skosmos:conceptSchemesInHierarchy'); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 271 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
| 272 | */ |
||
| 273 | public function getExternalResourcesLoading() |
||
| 274 | { |
||
| 275 | return $this->getBoolean('skosmos:loadExternalResources', true); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 280 | * @return boolean |
||
| 281 | */ |
||
| 282 | public function getShowLangCodes() |
||
| 283 | { |
||
| 284 | return $this->getBoolean('skosmos:explicitLanguageTags'); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 289 | * @return array array of concept class URIs (can be empty) |
||
| 290 | */ |
||
| 291 | public function getIndexClasses() |
||
| 292 | { |
||
| 293 | $resources = $this->resource->allResources("skosmos:indexShowClass"); |
||
| 294 | $ret = array(); |
||
| 295 | foreach ($resources as $res) { |
||
| 296 | $ret[] = $res->getURI(); |
||
| 297 | } |
||
| 298 | return $ret; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the languages supported by this vocabulary |
||
| 303 | * @return array languages supported by this vocabulary (as language tag strings) |
||
| 304 | */ |
||
| 305 | public function getLanguages() |
||
| 306 | { |
||
| 307 | $langs = $this->resource->allLiterals('skosmos:language'); |
||
| 308 | $ret = array(); |
||
| 309 | foreach ($langs as $lang) { |
||
| 310 | $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang()); |
||
| 311 | $ret[$langlit] = $lang->getValue(); |
||
| 312 | } |
||
| 313 | ksort($ret); |
||
| 314 | |||
| 315 | return $ret; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the vocabulary default sidebar view. |
||
| 320 | * @return string name of the view |
||
| 321 | */ |
||
| 322 | public function getDefaultSidebarView() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
| 344 | * @return string identifier eg. 'mesh'. |
||
| 345 | */ |
||
| 346 | public function getId() |
||
| 347 | { |
||
| 348 | $uriparts = explode("#", $this->resource->getURI()); |
||
| 349 | if (count($uriparts) != 1) |
||
| 350 | // hash namespace |
||
| 351 | { |
||
| 352 | return $uriparts[1]; |
||
| 353 | } |
||
| 354 | |||
| 355 | // slash namespace |
||
| 356 | $uriparts = explode("/", $this->resource->getURI()); |
||
| 357 | |||
| 358 | return $uriparts[count($uriparts) - 1]; |
||
| 359 | } |
||
| 360 | |||
| 361 | public function getShowStatistics() { |
||
| 362 | return $this->getBoolean('skosmos:showStatistics', true); |
||
| 363 | } |
||
| 364 | |||
| 365 | public function getPlugins() |
||
| 366 | { |
||
| 367 | return $this->plugins; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns the property/properties used for visualizing concept hierarchies. |
||
| 372 | * @return string array class URI or null |
||
| 373 | */ |
||
| 374 | |||
| 375 | View Code Duplication | public function getHierarchyProperty() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 393 | * @return boolean |
||
| 394 | */ |
||
| 395 | public function showNotation() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 402 | * @return boolean |
||
| 403 | */ |
||
| 404 | public function showAlphabeticalIndex() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
| 411 | * @return array of objects or an empty array |
||
| 412 | */ |
||
| 413 | public function getTypes($lang = null) |
||
| 414 | { |
||
| 415 | $resources = $this->resource->allResources("dc:type"); |
||
| 416 | $ret = array(); |
||
| 417 | foreach ($resources as $res) { |
||
| 418 | $prop = $res->getURI(); |
||
| 419 | $label = $res->label($lang) ? $res->label($lang) : $res->label($this->getDefaultLanguage()); |
||
| 420 | $ret[] = array('uri' => $prop, 'prefLabel' => $label->getValue()); |
||
| 421 | } |
||
| 422 | return $ret; |
||
| 423 | } |
||
| 424 | } |
||
| 425 |
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.