| Total Complexity | 40 |
| Total Lines | 239 |
| 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() |
||
| 43 | { |
||
| 44 | return $this->type; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function getLabel($lang = '', $queryExVocabs = true) |
||
|
|
|||
| 48 | { |
||
| 49 | if (isset($this->labelcache[$lang])) { |
||
| 50 | return $this->labelcache[$lang]; |
||
| 51 | } |
||
| 52 | |||
| 53 | $label = $this->queryLabel($lang); |
||
| 54 | $this->labelcache[$lang] = $label; |
||
| 55 | return $label; |
||
| 56 | } |
||
| 57 | |||
| 58 | private function queryLabel($lang = '', $queryExVocabs = true) |
||
| 59 | { |
||
| 60 | if ($this->clang) { |
||
| 61 | $lang = $this->clang; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | $label = $this->getResourceLabel($this->resource, $lang); |
||
| 66 | if ($label) { |
||
| 67 | return $label; |
||
| 68 | } |
||
| 69 | |||
| 70 | // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping |
||
| 71 | $exvocab = $queryExVocabs ? $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId()) : null; |
||
| 72 | |||
| 73 | // if the resource is from another vocabulary known by the skosmos instance |
||
| 74 | if ($exvocab) { |
||
| 75 | $label = $this->getExternalLabel($exvocab, $this->getUri(), $lang) ? $this->getExternalLabel($exvocab, $this->getUri(), $lang) : $this->getExternalLabel($exvocab, $this->getUri(), $exvocab->getConfig()->getDefaultLanguage()); |
||
| 76 | if ($label) { |
||
| 77 | return $label; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | // using URI as label if nothing else has been found. |
||
| 82 | return $this->resource->shorten() ? $this->resource->shorten() : $this->resource->getUri(); |
||
| 83 | } |
||
| 84 | |||
| 85 | private function getResourceLabel($res, $lang = '') { |
||
| 86 | |||
| 87 | if ($this->clang) { |
||
| 88 | $lang = $this->clang; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($res->label($lang) !== null) { // current language |
||
| 92 | return $res->label($lang); |
||
| 93 | } elseif ($res->label() !== null) { // any language |
||
| 94 | return $res->label(); |
||
| 95 | } elseif ($res->getLiteral('rdf:value', $lang) !== null) { // current language |
||
| 96 | return $res->getLiteral('rdf:value', $lang); |
||
| 97 | } elseif ($res->getLiteral('rdf:value') !== null) { // any language |
||
| 98 | return $res->getLiteral('rdf:value'); |
||
| 99 | } |
||
| 100 | return null; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function getUri() |
||
| 104 | { |
||
| 105 | return $this->resource->getUri(); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getExVocab() |
||
| 109 | { |
||
| 110 | return $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId()); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getVocab() |
||
| 116 | } |
||
| 117 | |||
| 118 | public function getVocabName($lang = '') |
||
| 119 | { |
||
| 120 | |||
| 121 | if ($this->clang) { |
||
| 122 | $lang = $this->clang; |
||
| 123 | } |
||
| 124 | |||
| 125 | // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping |
||
| 126 | $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId()); |
||
| 127 | if ($exvocab) { |
||
| 128 | return $exvocab->getTitle($lang); |
||
| 129 | } |
||
| 130 | |||
| 131 | // @codeCoverageIgnoreStart |
||
| 132 | $scheme = $this->resource->get('skos:inScheme'); |
||
| 133 | if ($scheme) { |
||
| 134 | $schemeResource = $this->model->getResourceFromUri($scheme->getUri()); |
||
| 135 | if ($schemeResource) { |
||
| 136 | $schemaName = $this->getResourceLabel($schemeResource); |
||
| 137 | if ($schemaName) { |
||
| 138 | return $schemaName; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | // got a label for the concept, but not the scheme - use the host name as scheme label |
||
| 143 | return parse_url($this->resource->getUri(), PHP_URL_HOST); |
||
| 144 | // @codeCoverageIgnoreEnd |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getNotation() |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return the mapping as a JSKOS-compatible array. |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function asJskos($queryExVocabs = true, $lang = null, $hrefLink = null) |
||
| 167 | { |
||
| 168 | $propertyLabel = $this->getLabel($lang, $queryExVocabs); |
||
| 247 | } |
||
| 248 | |||
| 251 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.