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 Vocabulary 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 Vocabulary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Vocabulary extends DataObject |
||
| 7 | { |
||
| 8 | /** cached value of URI space */ |
||
| 9 | private $urispace = null; |
||
| 10 | private $config; |
||
| 11 | |||
| 12 | public function __construct($model, $resource) |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Returns the VocabularyConfig object |
||
| 20 | * @return VocabularyConfig |
||
| 21 | */ |
||
| 22 | public function getConfig() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get the SPARQL endpoint URL for this vocabulary |
||
| 29 | * |
||
| 30 | * @return string endpoint URL |
||
| 31 | */ |
||
| 32 | public function getEndpoint() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get the SPARQL graph URI for this vocabulary |
||
| 39 | * |
||
| 40 | * @return string graph URI |
||
| 41 | */ |
||
| 42 | public function getGraph() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the SPARQL implementation for this vocabulary |
||
| 54 | * |
||
| 55 | * @return GenericSparql SPARQL object |
||
| 56 | */ |
||
| 57 | public function getSparql() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the URI space of concepts in this vocabulary. |
||
| 69 | * |
||
| 70 | * @return string full URI of concept |
||
| 71 | */ |
||
| 72 | public function getUriSpace() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the full URI of a concept in a vocabulary. If the passed local |
||
| 84 | * name is already a full URI, return it unchanged. |
||
| 85 | * |
||
| 86 | * @param $lname string local name of concept |
||
| 87 | * @return string full URI of concept |
||
| 88 | */ |
||
| 89 | public function getConceptURI($lname) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Asks the sparql implementation to make a label query for a uri. |
||
| 100 | * @param string $uri |
||
| 101 | * @param string $lang |
||
| 102 | */ |
||
| 103 | public function getConceptLabel($uri, $lang) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the localname of a concept in the vocabulary. If the URI is not |
||
| 110 | * in the URI space of this vocabulary, return the full URI. |
||
| 111 | * |
||
| 112 | * @param $uri string full URI of concept |
||
| 113 | * @return string local name of concept, or original full URI if the local name cannot be determined |
||
| 114 | */ |
||
| 115 | public function getLocalName($uri) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Retrieves all the information about the Vocabulary |
||
| 122 | * from the SPARQL-endpoint. |
||
| 123 | */ |
||
| 124 | public function getInfo($lang = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return all concept schemes in the vocabulary. |
||
| 200 | * @return array Array with concept scheme URIs (string) as keys and labels (string) as values |
||
| 201 | */ |
||
| 202 | |||
| 203 | public function getConceptSchemes($lang = '') |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return the URI of the default concept scheme of this vocabulary. If the skosmos:mainConceptScheme property is set in the |
||
| 214 | * vocabulary configuration, that will be returned. Otherwise an arbitrary concept scheme will be returned. |
||
| 215 | * @return string concept scheme URI |
||
| 216 | */ |
||
| 217 | |||
| 218 | public function getDefaultConceptScheme() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the main Concept Scheme of that Vocabulary, or null if not set. |
||
| 234 | * @param string $defaultConceptSchemeURI default concept scheme URI |
||
| 235 | * @return \EasyRdf\Graph|mixed |
||
| 236 | */ |
||
| 237 | public function getConceptScheme(string $defaultConceptSchemeURI) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Return the top concepts of a concept scheme in the vocabulary. |
||
| 244 | * @param string $conceptScheme URI of concept scheme whose top concepts to return. If not set, |
||
| 245 | * the default concept scheme of the vocabulary will be used. |
||
| 246 | * @param string $lang preferred language for the concept labels, |
||
| 247 | * @return array Array with concept URIs (string) as keys and labels (string) as values |
||
| 248 | */ |
||
| 249 | |||
| 250 | public function getTopConcepts($conceptScheme = null, $lang = '') |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Tries to parse version, date and time from sparql version information into a readable format. |
||
| 266 | * @param string $version |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | private function parseVersionInfo($version) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Counts the statistics of the vocabulary. |
||
| 284 | * @return array of the concept/group counts |
||
| 285 | */ |
||
| 286 | public function getStatistics($lang = '', $array=null, $group=null) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Counts the statistics of the vocabulary. |
||
| 295 | * @return array of the concept counts in different languages |
||
| 296 | */ |
||
| 297 | public function getLabelStatistics() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Gets the parent concepts of a concept and child concepts for all of those. |
||
| 309 | * @param string $uri |
||
| 310 | * @param string $lang language identifier. |
||
| 311 | */ |
||
| 312 | View Code Duplication | public function getConceptHierarchy($uri, $lang) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Gets the child relations of a concept and whether these children have more children. |
||
| 322 | * @param string $uri |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function getConceptChildren($uri, $lang) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Gets the skos:narrower relations of a concept. |
||
| 334 | * @param string $uri |
||
| 335 | * @param string $lang language identifier. |
||
| 336 | */ |
||
| 337 | public function getConceptNarrowers($uri, $lang) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Gets the skos:narrowerTransitive relations of a concept. |
||
| 345 | * @param string $uri |
||
| 346 | * @param integer $limit |
||
| 347 | * @param string $lang language identifier. |
||
| 348 | */ |
||
| 349 | public function getConceptTransitiveNarrowers($uri, $limit, $lang) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Gets the skos:broader relations of a concept. |
||
| 357 | * @param string $uri |
||
| 358 | * @param string $lang language identifier. |
||
| 359 | */ |
||
| 360 | public function getConceptBroaders($uri, $lang) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Gets the skos:broaderTransitive relations of a concept. |
||
| 368 | * @param string $uri |
||
| 369 | * @param integer $limit |
||
| 370 | * @param boolean $any set to true if you want to have a label even in case of a correct language one missing. |
||
| 371 | * @param string $lang language identifier. |
||
| 372 | */ |
||
| 373 | public function getConceptTransitiveBroaders($uri, $limit, $any = false, $lang) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Gets all the skos:related concepts of a concept. |
||
| 382 | * @param string $uri |
||
| 383 | * @param string $lang language identifier. |
||
| 384 | */ |
||
| 385 | public function getConceptRelateds($uri, $lang) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Makes a query into the sparql endpoint for a concept. |
||
| 393 | * @param string $uri the full URI of the concept |
||
| 394 | * @return Concept[] |
||
| 395 | */ |
||
| 396 | public function getConceptInfo($uri, $clang) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Lists the different concept groups available in the vocabulary. |
||
| 405 | * @param string $clang content language parameter |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function listConceptGroups($clang = null) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Lists the concepts available in the concept group. |
||
| 430 | * @param $clname |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function listConceptGroupContents($glname, $clang) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the letters of the alphabet which have been used in this vocabulary. |
||
| 456 | * The returned letters may also include specials such as '0-9' (digits) and '!*' (special characters). |
||
| 457 | * @param $clang content language |
||
| 458 | * @return array array of letters |
||
| 459 | */ |
||
| 460 | public function getAlphabet($clang) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Searches for concepts with a label starting with the specified letter. |
||
| 489 | * Also the special tokens '0-9' (digits), '!*' (special characters) and '*' |
||
| 490 | * (everything) are supported. |
||
| 491 | * @param $letter letter (or special token) to search for |
||
| 492 | */ |
||
| 493 | public function searchConceptsAlphabetical($letter, $limit = null, $offset = null, $clang = null) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Makes a query for the transitive broaders of a concept and returns the concepts hierarchy processed for the view. |
||
| 500 | * @param string $lang |
||
| 501 | * @param string $uri |
||
| 502 | */ |
||
| 503 | public function getBreadCrumbs($lang, $uri) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Takes the crumbs as a parameter and combines the crumbs if the path they form is too long. |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | private function combineCrumbs($origCrumbs) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Recursive function for building the breadcrumb paths for the view. |
||
| 538 | * @param array $bTresult contains the results of the broaderTransitive query. |
||
| 539 | * @param string $uri |
||
| 540 | * @param array $path |
||
| 541 | */ |
||
| 542 | private function getCrumbs($bTresult, $uri, $path = null) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Verify that the requested language is supported by the vocabulary. If not, returns |
||
| 584 | * the default language of the vocabulary. |
||
| 585 | * @param string $lang language to check |
||
| 586 | * @return string language tag that is supported by the vocabulary |
||
| 587 | */ |
||
| 588 | |||
| 589 | public function verifyVocabularyLanguage($lang) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Returns a list of recently changed or entirely new concepts. |
||
| 596 | * @param string $clang content language for the labels |
||
| 597 | * @param string $lang UI language for the dates |
||
| 598 | * @return Array |
||
| 599 | */ |
||
| 600 | public function getChangeList($prop, $clang, $lang, $offset) |
||
| 610 | |||
| 611 | public function getTitle($lang=null) { |
||
| 614 | |||
| 615 | public function getShortName() { |
||
| 618 | |||
| 619 | public function getId() { |
||
| 622 | |||
| 623 | } |
||
| 624 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: