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 BaseConfig |
||
| 7 | { |
||
| 8 | private $plugins; |
||
| 9 | private $pluginParameters; |
||
| 10 | private $languageOrderCache = array(); |
||
| 11 | |||
| 12 | public function __construct($resource, $globalPlugins=array()) |
||
| 13 | { |
||
| 14 | $this->resource = $resource; |
||
| 15 | $plugins = $this->resource->allLiterals('skosmos:usePlugin'); |
||
| 16 | $pluginArray = array(); |
||
| 17 | if ($plugins) { |
||
| 18 | foreach ($plugins as $pluginlit) { |
||
| 19 | $pluginArray[] = $pluginlit->getValue(); |
||
| 20 | } |
||
| 21 | } |
||
| 22 | $this->plugins = new PluginRegister(array_merge($globalPlugins, $pluginArray)); |
||
| 23 | // Get parameterized plugins defined as resources and their respective parameters |
||
| 24 | $pluginResources = $this->resource->allResources('skosmos:useParamPlugin'); |
||
| 25 | $this->pluginParameters = array(); |
||
| 26 | if ($pluginResources) { |
||
| 27 | foreach ($pluginResources as $pluginResource) { |
||
| 28 | $pluginName = $pluginResource->getLiteral('skosmos:usePlugin')->getValue(); |
||
| 29 | $this->pluginParameters[$pluginName] = array(); |
||
| 30 | |||
| 31 | $pluginParams = $pluginResource->allResources('skosmos:parameters'); |
||
| 32 | foreach ($pluginParams as $parameter) { |
||
| 33 | |||
| 34 | $paramLiterals = $parameter->allLiterals('schema:value'); |
||
| 35 | foreach ($paramLiterals as $paramLiteral) { |
||
| 36 | $paramName = $parameter->getLiteral('schema:propertyID')->getValue(); |
||
| 37 | $paramValue = $paramLiteral->getValue(); |
||
| 38 | $paramLang = $paramLiteral->getLang(); |
||
| 39 | if ($paramLang) { |
||
| 40 | $paramName .= '_' . $paramLang; |
||
| 41 | } |
||
| 42 | $this->pluginParameters[$pluginName][$paramName] = $paramValue; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | $pluginArray[] = $pluginName; |
||
| 46 | } |
||
| 47 | $this->plugins = new PluginRegister(array_merge($globalPlugins, $pluginArray)); |
||
| 48 | } |
||
| 49 | |||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the default language of this vocabulary |
||
| 54 | * @return string default language, e.g. 'en' |
||
| 55 | */ |
||
| 56 | |||
| 57 | public function getDefaultLanguage() |
||
| 58 | { |
||
| 59 | $deflang = $this->resource->getLiteral('skosmos:defaultLanguage'); |
||
| 60 | if ($deflang) { |
||
| 61 | return $deflang->getValue(); |
||
| 62 | } |
||
| 63 | |||
| 64 | $langs = $this->getLanguages(); |
||
| 65 | $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric |
||
| 66 | if (sizeof($langs) > 1) { |
||
| 67 | trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING); |
||
| 68 | } |
||
| 69 | |||
| 70 | return $deflang; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Whether the alphabetical index is small enough to be shown all at once. |
||
| 75 | * @return boolean true if all concepts can be shown at once. |
||
| 76 | */ |
||
| 77 | public function getAlphabeticalFull() |
||
| 78 | { |
||
| 79 | return $this->getBoolean('skosmos:fullAlphabeticalIndex'); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
| 84 | * using vocabId as a fallback. |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getShortName() |
||
| 88 | { |
||
| 89 | $shortname = $this->getLiteral('skosmos:shortName'); |
||
| 90 | if ($shortname) |
||
| 91 | return $shortname; |
||
| 92 | |||
| 93 | // if no shortname exists fall back to the id |
||
| 94 | return $this->getId(); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the vocabulary feedback e-mail address and return it. |
||
| 99 | * |
||
| 100 | * @return string e-mail address or null if not defined. |
||
| 101 | */ |
||
| 102 | public function getFeedbackRecipient() |
||
| 103 | { |
||
| 104 | $email = $this->resource->get('skosmos:feedbackRecipient'); |
||
| 105 | return isset($email) ? $email->getValue() : null; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the human readable vocabulary title. |
||
| 110 | * @return string the title of the vocabulary |
||
| 111 | */ |
||
| 112 | public function getTitle($lang = null) |
||
| 113 | { |
||
| 114 | return $this->getLiteral('dc:title', false, $lang); |
||
|
|
|||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns a boolean value set in the config.ttl config. |
||
| 119 | * @return boolean |
||
| 120 | */ |
||
| 121 | public function sortByNotation() |
||
| 122 | { |
||
| 123 | return $this->getBoolean('skosmos:sortByNotation'); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns a boolean value set in the config.ttl config. |
||
| 128 | * @return boolean |
||
| 129 | */ |
||
| 130 | public function showChangeList() |
||
| 131 | { |
||
| 132 | return $this->getBoolean('skosmos:showChangeList'); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * get the URLs from which the vocabulary data can be downloaded |
||
| 137 | * @return array Array with MIME type as key, URL as value |
||
| 138 | */ |
||
| 139 | public function getDataURLs() |
||
| 140 | { |
||
| 141 | $ret = array(); |
||
| 142 | $urls = $this->resource->allResources("void:dataDump"); |
||
| 143 | foreach ($urls as $url) { |
||
| 144 | // first try dc:format and dc11:format |
||
| 145 | $mimetypelit = $url->getLiteral('dc:format'); |
||
| 146 | if ($mimetypelit === null) { |
||
| 147 | $mimetypelit = $url->getLiteral('dc11:format'); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($mimetypelit !== null) { |
||
| 151 | $mimetype = $mimetypelit->getValue(); |
||
| 152 | } else { |
||
| 153 | $format = EasyRdf\Format::guessFormat(null, $url->getURI()); |
||
| 154 | if ($format === null) { |
||
| 155 | trigger_error("Could not guess format for <$url>.", E_USER_WARNING); |
||
| 156 | continue; |
||
| 157 | } |
||
| 158 | $mimetypes = array_keys($format->getMimeTypes()); |
||
| 159 | $mimetype = $mimetypes[0]; |
||
| 160 | } |
||
| 161 | |||
| 162 | $langLit = $url->getLiteral('dc:language'); |
||
| 163 | |||
| 164 | if ($langLit != null) { |
||
| 165 | //when the mimetype has language variants |
||
| 166 | $dataUrlLang = $langLit->getValue(); |
||
| 167 | |||
| 168 | if (!isset($ret[$mimetype])) { |
||
| 169 | $arr = array(); |
||
| 170 | } else { |
||
| 171 | $arr = $ret[$mimetype]; |
||
| 172 | } |
||
| 173 | $arr[$dataUrlLang] = $url->getURI(); |
||
| 174 | $ret[$mimetype] = $arr; |
||
| 175 | } else { |
||
| 176 | $ret[$mimetype] = $url->getURI(); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | return $ret; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns the main Concept Scheme URI of that Vocabulary, |
||
| 184 | * or null if not set. |
||
| 185 | * @return string concept scheme URI or null |
||
| 186 | */ |
||
| 187 | |||
| 188 | public function getMainConceptSchemeURI() |
||
| 189 | { |
||
| 190 | $val = $this->resource->getResource("skosmos:mainConceptScheme"); |
||
| 191 | if ($val) { |
||
| 192 | return $val->getURI(); |
||
| 193 | } |
||
| 194 | |||
| 195 | return null; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the class URI used for concept groups in this vocabulary, |
||
| 200 | * or null if not set. |
||
| 201 | * @return string group class URI or null |
||
| 202 | */ |
||
| 203 | |||
| 204 | public function getGroupClassURI() |
||
| 205 | { |
||
| 206 | $val = $this->resource->getResource("skosmos:groupClass"); |
||
| 207 | if ($val) { |
||
| 208 | return $val->getURI(); |
||
| 209 | } |
||
| 210 | |||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
| 216 | * or null if not set. |
||
| 217 | * @return string array class URI or null |
||
| 218 | */ |
||
| 219 | |||
| 220 | public function getArrayClassURI() |
||
| 221 | { |
||
| 222 | $val = $this->resource->getResource("skosmos:arrayClass"); |
||
| 223 | if ($val) { |
||
| 224 | return $val->getURI(); |
||
| 225 | } |
||
| 226 | |||
| 227 | return null; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns custom properties displayed on the search page if configured. |
||
| 232 | * @return string array class URI or null |
||
| 233 | */ |
||
| 234 | |||
| 235 | View Code Duplication | public function getAdditionalSearchProperties() |
|
| 236 | { |
||
| 237 | $resources = $this->resource->allResources("skosmos:showPropertyInSearch"); |
||
| 238 | $ret = array(); |
||
| 239 | foreach ($resources as $res) { |
||
| 240 | $prop = $res->getURI(); |
||
| 241 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible |
||
| 242 | { |
||
| 243 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
| 244 | } |
||
| 245 | |||
| 246 | $ret[] = $prop; |
||
| 247 | } |
||
| 248 | return $ret; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Queries whether the property should be shown with all the label language variations. |
||
| 253 | * @param string $property |
||
| 254 | * @return boolean |
||
| 255 | */ |
||
| 256 | public function hasMultiLingualProperty($property) |
||
| 257 | { |
||
| 258 | $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty"); |
||
| 259 | foreach ($resources as $res) { |
||
| 260 | $prop = $res->getURI(); |
||
| 261 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible |
||
| 262 | { |
||
| 263 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
| 264 | } |
||
| 265 | |||
| 266 | if ($prop === $property) { |
||
| 267 | return true; |
||
| 268 | } |
||
| 269 | |||
| 270 | } |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns a boolean value set in the config.ttl config. |
||
| 276 | * @return boolean |
||
| 277 | */ |
||
| 278 | public function getShowHierarchy() |
||
| 279 | { |
||
| 280 | return $this->getBoolean('skosmos:showTopConcepts'); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns a boolean value set in the config.ttl config. |
||
| 285 | * @return boolean |
||
| 286 | */ |
||
| 287 | public function showConceptSchemesInHierarchy() |
||
| 288 | { |
||
| 289 | return $this->getBoolean('skosmos:conceptSchemesInHierarchy'); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns a boolean value set in the config.ttl config. |
||
| 294 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
| 295 | */ |
||
| 296 | public function getExternalResourcesLoading() |
||
| 297 | { |
||
| 298 | return $this->getBoolean('skosmos:loadExternalResources', true); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns a boolean value set in the config.ttl config. |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public function getShowLangCodes() |
||
| 306 | { |
||
| 307 | return $this->getBoolean('skosmos:explicitLanguageTags'); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns a boolean value set in the config.ttl config. |
||
| 312 | * @return boolean |
||
| 313 | */ |
||
| 314 | public function searchByNotation() |
||
| 315 | { |
||
| 316 | return $this->getBoolean('skosmos:searchByNotation'); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns skosmos:marcSourcecode value set in config.ttl. |
||
| 321 | * @return string marcsource name |
||
| 322 | */ |
||
| 323 | public function getMarcSourceCode($lang = null) |
||
| 324 | { |
||
| 325 | return $this->getLiteral('skosmos:marcSourceCode', false, $lang); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Returns a boolean value set in the config.ttl config. |
||
| 330 | * @return array array of concept class URIs (can be empty) |
||
| 331 | */ |
||
| 332 | public function getIndexClasses() |
||
| 333 | { |
||
| 334 | return $this->getResources("skosmos:indexShowClass"); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns skosmos:externalProperty values set in the config.ttl config. |
||
| 339 | * @return array array of external property URIs (can be empty) |
||
| 340 | */ |
||
| 341 | public function getExtProperties() |
||
| 342 | { |
||
| 343 | return $this->getResources("skosmos:externalProperty"); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get the languages supported by this vocabulary |
||
| 348 | * @return array languages supported by this vocabulary (as language tag strings) |
||
| 349 | */ |
||
| 350 | public function getLanguages() |
||
| 351 | { |
||
| 352 | $langs = $this->resource->allLiterals('skosmos:language'); |
||
| 353 | $ret = array(); |
||
| 354 | foreach ($langs as $lang) { |
||
| 355 | $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang()); |
||
| 356 | $ret[$langlit] = $lang->getValue(); |
||
| 357 | } |
||
| 358 | ksort($ret); |
||
| 359 | |||
| 360 | return $ret; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the plugin parameters |
||
| 365 | * @return string plugin parameters or null |
||
| 366 | */ |
||
| 367 | public function getPluginParameters() { |
||
| 368 | return json_encode($this->pluginParameters, true); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the vocabulary default sidebar view. |
||
| 373 | * @return string name of the view |
||
| 374 | */ |
||
| 375 | public function getDefaultSidebarView() |
||
| 376 | { |
||
| 377 | $defview = $this->resource->getLiteral('skosmos:defaultSidebarView'); |
||
| 378 | if ($defview) { |
||
| 379 | $value = $defview->getValue(); |
||
| 380 | if ($value === 'groups' || $value === 'hierarchy') { |
||
| 381 | return $value; |
||
| 382 | } |
||
| 383 | |||
| 384 | } |
||
| 385 | if ($this->showAlphabeticalIndex() === false) { |
||
| 386 | if ($this->getShowHierarchy()) { |
||
| 387 | return 'hierarchy'; |
||
| 388 | } else if ($this->getGroupClassURI()) { |
||
| 389 | return 'groups'; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | return 'alphabetical'; // if not defined displaying the alphabetical index |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
| 397 | * @return string identifier eg. 'mesh'. |
||
| 398 | */ |
||
| 399 | public function getId() |
||
| 400 | { |
||
| 401 | $uriparts = explode("#", $this->resource->getURI()); |
||
| 402 | if (count($uriparts) != 1) |
||
| 403 | // hash namespace |
||
| 404 | { |
||
| 405 | return $uriparts[1]; |
||
| 406 | } |
||
| 407 | |||
| 408 | // slash namespace |
||
| 409 | $uriparts = explode("/", $this->resource->getURI()); |
||
| 410 | |||
| 411 | return $uriparts[count($uriparts) - 1]; |
||
| 412 | } |
||
| 413 | |||
| 414 | public function getShowStatistics() { |
||
| 415 | return $this->getBoolean('skosmos:showStatistics', true); |
||
| 416 | } |
||
| 417 | |||
| 418 | public function getPlugins() |
||
| 419 | { |
||
| 420 | return $this->plugins; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Returns the property/properties used for visualizing concept hierarchies. |
||
| 425 | * @return string array class URI or null |
||
| 426 | */ |
||
| 427 | |||
| 428 | View Code Duplication | public function getHierarchyProperty() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Returns a boolean value set in the config.ttl config. |
||
| 446 | * @return boolean |
||
| 447 | */ |
||
| 448 | public function showNotation() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns a boolean value set in the config.ttl config. |
||
| 455 | * @return boolean |
||
| 456 | */ |
||
| 457 | public function showAlphabeticalIndex() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns the alphabetical list qualifier in this vocabulary, |
||
| 464 | * or null if not set. |
||
| 465 | * @return EasyRdf\Resource|null alphabetical list qualifier resource or null |
||
| 466 | */ |
||
| 467 | public function getAlphabeticalListQualifier() |
||
| 468 | { |
||
| 469 | return $this->resource->getResource('skosmos:alphabeticalListQualifier'); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Returns a boolean value set in the config.ttl config. |
||
| 474 | * @return boolean |
||
| 475 | */ |
||
| 476 | public function getShowDeprecated() |
||
| 477 | { |
||
| 478 | return $this->getBoolean('skosmos:showDeprecated', false); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
| 483 | * @return array of objects or an empty array |
||
| 484 | */ |
||
| 485 | public function getTypes($lang = null) |
||
| 486 | { |
||
| 487 | $resources = $this->resource->allResources("dc:type"); |
||
| 488 | $ret = array(); |
||
| 489 | foreach ($resources as $res) { |
||
| 490 | $prop = $res->getURI(); |
||
| 491 | $label = $res->label($lang) ? $res->label($lang) : $res->label($this->getDefaultLanguage()); |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns an array of fallback languages that is ordered by priority and |
||
| 499 | * defined in the vocabulary configuration as a collection. |
||
| 500 | * Additionally, the chosen content language is inserted with the highest priority |
||
| 501 | * and the vocab default language is inserted with the lowest priority. |
||
| 502 | * @param string $clang |
||
| 503 | * @return array of language code strings |
||
| 504 | */ |
||
| 505 | public function getLanguageOrder($clang) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return boolean |
||
| 532 | */ |
||
| 533 | public function isUseModifiedDate() |
||
| 537 | } |
||
| 538 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: