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 | const DEFAULT_PROPERTY_ORDER = array("rdf:type", "dc:isReplacedBy", |
||
| 13 | "skos:definition", "skos:broader", "isothes:broaderGeneric", |
||
| 14 | "isothes:broaderPartitive", "isothes:broaderInstantial", |
||
| 15 | "skos:narrower", "isothes:narrowerGeneric", "isothes:narrowerPartitive", |
||
| 16 | "isothes:narrowerInstantial", "skos:related", "skos:altLabel", |
||
| 17 | "skos:note", "skos:scopeNote", "skos:historyNote", "rdfs:comment", |
||
| 18 | "dc11:source", "dc:source", "skosmos:memberOf", "skosmos:memberOfArray"); |
||
| 19 | |||
| 20 | const ISO25964_PROPERTY_ORDER = array("rdf:type", "dc:isReplacedBy", |
||
| 21 | // ISO 25964 allows placing all text fields (inc. SN and DEF) together |
||
| 22 | // so we will do that, except for HN, which is clearly administrative |
||
| 23 | "skos:note", "skos:scopeNote", "skos:definition", "rdfs:comment", |
||
| 24 | "dc11:source", "dc:source", "skos:altLabel", "skos:broader", |
||
| 25 | "isothes:broaderGeneric", "isothes:broaderPartitive", |
||
| 26 | "isothes:broaderInstantial", "skos:narrower", "isothes:narrowerGeneric", |
||
| 27 | "isothes:narrowerPartitive", "isothes:narrowerInstantial", |
||
| 28 | "skos:related", "skos:historyNote", "skosmos:memberOf", |
||
| 29 | "skosmos:memberOfArray"); |
||
| 30 | |||
| 31 | public function __construct($resource, $globalPlugins=array()) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get the SPARQL endpoint URL for this vocabulary |
||
| 73 | * |
||
| 74 | * @return string|null endpoint URL, or null if not set |
||
| 75 | */ |
||
| 76 | public function getSparqlEndpoint() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the SPARQL graph URI for this vocabulary |
||
| 87 | * |
||
| 88 | * @return string|null graph URI, or null if not set |
||
| 89 | */ |
||
| 90 | public function getSparqlGraph() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the SPARQL dialect for this vocabulary |
||
| 102 | * |
||
| 103 | * @return string|null dialect name |
||
| 104 | */ |
||
| 105 | public function getSparqlDialect() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the default language of this vocabulary |
||
| 117 | * @return string default language, e.g. 'en' |
||
| 118 | */ |
||
| 119 | |||
| 120 | public function getDefaultLanguage() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Whether the alphabetical index is small enough to be shown all at once. |
||
| 138 | * @return boolean true if all concepts can be shown at once. |
||
| 139 | */ |
||
| 140 | public function getAlphabeticalFull() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
| 147 | * using vocabId as a fallback. |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getShortName() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the vocabulary feedback e-mail address and return it. |
||
| 162 | * |
||
| 163 | * @return string e-mail address or null if not defined. |
||
| 164 | */ |
||
| 165 | public function getFeedbackRecipient() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the human readable vocabulary title. |
||
| 173 | * @return string the title of the vocabulary |
||
| 174 | */ |
||
| 175 | public function getTitle($lang = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns a boolean value set in the config.ttl config. |
||
| 182 | * @return boolean |
||
| 183 | */ |
||
| 184 | public function sortByNotation() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns a boolean value set in the config.ttl config. |
||
| 191 | * @return boolean |
||
| 192 | */ |
||
| 193 | public function showChangeList() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * get the URLs from which the vocabulary data can be downloaded |
||
| 200 | * @return array Array with MIME type as key, URL as value |
||
| 201 | */ |
||
| 202 | public function getDataURLs() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the main Concept Scheme URI of that Vocabulary, |
||
| 247 | * or null if not set. |
||
| 248 | * @return string concept scheme URI or null |
||
| 249 | */ |
||
| 250 | |||
| 251 | public function getMainConceptSchemeURI() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the class URI used for concept groups in this vocabulary, |
||
| 263 | * or null if not set. |
||
| 264 | * @return string group class URI or null |
||
| 265 | */ |
||
| 266 | |||
| 267 | public function getGroupClassURI() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
| 279 | * or null if not set. |
||
| 280 | * @return string array class URI or null |
||
| 281 | */ |
||
| 282 | |||
| 283 | public function getArrayClassURI() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns custom properties displayed on the search page if configured. |
||
| 295 | * @return array array class URI or null |
||
| 296 | */ |
||
| 297 | |||
| 298 | View Code Duplication | public function getAdditionalSearchProperties() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Queries whether the property should be shown with all the label language variations. |
||
| 316 | * @param string $property |
||
| 317 | * @return boolean |
||
| 318 | */ |
||
| 319 | public function hasMultiLingualProperty($property) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns a boolean value set in the config.ttl config. |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | public function getShowHierarchy() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns a boolean value set in the config.ttl config. |
||
| 348 | * @return boolean |
||
| 349 | */ |
||
| 350 | public function showConceptSchemesInHierarchy() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns a boolean value set in the config.ttl config. |
||
| 357 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
| 358 | */ |
||
| 359 | public function getExternalResourcesLoading() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a boolean value set in the config.ttl config. |
||
| 366 | * @return boolean |
||
| 367 | */ |
||
| 368 | public function getShowLangCodes() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Returns a boolean value set in the config.ttl config. |
||
| 375 | * @return boolean |
||
| 376 | */ |
||
| 377 | public function searchByNotation() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns skosmos:marcSourcecode value set in config.ttl. |
||
| 384 | * @return string marcsource name |
||
| 385 | */ |
||
| 386 | public function getMarcSourceCode($lang = null) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns a boolean value set in the config.ttl config. |
||
| 393 | * @return array array of concept class URIs (can be empty) |
||
| 394 | */ |
||
| 395 | public function getIndexClasses() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns skosmos:externalProperty values set in the config.ttl config. |
||
| 402 | * @return array array of external property URIs (can be empty) |
||
| 403 | */ |
||
| 404 | public function getExtProperties() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the languages supported by this vocabulary |
||
| 411 | * @return array languages supported by this vocabulary (as language tag strings) |
||
| 412 | */ |
||
| 413 | public function getLanguages() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns the plugin parameters |
||
| 428 | * @return string plugin parameters or null |
||
| 429 | */ |
||
| 430 | public function getPluginParameters() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Returns the vocabulary default sidebar view. |
||
| 436 | * @return string name of the view |
||
| 437 | */ |
||
| 438 | public function getDefaultSidebarView() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
| 460 | * @return string identifier eg. 'mesh'. |
||
| 461 | */ |
||
| 462 | public function getId() |
||
| 476 | |||
| 477 | public function getShowStatistics() { |
||
| 480 | |||
| 481 | public function getPlugins() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns the property/properties used for visualizing concept hierarchies. |
||
| 488 | * @return array array class URI or null |
||
| 489 | */ |
||
| 490 | |||
| 491 | View Code Duplication | public function getHierarchyProperty() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Returns a boolean value set in the config.ttl config. |
||
| 509 | * @return boolean |
||
| 510 | */ |
||
| 511 | public function showNotation() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns a boolean value set in the config.ttl config. |
||
| 518 | * @return boolean |
||
| 519 | */ |
||
| 520 | public function showAlphabeticalIndex() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Returns the alphabetical list qualifier in this vocabulary, |
||
| 527 | * or null if not set. |
||
| 528 | * @return EasyRdf\Resource|null alphabetical list qualifier resource or null |
||
| 529 | */ |
||
| 530 | public function getAlphabeticalListQualifier() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns a boolean value set in the config.ttl config. |
||
| 537 | * @return boolean |
||
| 538 | */ |
||
| 539 | public function getShowDeprecated() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
| 546 | * @return array of objects or an empty array |
||
| 547 | */ |
||
| 548 | public function getTypes($lang = null) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns an array of fallback languages that is ordered by priority and |
||
| 562 | * defined in the vocabulary configuration as a collection. |
||
| 563 | * Additionally, the chosen content language is inserted with the highest priority |
||
| 564 | * and the vocab default language is inserted with the lowest priority. |
||
| 565 | * @param string $clang |
||
| 566 | * @return array of language code strings |
||
| 567 | */ |
||
| 568 | public function getLanguageOrder($clang) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @return boolean |
||
| 595 | */ |
||
| 596 | public function isUseModifiedDate() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | public function getPropertyOrder() |
||
| 632 | } |
||
| 633 |
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: