| Total Complexity | 42 | 
| Total Lines | 246 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like ConceptMappingPropertyValue 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.
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 ConceptMappingPropertyValue, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 8 | class ConceptMappingPropertyValue extends VocabularyDataObject | ||
| 9 | { | ||
| 10 | /** property type */ | ||
| 11 | private $type; | ||
| 12 | private $source; | ||
| 13 | private $clang; | ||
| 14 | private $labelcache; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * ConceptMappingPropertyValue constructor. | ||
| 18 | * | ||
| 19 | * @param Model $model | ||
| 20 | * @param Vocabulary $vocab Target vocabulary | ||
| 21 | * @param Resource $target Target concept resource | ||
| 22 | * @param Resource $source Source concept resource | ||
| 23 | * @param string $prop Mapping property | ||
| 24 | * @param ?string $clang Preferred label language (nullable) | ||
| 25 | */ | ||
| 26 | public function __construct(Model $model, Vocabulary $vocab, Resource $target, Resource $source, string $prop, $clang = '') | ||
| 27 |     { | ||
| 28 | parent::__construct($model, $vocab, $target); | ||
| 29 | $this->source = $source; | ||
| 30 | $this->type = $prop; | ||
| 31 | $this->clang = $clang; | ||
| 32 | $this->labelcache = array(); | ||
| 33 | } | ||
| 34 | |||
| 35 | public function __toString() | ||
| 36 |     { | ||
| 37 | $label = $this->getLabel(); | ||
| 38 | $notation = $this->getNotation(); | ||
| 39 | return ltrim($notation . ' ') . $label; | ||
| 40 | } | ||
| 41 | |||
| 42 | public function getType() | ||
| 45 | } | ||
| 46 | |||
| 47 | public function getLabel($lang = '', $allowExternal = true) | ||
| 48 |     { | ||
| 49 | $key = $lang . "/" . ($allowExternal ? "true" : "false"); | ||
| 50 |         if (isset($this->labelcache[$key])) { | ||
| 51 | return $this->labelcache[$key]; | ||
| 52 | } | ||
| 53 | |||
| 54 | $label = $this->queryLabel($lang, $allowExternal); | ||
| 55 | $this->labelcache[$key] = $label; | ||
| 56 | return $label; | ||
| 57 | } | ||
| 58 | |||
| 59 | public function getSortKey() | ||
| 60 |     { | ||
| 61 | return strtolower($this->getVocabName() . ": " . $this->getLabel()); | ||
| 62 | } | ||
| 63 | |||
| 64 | private function queryLabel($lang = '', $allowExternal = true) | ||
| 65 |     { | ||
| 66 |         if ($this->clang) { | ||
| 67 | $lang = $this->clang; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | $label = $this->getResourceLabel($this->resource, $lang); | ||
| 72 |         if ($label) { | ||
| 73 | return $label; | ||
| 74 | } | ||
| 75 | |||
| 76 | // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping | ||
| 77 | $exvocab = $allowExternal ? $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId()) : null; | ||
| 78 | |||
| 79 | // if the resource is from another vocabulary known by the skosmos instance | ||
| 80 |         if ($exvocab) { | ||
| 81 | $label = $this->getExternalLabel($exvocab, $this->getUri(), $lang) ? $this->getExternalLabel($exvocab, $this->getUri(), $lang) : $this->getExternalLabel($exvocab, $this->getUri(), $exvocab->getConfig()->getDefaultLanguage()); | ||
| 82 |             if ($label) { | ||
|  | |||
| 83 | return $label; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | // using URI as label if nothing else has been found. | ||
| 88 | return $this->resource->shorten() ? $this->resource->shorten() : $this->resource->getUri(); | ||
| 89 | } | ||
| 90 | |||
| 91 | private function getResourceLabel($res, $lang = '') | ||
| 92 |     { | ||
| 93 | |||
| 94 |         if ($this->clang) { | ||
| 95 | $lang = $this->clang; | ||
| 96 | } | ||
| 97 | |||
| 98 |         if ($res->label($lang) !== null) { // current language | ||
| 99 | return $res->label($lang); | ||
| 100 |         } elseif ($res->label() !== null) { // any language | ||
| 101 | return $res->label(); | ||
| 102 |         } elseif ($res->getLiteral('rdf:value', $lang) !== null) { // current language | ||
| 103 |             return $res->getLiteral('rdf:value', $lang); | ||
| 104 |         } elseif ($res->getLiteral('rdf:value') !== null) { // any language | ||
| 105 |             return $res->getLiteral('rdf:value'); | ||
| 106 | } | ||
| 107 | return null; | ||
| 108 | } | ||
| 109 | |||
| 110 | public function getUri() | ||
| 111 |     { | ||
| 112 | return $this->resource->getUri(); | ||
| 113 | } | ||
| 114 | |||
| 115 | public function getExVocab() | ||
| 116 |     { | ||
| 117 | return $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId()); | ||
| 118 | } | ||
| 119 | |||
| 120 | public function getVocab() | ||
| 123 | } | ||
| 124 | |||
| 125 | public function getVocabName($lang = '') | ||
| 126 |     { | ||
| 127 | |||
| 128 |         if ($this->clang) { | ||
| 129 | $lang = $this->clang; | ||
| 130 | } | ||
| 131 | |||
| 132 | // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping | ||
| 133 | $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId()); | ||
| 134 |         if ($exvocab) { | ||
| 135 | return $exvocab->getTitle($lang); | ||
| 136 | } | ||
| 137 | |||
| 138 | // @codeCoverageIgnoreStart | ||
| 139 |         $scheme = $this->resource->get('skos:inScheme'); | ||
| 140 |         if ($scheme) { | ||
| 141 | $schemeResource = $this->model->getResourceFromUri($scheme->getUri()); | ||
| 142 |             if ($schemeResource) { | ||
| 143 | $schemaName = $this->getResourceLabel($schemeResource); | ||
| 144 |                 if ($schemaName) { | ||
| 145 | return $schemaName; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | // got a label for the concept, but not the scheme - use the host name as scheme label | ||
| 150 | return parse_url($this->resource->getUri(), PHP_URL_HOST); | ||
| 151 | // @codeCoverageIgnoreEnd | ||
| 152 | } | ||
| 153 | |||
| 154 | public function getNotation() | ||
| 167 | } | ||
| 168 | |||
| 169 | /** | ||
| 170 | * Return the mapping as a JSKOS-compatible array. | ||
| 171 | * @return array | ||
| 172 | */ | ||
| 173 | public function asJskos($allowExternal = true, $lang = null, $hrefLink = null) | ||
| 254 | } | ||
| 255 | |||
| 257 |