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 | * Get the localname of a concept in the vocabulary. If the URI is not |
||
122 | * in the URI space of this vocabulary, return the full URI. |
||
123 | * |
||
124 | * @param $uri string full URI of concept |
||
125 | * @return string local name of concept, or original full URI if the local name cannot be determined |
||
126 | */ |
||
127 | public function getLocalName($uri) |
||
131 | |||
132 | /** |
||
133 | * Retrieves all the information about the Vocabulary |
||
134 | * from the SPARQL-endpoint. |
||
135 | */ |
||
136 | public function getInfo($lang = null) |
||
209 | |||
210 | /** |
||
211 | * Return all concept schemes in the vocabulary. |
||
212 | * @return array Array with concept scheme URIs (string) as keys and labels (string) as values |
||
213 | */ |
||
214 | |||
215 | public function getConceptSchemes($lang = '') |
||
223 | |||
224 | /** |
||
225 | * Return the URI of the default concept scheme of this vocabulary. If the skosmos:mainConceptScheme property is set in the |
||
226 | * vocabulary configuration, that will be returned. Otherwise an arbitrary concept scheme will be returned. |
||
227 | * @return string concept scheme URI |
||
228 | */ |
||
229 | |||
230 | public function getDefaultConceptScheme() |
||
243 | |||
244 | /** |
||
245 | * Returns the main Concept Scheme of that Vocabulary, or null if not set. |
||
246 | * @param string $defaultConceptSchemeURI default concept scheme URI |
||
247 | * @return \EasyRdf\Graph|mixed |
||
248 | */ |
||
249 | public function getConceptScheme(string $defaultConceptSchemeURI) |
||
253 | |||
254 | /** |
||
255 | * Return the top concepts of a concept scheme in the vocabulary. |
||
256 | * @param string $conceptScheme URI of concept scheme whose top concepts to return. If not set, |
||
257 | * the default concept scheme of the vocabulary will be used. |
||
258 | * @param string $lang preferred language for the concept labels, |
||
259 | * @return array Array with concept URIs (string) as keys and labels (string) as values |
||
260 | */ |
||
261 | |||
262 | public function getTopConcepts($conceptScheme = null, $lang = '') |
||
275 | |||
276 | /** |
||
277 | * Tries to parse version, date and time from sparql version information into a readable format. |
||
278 | * @param string $version |
||
279 | * @return string |
||
280 | */ |
||
281 | private function parseVersionInfo($version) |
||
293 | |||
294 | /** |
||
295 | * Counts the statistics of the vocabulary. |
||
296 | * @return array of the concept/group counts |
||
297 | */ |
||
298 | public function getStatistics($lang = '', $array=null, $group=null) |
||
304 | |||
305 | /** |
||
306 | * Counts the statistics of the vocabulary. |
||
307 | * @return array of the concept counts in different languages |
||
308 | */ |
||
309 | public function getLabelStatistics() |
||
318 | |||
319 | /** |
||
320 | * Gets the parent concepts of a concept and child concepts for all of those. |
||
321 | * @param string $uri |
||
322 | * @param string $lang language identifier. |
||
323 | */ |
||
324 | View Code Duplication | public function getConceptHierarchy($uri, $lang) |
|
331 | |||
332 | /** |
||
333 | * Gets the child relations of a concept and whether these children have more children. |
||
334 | * @param string $uri |
||
335 | */ |
||
336 | View Code Duplication | public function getConceptChildren($uri, $lang) |
|
343 | |||
344 | /** |
||
345 | * Gets the skos:narrower relations of a concept. |
||
346 | * @param string $uri |
||
347 | * @param string $lang language identifier. |
||
348 | */ |
||
349 | public function getConceptNarrowers($uri, $lang) |
||
354 | |||
355 | /** |
||
356 | * Gets the skos:narrowerTransitive relations of a concept. |
||
357 | * @param string $uri |
||
358 | * @param integer $limit |
||
359 | * @param string $lang language identifier. |
||
360 | */ |
||
361 | public function getConceptTransitiveNarrowers($uri, $limit, $lang) |
||
366 | |||
367 | /** |
||
368 | * Gets the skos:broader relations of a concept. |
||
369 | * @param string $uri |
||
370 | * @param string $lang language identifier. |
||
371 | */ |
||
372 | public function getConceptBroaders($uri, $lang) |
||
377 | |||
378 | /** |
||
379 | * Gets the skos:broaderTransitive relations of a concept. |
||
380 | * @param string $uri |
||
381 | * @param integer $limit |
||
382 | * @param boolean $any set to true if you want to have a label even in case of a correct language one missing. |
||
383 | * @param string $lang language identifier. |
||
384 | */ |
||
385 | public function getConceptTransitiveBroaders($uri, $limit, $any = false, $lang) |
||
391 | |||
392 | /** |
||
393 | * Gets all the skos:related concepts of a concept. |
||
394 | * @param string $uri |
||
395 | * @param string $lang language identifier. |
||
396 | */ |
||
397 | public function getConceptRelateds($uri, $lang) |
||
402 | |||
403 | /** |
||
404 | * Makes a query into the sparql endpoint for a concept. |
||
405 | * @param string $uri the full URI of the concept |
||
406 | * @return Concept[] |
||
407 | */ |
||
408 | public function getConceptInfo($uri, $clang) |
||
414 | |||
415 | /** |
||
416 | * Lists the different concept groups available in the vocabulary. |
||
417 | * @param string $clang content language parameter |
||
418 | * @return array |
||
419 | */ |
||
420 | public function listConceptGroups($clang = null) |
||
439 | |||
440 | /** |
||
441 | * Lists the concepts available in the concept group. |
||
442 | * @param $clname |
||
443 | * @return array |
||
444 | */ |
||
445 | public function listConceptGroupContents($glname, $clang) |
||
465 | |||
466 | /** |
||
467 | * Returns the letters of the alphabet which have been used in this vocabulary. |
||
468 | * The returned letters may also include specials such as '0-9' (digits) and '!*' (special characters). |
||
469 | * @param $clang content language |
||
470 | * @return array array of letters |
||
471 | */ |
||
472 | public function getAlphabet($clang) |
||
498 | |||
499 | /** |
||
500 | * Searches for concepts with a label starting with the specified letter. |
||
501 | * Also the special tokens '0-9' (digits), '!*' (special characters) and '*' |
||
502 | * (everything) are supported. |
||
503 | * @param $letter letter (or special token) to search for |
||
504 | */ |
||
505 | public function searchConceptsAlphabetical($letter, $limit = null, $offset = null, $clang = null) |
||
509 | |||
510 | /** |
||
511 | * Makes a query for the transitive broaders of a concept and returns the concepts hierarchy processed for the view. |
||
512 | * @param string $lang |
||
513 | * @param string $uri |
||
514 | */ |
||
515 | public function getBreadCrumbs($lang, $uri) |
||
521 | |||
522 | /** |
||
523 | * Takes the crumbs as a parameter and combines the crumbs if the path they form is too long. |
||
524 | * @return array |
||
525 | */ |
||
526 | private function combineCrumbs($origCrumbs) |
||
547 | |||
548 | /** |
||
549 | * Recursive function for building the breadcrumb paths for the view. |
||
550 | * @param array $bTresult contains the results of the broaderTransitive query. |
||
551 | * @param string $uri |
||
552 | * @param array $path |
||
553 | */ |
||
554 | private function getCrumbs($bTresult, $uri, $path = null) |
||
593 | |||
594 | /** |
||
595 | * Verify that the requested language is supported by the vocabulary. If not, returns |
||
596 | * the default language of the vocabulary. |
||
597 | * @param string $lang language to check |
||
598 | * @return string language tag that is supported by the vocabulary |
||
599 | */ |
||
600 | |||
601 | public function verifyVocabularyLanguage($lang) |
||
605 | |||
606 | /** |
||
607 | * Returns a list of recently changed or entirely new concepts. |
||
608 | * @param string $clang content language for the labels |
||
609 | * @param string $lang UI language for the dates |
||
610 | * @return Array |
||
611 | */ |
||
612 | public function getChangeList($prop, $clang, $lang, $offset) |
||
622 | |||
623 | public function getTitle($lang=null) { |
||
626 | |||
627 | public function getShortName() { |
||
630 | |||
631 | public function getId() { |
||
634 | |||
635 | public function getModifiedDate() |
||
636 | { |
||
637 | $modifiedDate = null; |
||
638 | |||
639 | $conceptSchemeURI = $this->getDefaultConceptScheme(); |
||
640 | if ($conceptSchemeURI) { |
||
641 | $conceptSchemeGraph = $this->getConceptScheme($conceptSchemeURI); |
||
642 | if (!$conceptSchemeGraph->isEmpty()) { |
||
643 | $literal = $conceptSchemeGraph->getLiteral($conceptSchemeURI, "dc:modified"); |
||
644 | if ($literal) { |
||
645 | $modifiedDate = $literal->getValue(); |
||
646 | } |
||
647 | } |
||
648 | } |
||
649 | |||
650 | return $modifiedDate; |
||
651 | } |
||
652 | |||
653 | public function isUseModifiedDate() |
||
657 | } |
||
658 |
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: