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 Concept 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 Concept, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Concept extends VocabularyDataObject |
||
|
|
|||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Stores a label string if the concept has been found through |
||
| 11 | * a altLabel/label in a another language than the ui. |
||
| 12 | */ |
||
| 13 | private $foundby; |
||
| 14 | /** Type of foundby match: 'alt', 'hidden' or 'lang' */ |
||
| 15 | private $foundbytype; |
||
| 16 | /** the EasyRdf_Graph object of the concept */ |
||
| 17 | private $graph; |
||
| 18 | private $clang; |
||
| 19 | |||
| 20 | /** concept properties that should not be shown to users */ |
||
| 21 | private $DELETED_PROPERTIES = array( |
||
| 22 | 'skosext:broaderGeneric', # these are remnants of bad modeling |
||
| 23 | 'skosext:broaderPartitive', # |
||
| 24 | |||
| 25 | 'skos:hiddenLabel', # because it's supposed to be hidden |
||
| 26 | 'skos:prefLabel', # handled separately by getLabel |
||
| 27 | 'rdfs:label', # handled separately by getLabel |
||
| 28 | |||
| 29 | 'skos:topConceptOf', # because it's too technical, not relevant for users |
||
| 30 | 'skos:inScheme', # should be evident in any case |
||
| 31 | 'skos:member', # this shouldn't be shown on the group page |
||
| 32 | 'dc:created', # handled separately |
||
| 33 | 'dc:modified', # handled separately |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** related concepts that should be shown to users in the appendix */ |
||
| 37 | private $MAPPING_PROPERTIES = array( |
||
| 38 | 'skos:exactMatch', |
||
| 39 | 'skos:narrowMatch', |
||
| 40 | 'skos:broadMatch', |
||
| 41 | 'skos:closeMatch', |
||
| 42 | 'skos:relatedMatch', |
||
| 43 | 'rdfs:seeAlso', |
||
| 44 | 'owl:sameAs', |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Initializing the concept object requires the following parameters. |
||
| 49 | * @param Model $model |
||
| 50 | * @param Vocabulary $vocab |
||
| 51 | * @param EasyRdf_Resource $resource |
||
| 52 | * @param EasyRdf_Graph $graph |
||
| 53 | */ |
||
| 54 | public function __construct($model, $vocab, $resource, $graph, $clang) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the concept uri. |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getUri() |
||
| 75 | |||
| 76 | public function getType() |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns a boolean value indicating whether the resource is a group defined in the vocab config as skosmos:groupClass. |
||
| 84 | * @return boolean |
||
| 85 | */ |
||
| 86 | public function isGroup() { |
||
| 87 | $groupClass = $this->getVocab()->getConfig()->getGroupClassURI(); |
||
| 88 | if ($groupClass) { |
||
| 89 | $groupClass = EasyRdf_Namespace::shorten($groupClass) !== null ? EasyRdf_Namespace::shorten($groupClass) : $groupClass; |
||
| 90 | return in_array($groupClass, $this->getType()); |
||
| 91 | } |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns a boolean value indicating if the concept has been deprecated. |
||
| 97 | * @return boolean |
||
| 98 | */ |
||
| 99 | public function getDeprecated() |
||
| 100 | { |
||
| 101 | $deprecatedValue = $this->resource->getLiteral('owl:deprecated'); |
||
| 102 | return ($deprecatedValue !== null && filter_var($deprecatedValue->getValue(), FILTER_VALIDATE_BOOLEAN)); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns a label for the concept in the content language or if not possible in any language. |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getLabel() |
||
| 110 | { |
||
| 111 | $lang = $this->clang; |
||
| 112 | // 1. label in current language |
||
| 113 | if ($this->resource->label($lang) !== null) { |
||
| 114 | return $this->resource->label($lang); |
||
| 115 | } |
||
| 116 | |||
| 117 | // 2. label in the vocabulary default language |
||
| 118 | View Code Duplication | if ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { |
|
| 119 | return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage()); |
||
| 120 | } |
||
| 121 | |||
| 122 | // 3. label in any language |
||
| 123 | $label = $this->resource->label(); |
||
| 124 | // if the label lang code is a subset of the ui lang eg. en-GB |
||
| 125 | if ($label !== null && strpos($label->getLang(), $lang . '-') === 0) { |
||
| 126 | return EasyRdf_Literal::create($label, $lang); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($label !== null) { |
||
| 130 | return $label->getLang() ? $label->getValue() . " (" . $label->getLang() . ")" : $label->getValue(); |
||
| 131 | } |
||
| 132 | |||
| 133 | // empty |
||
| 134 | return ""; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns a notation for the concept or null if it has not been defined. |
||
| 139 | * @return string eg. '999' |
||
| 140 | */ |
||
| 141 | public function getNotation() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the Vocabulary object or undefined if that is not available. |
||
| 153 | * @return Vocabulary |
||
| 154 | */ |
||
| 155 | public function getVocab() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the vocabulary shortname string or id if that is not available. |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getShortName() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the vocabulary shortname string or id if that is not available. |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getVocabTitle() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Setter for the $clang property. |
||
| 180 | * @param string $clang language code eg. 'en' |
||
| 181 | */ |
||
| 182 | public function setContentLang($clang) |
||
| 186 | |||
| 187 | public function getContentLang() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Setter for the $foundby property. |
||
| 194 | * @param string $label label that was matched |
||
| 195 | * @param string $type type of match: 'alt', 'hidden', or 'lang' |
||
| 196 | */ |
||
| 197 | public function setFoundBy($label, $type) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Getter for the $foundby property. |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getFoundBy() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Getter for the $foundbytype property. |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getFoundByType() |
||
| 220 | |||
| 221 | public function getMappingProperties() |
||
| 222 | { |
||
| 223 | $ret = array(); |
||
| 224 | |||
| 225 | $longUris = $this->resource->propertyUris(); |
||
| 226 | foreach ($longUris as &$prop) { |
||
| 227 | View Code Duplication | if (EasyRdf_Namespace::shorten($prop) !== null) { |
|
| 228 | // shortening property labels if possible |
||
| 229 | $prop = $sprop = EasyRdf_Namespace::shorten($prop); |
||
| 230 | } else { |
||
| 231 | $sprop = "<$prop>"; |
||
| 232 | } |
||
| 233 | // EasyRdf requires full URIs to be in angle brackets |
||
| 234 | |||
| 235 | if (in_array($prop, $this->MAPPING_PROPERTIES) && !in_array($prop, $this->DELETED_PROPERTIES)) { |
||
| 236 | $propres = new EasyRdf_Resource($prop, $this->graph); |
||
| 237 | $proplabel = $propres->label($this->getEnvLang()) ? $propres->label($this->getEnvLang()) : $propres->label(); // current language |
||
| 238 | $propobj = new ConceptProperty($prop, $proplabel); |
||
| 239 | if ($propobj->getLabel() !== null) { |
||
| 240 | // only display properties for which we have a label |
||
| 241 | $ret[$prop] = $propobj; |
||
| 242 | } |
||
| 243 | |||
| 244 | // Iterating through every resource and adding these to the data object. |
||
| 245 | foreach ($this->resource->allResources($sprop) as $val) { |
||
| 246 | if (isset($ret[$prop])) { |
||
| 247 | // checking if the target vocabulary can be found at the skosmos endpoint |
||
| 248 | $exuri = $val->getUri(); |
||
| 249 | $exvoc = $this->model->guessVocabularyFromURI($exuri); |
||
| 250 | // if not querying the uri itself |
||
| 251 | if (!$exvoc) { |
||
| 252 | $response = null; |
||
| 253 | // if told to do so in the vocabulary configuration |
||
| 254 | if ($this->vocab->getConfig()->getExternalResourcesLoading()) { |
||
| 255 | $response = $this->model->getResourceFromUri($exuri); |
||
| 256 | } |
||
| 257 | |||
| 258 | if ($response) { |
||
| 259 | $ret[$prop]->addValue(new ConceptMappingPropertyValue($this->model, $this->vocab, $response, $prop), $this->clang); |
||
| 260 | continue; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | $ret[$prop]->addValue(new ConceptMappingPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang), $this->clang); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // sorting the properties to a order preferred in the Skosmos concept page. |
||
| 270 | $ret = $this->arbitrarySort($ret); |
||
| 271 | |||
| 272 | return $ret; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Iterates over all the properties of the concept and returns those in an array. |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function getProperties() |
||
| 280 | { |
||
| 281 | $properties = array(); |
||
| 282 | $narrowersByUri = array(); |
||
| 283 | $inCollection = array(); |
||
| 284 | $membersArray = array(); |
||
| 285 | $longUris = $this->resource->propertyUris(); |
||
| 286 | $duplicates = array(); |
||
| 287 | $ret = array(); |
||
| 288 | |||
| 289 | // looking for collections and linking those with their narrower concepts |
||
| 290 | if ($this->vocab->getConfig()->getArrayClassURI() !== null) { |
||
| 291 | $collections = $this->graph->allOfType($this->vocab->getConfig()->getArrayClassURI()); |
||
| 292 | if (sizeof($collections) > 0) { |
||
| 293 | // indexing the narrowers once to avoid iterating all of them with every collection |
||
| 294 | foreach ($this->resource->allResources('skos:narrower') as $narrower) { |
||
| 295 | $narrowersByUri[$narrower->getUri()] = $narrower; |
||
| 296 | } |
||
| 297 | |||
| 298 | foreach ($collections as $coll) { |
||
| 299 | $currCollMembers = $this->getCollectionMembers($coll, $narrowersByUri); |
||
| 300 | foreach ($currCollMembers as $collection) { |
||
| 301 | if ($collection->getSubMembers()) { |
||
| 302 | $submembers = $collection->getSubMembers(); |
||
| 303 | foreach ($submembers as $member) { |
||
| 304 | $inCollection[$member->getUri()] = true; |
||
| 305 | } |
||
| 306 | |||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | if (isset($collection) && $collection->getSubMembers()) { |
||
| 311 | $membersArray = array_merge($currCollMembers, $membersArray); |
||
| 312 | } |
||
| 313 | |||
| 314 | } |
||
| 315 | $properties['skos:narrower'] = $membersArray; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | foreach ($longUris as &$prop) { |
||
| 320 | View Code Duplication | if (EasyRdf_Namespace::shorten($prop) !== null) { |
|
| 321 | // shortening property labels if possible |
||
| 322 | $prop = $sprop = EasyRdf_Namespace::shorten($prop); |
||
| 323 | } else { |
||
| 324 | $sprop = "<$prop>"; |
||
| 325 | } |
||
| 326 | // EasyRdf requires full URIs to be in angle brackets |
||
| 327 | |||
| 328 | if (!in_array($prop, $this->DELETED_PROPERTIES) || ($this->isGroup() === false && $prop === 'skos:member')) { |
||
| 329 | $propres = new EasyRdf_Resource($prop, $this->graph); |
||
| 330 | $proplabel = $propres->label($this->getEnvLang()) ? $propres->label($this->getEnvLang()) : $propres->label(); |
||
| 331 | $superprop = $propres->get('rdfs:subPropertyOf') ? $propres->get('rdfs:subPropertyOf')->getURI() : null; |
||
| 332 | if ($superprop) { |
||
| 333 | $superprop = EasyRdf_Namespace::shorten($superprop) ? EasyRdf_Namespace::shorten($superprop) : $superprop; |
||
| 334 | } |
||
| 335 | $propobj = new ConceptProperty($prop, $proplabel, $superprop); |
||
| 336 | |||
| 337 | if ($propobj->getLabel() !== null) { |
||
| 338 | // only display properties for which we have a label |
||
| 339 | $ret[$prop] = $propobj; |
||
| 340 | } |
||
| 341 | |||
| 342 | // searching for subproperties of literals too |
||
| 343 | foreach ($this->graph->allResources($prop, 'rdfs:subPropertyOf') as $subi) { |
||
| 344 | $suburi = EasyRdf_Namespace::shorten($subi->getUri()) ? EasyRdf_Namespace::shorten($subi->getUri()) : $subi->getUri(); |
||
| 345 | $duplicates[$suburi] = $prop; |
||
| 346 | } |
||
| 347 | |||
| 348 | // Iterating through every literal and adding these to the data object. |
||
| 349 | foreach ($this->resource->allLiterals($sprop) as $val) { |
||
| 350 | $literal = new ConceptPropertyValueLiteral($val, $prop); |
||
| 351 | // only add literals when they match the content/hit language or have no language defined |
||
| 352 | if (isset($ret[$prop]) && ($literal->getLang() === $this->clang || $literal->getLang() === null)) { |
||
| 353 | $ret[$prop]->addValue($literal); |
||
| 354 | } |
||
| 355 | |||
| 356 | } |
||
| 357 | |||
| 358 | // Iterating through every resource and adding these to the data object. |
||
| 359 | foreach ($this->resource->allResources($sprop) as $val) { |
||
| 360 | // skipping narrower concepts which are already shown in a collection |
||
| 361 | if ($sprop === 'skos:narrower' && array_key_exists($val->getUri(), $inCollection)) { |
||
| 362 | continue; |
||
| 363 | } |
||
| 364 | |||
| 365 | // hiding rdf:type property if it's just skos:Concept |
||
| 366 | if ($sprop === 'rdf:type' && $val->shorten() === 'skos:Concept') { |
||
| 367 | continue; |
||
| 368 | } |
||
| 369 | |||
| 370 | // handled by getMappingProperties() |
||
| 371 | if (in_array($sprop, $this->MAPPING_PROPERTIES)) { |
||
| 372 | continue; |
||
| 373 | } |
||
| 374 | |||
| 375 | View Code Duplication | if (isset($ret[$prop])) { |
|
| 376 | $ret[$prop]->addValue(new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang), $this->clang); |
||
| 377 | } |
||
| 378 | |||
| 379 | } |
||
| 380 | } |
||
| 381 | } |
||
| 382 | // adding narrowers part of a collection |
||
| 383 | foreach ($properties as $prop => $values) { |
||
| 384 | foreach ($values as $value) { |
||
| 385 | $ret[$prop]->addValue($value, $this->clang); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | foreach ($ret as $key => $prop) { |
||
| 390 | if (sizeof($prop->getValues()) === 0) { |
||
| 391 | unset($ret[$key]); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | $ret = $this->removeDuplicatePropertyValues($ret, $duplicates); |
||
| 396 | // sorting the properties to the order preferred in the Skosmos concept page. |
||
| 397 | $ret = $this->arbitrarySort($ret); |
||
| 398 | return $ret; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Removes properties that have duplicate values. |
||
| 403 | * @param $ret the array of properties generated by getProperties |
||
| 404 | * @param $duplicates array of properties found are a subProperty of a another property |
||
| 405 | * @return array of ConceptProperties |
||
| 406 | */ |
||
| 407 | public function removeDuplicatePropertyValues($ret, $duplicates) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Gets the creation date and modification date if available. |
||
| 435 | * @return String containing the date information in a human readable format. |
||
| 436 | */ |
||
| 437 | public function getDate() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Gets the members of a specific collection. |
||
| 483 | * @param $coll |
||
| 484 | * @param array containing all narrowers as EasyRdf_Resource |
||
| 485 | * @return array containing ConceptPropertyValue objects |
||
| 486 | */ |
||
| 487 | private function getCollectionMembers($coll, $narrowers) |
||
| 488 | { |
||
| 489 | $membersArray = array(); |
||
| 490 | $collLabel = $coll->label()->getValue($this->clang) ? $coll->label($this->clang) : $coll->label(); |
||
| 491 | if ($collLabel) { |
||
| 492 | $collLabel = $collLabel->getValue(); |
||
| 493 | } |
||
| 494 | |||
| 495 | $membersArray[$collLabel] = new ConceptPropertyValue($this->model, $this->vocab, $coll, 'skos:narrower', $this->clang); |
||
| 496 | foreach ($coll->allResources('skos:member') as $member) { |
||
| 497 | if (array_key_exists($member->getUri(), $narrowers)) { |
||
| 498 | $narrower = $narrowers[$member->getUri()]; |
||
| 499 | View Code Duplication | if (isset($narrower)) { |
|
| 500 | $membersArray[$collLabel]->addSubMember(new ConceptPropertyValue($this->model, $this->vocab, $narrower, 'skos:member', $this->clang), $this->clang); |
||
| 501 | } |
||
| 502 | |||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | return $membersArray; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Gets the groups the concept belongs to. |
||
| 511 | */ |
||
| 512 | public function getGroupProperties() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Gets the groups/arrays the concept belongs to. |
||
| 519 | */ |
||
| 520 | public function getReverseResources($includeArrays) { |
||
| 521 | $groups = array(); |
||
| 522 | $reverseResources = $this->graph->resourcesMatching('skos:member', $this->resource); |
||
| 523 | if (isset($reverseResources)) { |
||
| 524 | $arrayClassURI = $this->vocab !== null ? $this->vocab->getConfig()->getArrayClassURI() : null; |
||
| 525 | $arrayClass = $arrayClassURI !== null ? EasyRdf_Namespace::shorten($arrayClassURI) : null; |
||
| 526 | $superGroups = $this->resource->all('isothes:superGroup'); |
||
| 527 | $superGroupUris = array_map(function($obj) { return $obj->getUri(); }, $superGroups); |
||
| 528 | foreach ($reverseResources as $reverseResource) { |
||
| 529 | if (in_array($arrayClass, $reverseResource->types()) === $includeArrays) { |
||
| 530 | // not adding the memberOf if the reverse resource is already covered by isothes:superGroup see issue #433 |
||
| 531 | if (in_array($reverseResource->getUri(), $superGroupUris)) { |
||
| 532 | continue; |
||
| 533 | } |
||
| 534 | $property = in_array($arrayClass, $reverseResource->types()) ? "skosmos:memberOfArray" : "skosmos:memberOf"; |
||
| 535 | $collLabel = $reverseResource->label($this->clang) ? $reverseResource->label($this->clang) : $reverseResource->label(); |
||
| 536 | if ($collLabel) { |
||
| 537 | $collLabel = $collLabel->getValue(); |
||
| 538 | } |
||
| 539 | |||
| 540 | $groups[$collLabel] = new ConceptPropertyValue($this->model, $this->vocab, $reverseResource, $property, $this->clang); |
||
| 541 | ksort($groups); |
||
| 542 | $super = $this->graph->resourcesMatching('skos:member', $reverseResource); |
||
| 543 | while (isset($super) && !empty($super)) { |
||
| 544 | foreach ($super as $res) { |
||
| 545 | $superprop = new ConceptPropertyValue($this->model, $this->vocab, $res, 'skosmos:memberOfSuper', $this->clang); |
||
| 546 | array_unshift($groups, $superprop); |
||
| 547 | $super = $this->graph->resourcesMatching('skos:member', $res); |
||
| 548 | } |
||
| 549 | } |
||
| 550 | } |
||
| 551 | } |
||
| 552 | } |
||
| 553 | return $groups; |
||
| 554 | } |
||
| 555 | |||
| 556 | public function getArrayProperties() { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Reads the literal language code and gets a name for it from Punic or alternatively |
||
| 562 | * tries to search for a gettext translation. |
||
| 563 | * @param EasyRdf_Literal $lit |
||
| 564 | * @return string e.g. 'English' |
||
| 565 | */ |
||
| 566 | private function literalLanguageToString($lit) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Gets the values for the property in question in all other languages than the ui language. |
||
| 577 | */ |
||
| 578 | public function getForeignLabels() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Gets the values for the property in question in all other languages than the ui language. |
||
| 599 | * @param string $property |
||
| 600 | */ |
||
| 601 | public function getAllLabels($property) |
||
| 612 | } |
||
| 613 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.