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() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns a label for the concept in the ui language or if not possible in any language. |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getLabel() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns a notation for the concept or null if it has not been defined. |
||
| 130 | * @return string eg. '999' |
||
| 131 | */ |
||
| 132 | public function getNotation() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns the Vocabulary object or undefined if that is not available. |
||
| 144 | * @return Vocabulary |
||
| 145 | */ |
||
| 146 | public function getVocab() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the vocabulary shortname string or id if that is not available. |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getShortName() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the vocabulary shortname string or id if that is not available. |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getVocabTitle() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Setter for the $clang property. |
||
| 171 | * @param string $clang language code eg. 'en' |
||
| 172 | */ |
||
| 173 | public function setContentLang($clang) |
||
| 177 | |||
| 178 | public function getContentLang() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Setter for the $foundby property. |
||
| 185 | * @param string $label label that was matched |
||
| 186 | * @param string $type type of match: 'alt', 'hidden', or 'lang' |
||
| 187 | */ |
||
| 188 | public function setFoundBy($label, $type) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Getter for the $foundby property. |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getFoundBy() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Getter for the $foundbytype property. |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getFoundByType() |
||
| 211 | |||
| 212 | public function getMappingProperties() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Iterates over all the properties of the concept and returns those in an array. |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | public function getProperties() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Removes properties that have duplicate values. |
||
| 394 | * @param $ret the array of properties generated by getProperties |
||
| 395 | * @param $duplicates array of properties found are a subProperty of a another property |
||
| 396 | * @return array of ConceptProperties |
||
| 397 | */ |
||
| 398 | public function removeDuplicatePropertyValues($ret, $duplicates) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets the creation date and modification date if available. |
||
| 426 | * @return String containing the date information in a human readable format. |
||
| 427 | */ |
||
| 428 | public function getDate() |
||
| 429 | { |
||
| 430 | $ret = ''; |
||
| 431 | $created = ''; |
||
| 432 | $modified = ''; |
||
| 433 | try { |
||
| 434 | // finding the created properties |
||
| 435 | if ($this->resource->get('dc:created')) { |
||
| 436 | $created = $this->resource->get('dc:created')->getValue(); |
||
| 437 | } |
||
| 438 | |||
| 439 | // finding the modified properties |
||
| 440 | if ($this->resource->get('dc:modified')) { |
||
| 441 | $modified = $this->resource->get('dc:modified')->getValue(); |
||
| 442 | } |
||
| 443 | |||
| 444 | // making a human readable string from the timestamps |
||
| 445 | if ($created != '') { |
||
| 446 | $ret = gettext('skosmos:created') . ' ' . (Punic\Calendar::formatDate($created, 'short')); |
||
| 447 | } |
||
| 448 | |||
| 449 | if ($modified != '') { |
||
| 450 | if ($created != '') { |
||
| 451 | $ret .= ', ' . gettext('skosmos:modified') . ' ' . (Punic\Calendar::formatDate($modified, 'short')); |
||
| 452 | } else { |
||
| 453 | $ret .= ' ' . ucfirst(gettext('skosmos:modified')) . ' ' . (Punic\Calendar::formatDate($modified, 'short')); |
||
| 454 | } |
||
| 455 | |||
| 456 | } |
||
| 457 | } catch (Exception $e) { |
||
| 458 | trigger_error($e->getMessage(), E_USER_WARNING); |
||
| 459 | $ret = ''; |
||
| 460 | if ($this->resource->get('dc:modified')) { |
||
| 461 | $modified = (string) $this->resource->get('dc:modified'); |
||
| 462 | $ret = gettext('skosmos:modified') . ' ' . $modified; |
||
| 463 | } |
||
| 464 | if ($this->resource->get('dc:created')) { |
||
| 465 | $created .= (string) $this->resource->get('dc:created'); |
||
| 466 | $ret .= ' ' . gettext('skosmos:created') . ' ' . $created; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | return $ret; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Gets the members of a specific collection. |
||
| 474 | * @param $coll |
||
| 475 | * @param array containing all narrowers as EasyRdf_Resource |
||
| 476 | * @return array containing ConceptPropertyValue objects |
||
| 477 | */ |
||
| 478 | private function getCollectionMembers($coll, $narrowers) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Gets the groups the concept belongs to. |
||
| 502 | */ |
||
| 503 | public function getGroupProperties() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Gets the groups/arrays the concept belongs to. |
||
| 510 | */ |
||
| 511 | public function getReverseResources($includeArrays) { |
||
| 540 | |||
| 541 | public function getArrayProperties() { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Reads the literal language code and gets a name for it from Punic or alternatively |
||
| 547 | * tries to search for a gettext translation. |
||
| 548 | * @param EasyRdf_Literal $lit |
||
| 549 | * @return string e.g. 'English' |
||
| 550 | */ |
||
| 551 | private function literalLanguageToString($lit) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Gets the values for the property in question in all other languages than the ui language. |
||
| 562 | */ |
||
| 563 | public function getForeignLabels() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Gets the values for the property in question in all other languages than the ui language. |
||
| 584 | * @param string $property |
||
| 585 | */ |
||
| 586 | public function getAllLabels($property) |
||
| 602 | } |
||
| 603 |
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.