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 |
||
| 12 | class Concept extends VocabularyDataObject |
||
|
|
|||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Stores a label string if the concept has been found through |
||
| 16 | * a altLabel/label in a another language than the ui. |
||
| 17 | */ |
||
| 18 | private $foundby; |
||
| 19 | /** Type of foundby match: 'alt', 'hidden' or 'lang' */ |
||
| 20 | private $foundbytype; |
||
| 21 | /** the EasyRdf_Graph object of the concept */ |
||
| 22 | private $graph; |
||
| 23 | private $clang; |
||
| 24 | |||
| 25 | /** concept properties that should not be shown to users */ |
||
| 26 | private $DELETED_PROPERTIES = array( |
||
| 27 | 'skosext:broaderGeneric', # these are remnants of bad modeling |
||
| 28 | 'skosext:broaderPartitive', # |
||
| 29 | |||
| 30 | 'skos:hiddenLabel', # because it's supposed to be hidden |
||
| 31 | 'skos:prefLabel', # handled separately by getLabel |
||
| 32 | 'rdfs:label', # handled separately by getLabel |
||
| 33 | |||
| 34 | 'skos:topConceptOf', # because it's too technical, not relevant for users |
||
| 35 | 'skos:inScheme', # should be evident in any case |
||
| 36 | 'skos:member', # this is shouldn't be shown on the group page |
||
| 37 | 'dc:created', # handled separately |
||
| 38 | 'dc:modified', # handled separately |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** related concepts that should be shown to users in the appendix */ |
||
| 42 | private $MAPPING_PROPERTIES = array( |
||
| 43 | 'skos:exactMatch', |
||
| 44 | 'skos:narrowMatch', |
||
| 45 | 'skos:broadMatch', |
||
| 46 | 'skos:closeMatch', |
||
| 47 | 'skos:relatedMatch', |
||
| 48 | 'rdfs:seeAlso', |
||
| 49 | 'owl:sameAs', |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initializing the concept object requires the following parameters. |
||
| 54 | * @param Model $model |
||
| 55 | * @param Vocabulary $vocab |
||
| 56 | * @param EasyRdf_Resource $resource |
||
| 57 | * @param EasyRdf_Graph $graph |
||
| 58 | */ |
||
| 59 | public function __construct($model, $vocab, $resource, $graph, $clang) |
||
| 60 | { |
||
| 61 | parent::__construct($model, $vocab, $resource); |
||
| 62 | $this->order = array("rdf:type", "dc:isReplacedBy", "skos:definition", "skos:broader", "skos:narrower", "skos:related", "skos:altLabel", "skosmos:memberOf", "skos:note", "skos:scopeNote", "skos:historyNote", "rdfs:comment", "dc11:source", "dc:source", "skos:prefLabel"); |
||
| 63 | $this->graph = $graph; |
||
| 64 | $this->clang = $clang; |
||
| 65 | // setting the Punic plugins locale for localized datetime conversions |
||
| 66 | if ($this->clang && $this->clang !== '') { |
||
| 67 | Punic\Data::setDefaultLocale($clang); |
||
| 68 | } |
||
| 69 | |||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Returns the concept uri. |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getUri() |
||
| 80 | |||
| 81 | public function getType() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns a boolean value indicating if the concept has been deprecated. |
||
| 88 | * @return boolean |
||
| 89 | */ |
||
| 90 | public function getDeprecated() |
||
| 91 | { |
||
| 92 | foreach ($this->resource->all('rdf:type') as $type) { |
||
| 93 | if (strpos($type->getUri(), 'DeprecatedConcept')) { |
||
| 94 | return true; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns a label for the concept in the ui language or if not possible in any language. |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getLabel() |
||
| 106 | { |
||
| 107 | $lang = $this->clang; |
||
| 108 | // 1. label in current language |
||
| 109 | if ($this->resource->label($lang) !== null) { |
||
| 110 | return $this->resource->label($lang); |
||
| 111 | } |
||
| 112 | |||
| 113 | // 2. label in the vocabulary default language |
||
| 114 | View Code Duplication | if ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { |
|
| 115 | return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage()); |
||
| 116 | } |
||
| 117 | |||
| 118 | // 3. label in any language |
||
| 119 | $label = $this->resource->label(); |
||
| 120 | // if the label lang code is a subset of the ui lang eg. en-GB |
||
| 121 | if ($label !== null && strpos($label->getLang(), $this->getEnvLang() . '-') === 0) { |
||
| 122 | return $label->getValue(); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($label !== null) { |
||
| 126 | return $label->getValue() . " (" . $label->getLang() . ")"; |
||
| 127 | } |
||
| 128 | |||
| 129 | // empty |
||
| 130 | return ""; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns a notation for the concept or null if it has not been defined. |
||
| 135 | * @return string eg. '999' |
||
| 136 | */ |
||
| 137 | public function getNotation() |
||
| 138 | { |
||
| 139 | $notation = $this->resource->get('skos:notation'); |
||
| 140 | if ($notation !== null) { |
||
| 141 | return $notation->getValue(); |
||
| 142 | } |
||
| 143 | |||
| 144 | return null; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the Vocabulary object or undefined if that is not available. |
||
| 149 | * @return Vocabulary |
||
| 150 | */ |
||
| 151 | public function getVocab() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the vocabulary shortname string or id if that is not available. |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getShortName() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the vocabulary shortname string or id if that is not available. |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getVocabTitle() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Setter for the $clang property. |
||
| 176 | * @param string $clang language code eg. 'en' |
||
| 177 | */ |
||
| 178 | public function setContentLang($clang) |
||
| 182 | |||
| 183 | public function getContentLang() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Setter for the $foundby property. |
||
| 190 | * @param string $label label that was matched |
||
| 191 | * @param string $type type of match: 'alt', 'hidden', or 'lang' |
||
| 192 | */ |
||
| 193 | public function setFoundBy($label, $type) |
||
| 194 | { |
||
| 195 | $this->foundby = $label; |
||
| 196 | $this->foundbytype = $type; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Getter for the $foundby property. |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getFoundBy() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Getter for the $foundbytype property. |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getFoundByType() |
||
| 216 | |||
| 217 | public function getMappingProperties() |
||
| 218 | { |
||
| 219 | $ret = array(); |
||
| 220 | |||
| 221 | $long_uris = $this->resource->propertyUris(); |
||
| 222 | foreach ($long_uris as &$prop) { |
||
| 223 | View Code Duplication | if (EasyRdf_Namespace::shorten($prop) !== null) { |
|
| 224 | // shortening property labels if possible |
||
| 225 | $prop = $sprop = EasyRdf_Namespace::shorten($prop); |
||
| 226 | } else { |
||
| 227 | $sprop = "<$prop>"; |
||
| 228 | } |
||
| 229 | // EasyRdf requires full URIs to be in angle brackets |
||
| 230 | |||
| 231 | if (in_array($prop, $this->MAPPING_PROPERTIES) && !in_array($prop, $this->DELETED_PROPERTIES)) { |
||
| 232 | $propres = new EasyRdf_Resource($prop, $this->graph); |
||
| 233 | $proplabel = $propres->label($this->getEnvLang()) ? $propres->label($this->getEnvLang()) : $propres->label(); // current language |
||
| 234 | $propobj = new ConceptProperty($prop, $proplabel); |
||
| 235 | if ($propobj->getLabel() !== null) { |
||
| 236 | // only display properties for which we have a label |
||
| 237 | $ret[$prop] = $propobj; |
||
| 238 | } |
||
| 239 | |||
| 240 | // Iterating through every resource and adding these to the data object. |
||
| 241 | foreach ($this->resource->allResources($sprop) as $val) { |
||
| 242 | if (isset($ret[$prop])) { |
||
| 243 | // checking if the target vocabulary can be found at the skosmos endpoint |
||
| 244 | $exuri = $val->getUri(); |
||
| 245 | $exvoc = $this->model->guessVocabularyFromURI($exuri); |
||
| 246 | // if not querying the uri itself |
||
| 247 | if (!$exvoc) { |
||
| 248 | $response = null; |
||
| 249 | // if told to do so in the vocabulary configuration |
||
| 250 | if ($this->vocab->getConfig()->getExternalResourcesLoading()) { |
||
| 251 | $response = $this->model->getResourceFromUri($exuri); |
||
| 252 | } |
||
| 253 | |||
| 254 | if ($response) { |
||
| 255 | $ret[$prop]->addValue(new ConceptMappingPropertyValue($this->model, $this->vocab, $response, $prop), $this->clang); |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | $ret[$prop]->addValue(new ConceptMappingPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang), $this->clang); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | // sorting the properties to a order preferred in the Skosmos concept page. |
||
| 266 | $ret = $this->arbitrarySort($ret); |
||
| 267 | |||
| 268 | return $ret; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Iterates over all the properties of the concept and returns those in an array. |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getProperties() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Removes properties that have duplicate values. |
||
| 395 | * @param $ret the array of properties generated by getProperties |
||
| 396 | * @param $duplicates array of properties found are a subProperty of a another property |
||
| 397 | * @return array of ConceptProperties |
||
| 398 | */ |
||
| 399 | public function removeDuplicatePropertyValues($ret, $duplicates) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the creation date and modification date if available. |
||
| 427 | * @return String containing the date information in a human readable format. |
||
| 428 | */ |
||
| 429 | public function getDate() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Gets the members of a specific collection. |
||
| 471 | * @param $coll |
||
| 472 | * @param array containing all narrowers as EasyRdf_Resource |
||
| 473 | * @return array containing ConceptPropertyValue objects |
||
| 474 | */ |
||
| 475 | private function getCollectionMembers($coll, $narrowers) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Gets the groups the concept belongs to. |
||
| 499 | */ |
||
| 500 | public function getGroupProperties() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Gets the groups/arrays the concept belongs to. |
||
| 507 | */ |
||
| 508 | public function getReverseResources($includeArrays) { |
||
| 537 | |||
| 538 | public function getArrayProperties() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Gets the values for the property in question in all other languages than the ui language. |
||
| 544 | */ |
||
| 545 | public function getForeignLabels() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Gets the values for the property in question in all other languages than the ui language. |
||
| 568 | * @param string $property |
||
| 569 | */ |
||
| 570 | public function getAllLabels($property) |
||
| 586 | } |
||
| 587 |
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.