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 implements Modifiable |
||
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|null 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 | * Return true if the URI is within the URI space of this vocabulary. |
||
84 | * |
||
85 | * @param string full URI of concept |
||
86 | * @return bool true if URI is within URI namespace, false otherwise |
||
87 | */ |
||
88 | |||
89 | public function containsURI($uri) |
||
93 | |||
94 | /** |
||
95 | * Get the full URI of a concept in a vocabulary. If the passed local |
||
96 | * name is already a full URI, return it unchanged. |
||
97 | * |
||
98 | * @param $lname string local name of concept |
||
99 | * @return string full URI of concept |
||
100 | */ |
||
101 | public function getConceptURI($lname) |
||
109 | |||
110 | /** |
||
111 | * Asks the sparql implementation to make a label query for a uri. |
||
112 | * @param string $uri |
||
113 | * @param string $lang |
||
114 | */ |
||
115 | public function getConceptLabel($uri, $lang) |
||
119 | |||
120 | /** |
||
121 | * Asks the sparql implementation to make a label query for a uri. |
||
122 | * @param string $uri |
||
123 | * @param string $lang |
||
124 | * @return array array of altLabels |
||
125 | */ |
||
126 | public function getAllConceptLabels($uri, $lang) |
||
127 | { |
||
128 | return $this->getSparql()->queryAllConceptLabels($uri, $lang); |
||
129 | } |
||
130 | /** |
||
131 | * Get the localname of a concept in the vocabulary. If the URI is not |
||
132 | * in the URI space of this vocabulary, return the full URI. |
||
133 | * |
||
134 | * @param $uri string full URI of concept |
||
135 | * @return string local name of concept, or original full URI if the local name cannot be determined |
||
136 | */ |
||
137 | public function getLocalName($uri) |
||
141 | |||
142 | /** |
||
143 | * Retrieves all the information about the Vocabulary |
||
144 | * from the SPARQL-endpoint. |
||
145 | */ |
||
146 | public function getInfo($lang = null) |
||
219 | |||
220 | /** |
||
221 | * Return all concept schemes in the vocabulary. |
||
222 | * @return array Array with concept scheme URIs (string) as keys and labels (string) as values |
||
223 | */ |
||
224 | |||
225 | public function getConceptSchemes($lang = '') |
||
233 | |||
234 | /** |
||
235 | * Return the URI of the default concept scheme of this vocabulary. If the skosmos:mainConceptScheme property is set in the |
||
236 | * vocabulary configuration, that will be returned. Otherwise an arbitrary concept scheme will be returned. |
||
237 | * @return string concept scheme URI |
||
238 | */ |
||
239 | |||
240 | public function getDefaultConceptScheme() |
||
253 | |||
254 | /** |
||
255 | * Returns the main Concept Scheme of that Vocabulary, or null if not set. |
||
256 | * @param string $defaultConceptSchemeURI default concept scheme URI |
||
257 | * @return \EasyRdf\Graph|mixed |
||
258 | */ |
||
259 | public function getConceptScheme(string $defaultConceptSchemeURI) |
||
263 | |||
264 | /** |
||
265 | * Return the top concepts of a concept scheme in the vocabulary. |
||
266 | * @param string $conceptScheme URI of concept scheme whose top concepts to return. If not set, |
||
267 | * the default concept scheme of the vocabulary will be used. |
||
268 | * @param string $lang preferred language for the concept labels, |
||
269 | * @return array Array with concept URIs (string) as keys and labels (string) as values |
||
270 | */ |
||
271 | |||
272 | public function getTopConcepts($conceptScheme = null, $lang = '') |
||
285 | |||
286 | /** |
||
287 | * Tries to parse version, date and time from sparql version information into a readable format. |
||
288 | * @param string $version |
||
289 | * @return string |
||
290 | */ |
||
291 | private function parseVersionInfo($version) |
||
303 | |||
304 | /** |
||
305 | * Counts the statistics of the vocabulary. |
||
306 | * @return array of the concept/group counts |
||
307 | */ |
||
308 | public function getStatistics($lang = '', $array=null, $group=null) |
||
314 | |||
315 | /** |
||
316 | * Counts the statistics of the vocabulary. |
||
317 | * @return array of the concept counts in different languages |
||
318 | */ |
||
319 | public function getLabelStatistics() |
||
328 | |||
329 | /** |
||
330 | * Gets the parent concepts of a concept and child concepts for all of those. |
||
331 | * @param string $uri |
||
332 | * @param string $lang language identifier. |
||
333 | */ |
||
334 | View Code Duplication | public function getConceptHierarchy($uri, $lang) |
|
341 | |||
342 | /** |
||
343 | * Gets the child relations of a concept and whether these children have more children. |
||
344 | * @param string $uri |
||
345 | */ |
||
346 | View Code Duplication | public function getConceptChildren($uri, $lang) |
|
353 | |||
354 | /** |
||
355 | * Gets the skos:narrower relations of a concept. |
||
356 | * @param string $uri |
||
357 | * @param string $lang language identifier. |
||
358 | */ |
||
359 | public function getConceptNarrowers($uri, $lang) |
||
364 | |||
365 | /** |
||
366 | * Gets the skos:narrowerTransitive relations of a concept. |
||
367 | * @param string $uri |
||
368 | * @param integer $limit |
||
369 | * @param string $lang language identifier. |
||
370 | */ |
||
371 | public function getConceptTransitiveNarrowers($uri, $limit, $lang) |
||
376 | |||
377 | /** |
||
378 | * Gets the skos:broader relations of a concept. |
||
379 | * @param string $uri |
||
380 | * @param string $lang language identifier. |
||
381 | */ |
||
382 | public function getConceptBroaders($uri, $lang) |
||
387 | |||
388 | /** |
||
389 | * Gets the skos:broaderTransitive relations of a concept. |
||
390 | * @param string $uri |
||
391 | * @param integer $limit |
||
392 | * @param boolean $any set to true if you want to have a label even in case of a correct language one missing. |
||
393 | * @param string $lang language identifier. |
||
394 | */ |
||
395 | public function getConceptTransitiveBroaders($uri, $limit, $any = false, $lang) |
||
401 | |||
402 | /** |
||
403 | * Gets all the skos:related concepts of a concept. |
||
404 | * @param string $uri |
||
405 | * @param string $lang language identifier. |
||
406 | */ |
||
407 | public function getConceptRelateds($uri, $lang) |
||
412 | |||
413 | /** |
||
414 | * Makes a query into the sparql endpoint for a concept. |
||
415 | * @param string $uri the full URI of the concept |
||
416 | * @return Concept[] |
||
417 | */ |
||
418 | public function getConceptInfo($uri, $clang) |
||
424 | |||
425 | /** |
||
426 | * Lists the different concept groups available in the vocabulary. |
||
427 | * @param string $clang content language parameter |
||
428 | * @return array |
||
429 | */ |
||
430 | public function listConceptGroups($clang = null) |
||
449 | |||
450 | /** |
||
451 | * Lists the concepts available in the concept group. |
||
452 | * @param $clname |
||
453 | * @return array |
||
454 | */ |
||
455 | public function listConceptGroupContents($glname, $clang) |
||
475 | |||
476 | /** |
||
477 | * Returns the letters of the alphabet which have been used in this vocabulary. |
||
478 | * The returned letters may also include specials such as '0-9' (digits) and '!*' (special characters). |
||
479 | * @param $clang content language |
||
480 | * @return array array of letters |
||
481 | */ |
||
482 | public function getAlphabet($clang) |
||
508 | |||
509 | /** |
||
510 | * Searches for concepts with a label starting with the specified letter. |
||
511 | * Also the special tokens '0-9' (digits), '!*' (special characters) and '*' |
||
512 | * (everything) are supported. |
||
513 | * @param $letter letter (or special token) to search for |
||
514 | */ |
||
515 | public function searchConceptsAlphabetical($letter, $limit = null, $offset = null, $clang = null) |
||
519 | |||
520 | /** |
||
521 | * Makes a query for the transitive broaders of a concept and returns the concepts hierarchy processed for the view. |
||
522 | * @param string $lang |
||
523 | * @param string $uri |
||
524 | */ |
||
525 | public function getBreadCrumbs($lang, $uri) |
||
531 | |||
532 | /** |
||
533 | * Takes the crumbs as a parameter and combines the crumbs if the path they form is too long. |
||
534 | * @return array |
||
535 | */ |
||
536 | private function combineCrumbs($origCrumbs) |
||
557 | |||
558 | /** |
||
559 | * Recursive function for building the breadcrumb paths for the view. |
||
560 | * @param array $bTresult contains the results of the broaderTransitive query. |
||
561 | * @param string $uri |
||
562 | * @param array $path |
||
563 | */ |
||
564 | private function getCrumbs($bTresult, $uri, $path = null) |
||
603 | |||
604 | /** |
||
605 | * Verify that the requested language is supported by the vocabulary. If not, returns |
||
606 | * the default language of the vocabulary. |
||
607 | * @param string $lang language to check |
||
608 | * @return string language tag that is supported by the vocabulary |
||
609 | */ |
||
610 | |||
611 | public function verifyVocabularyLanguage($lang) |
||
615 | |||
616 | /** |
||
617 | * Returns a list of recently changed or entirely new concepts. |
||
618 | * @param string $clang content language for the labels |
||
619 | * @param string $lang UI language for the dates |
||
620 | * @return Array |
||
621 | */ |
||
622 | public function getChangeList($prop, $clang, $lang, $offset) |
||
632 | |||
633 | public function getTitle($lang=null) { |
||
636 | |||
637 | public function getShortName() { |
||
640 | |||
641 | public function getId() { |
||
644 | |||
645 | public function getModifiedDate() |
||
662 | |||
663 | public function isUseModifiedDate() |
||
667 | } |
||
668 |
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: