Completed
Push — master ( bc9513...38f056 )
by Osma
02:06
created

ConceptMappingPropertyValue::asJskos()   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 40
nop 1
dl 0
loc 57
rs 8.0048
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use EasyRdf\Resource;
4
5
/**
6
 * Class for handling concept property values.
7
 */
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)
0 ignored issues
show
Documentation introduced by
The doc-type ?string could not be parsed: Unknown type name "?string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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 $notation . $label;
40
    }
41
42
    public function getType()
43
    {
44
        return $this->type;
45
    }
46
47
    public function getLabel($lang = '', $queryExVocabs = true)
0 ignored issues
show
Unused Code introduced by
The parameter $queryExVocabs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
        $label = $this->resource->shorten() ? $this->resource->shorten() : $this->resource->getUri();
83
        return $label;
84
    }
85
86
    private function getResourceLabel($res, $lang = '') {
87
88
        if ($this->clang) {
89
            $lang = $this->clang;
90
        }
91
92
        if ($res->label($lang) !== null) { // current language
93
            return $res->label($lang);
94
        } elseif ($res->label() !== null) { // any language
95
            return $res->label();
96
        } elseif ($res->getLiteral('rdf:value', $lang) !== null) { // current language
97
            return $res->getLiteral('rdf:value', $lang);
98
        } elseif ($res->getLiteral('rdf:value') !== null) { // any language
99
            return $res->getLiteral('rdf:value');
100
        }
101
        return null;
102
    }
103
104
    public function getUri()
105
    {
106
        return $this->resource->getUri();
107
    }
108
109
    public function getExVocab()
110
    {
111
        $exvocab = $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId());
112
        return $exvocab;
113
    }
114
115
    public function getVocab()
116
    {
117
        return $this->vocab;
118
    }
119
120
    public function getVocabName($lang = '')
121
    {
122
123
        if ($this->clang) {
124
            $lang = $this->clang;
125
        }
126
127
        // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
128
        $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId());
129
        if ($exvocab) {
130
            return $exvocab->getTitle($lang);
131
        }
132
133
        // @codeCoverageIgnoreStart
134
        $scheme = $this->resource->get('skos:inScheme');
135
        if ($scheme) {
136
            $schemeResource = $this->model->getResourceFromUri($scheme->getUri());
137
            if ($schemeResource) {
138
                $schemaName = $this->getResourceLabel($schemeResource);
139
                if ($schemaName) {
140
                    //var_dump($schemaName);
141
                    return $schemaName;
142
                }
143
            }
144
        }
145
        // got a label for the concept, but not the scheme - use the host name as scheme label
146
        return parse_url($this->resource->getUri(), PHP_URL_HOST);
147
        // @codeCoverageIgnoreEnd
148
    }
149
150
    public function isExternal() {
151
        $propertyUris = $this->resource->propertyUris();
152
        return empty($propertyUris);
153
    }
154
155
    public function getNotation()
156
    {
157
        if ($this->resource->get('skos:notation')) {
158
            return $this->resource->get('skos:notation')->getValue();
159
        }
160
161
        $exvocab = $this->getExvocab();
162
163
        // if the resource is from a another vocabulary known by the skosmos instance
164
        if ($exvocab) {
165
            return $this->getExternalNotation($exvocab, $this->getUri());
166
        }
167
        return null;
168
    }
169
170
    /**
171
     * Return the mapping as a JSKOS-compatible array.
172
     * @return array
173
     */
174
    public function asJskos($queryExVocabs = true)
175
    {
176
        $ret = [
177
            'type' => [$this->type],
178
            'from' => [
179
                'memberSet' => [
180
                    [
181
                        'uri' => (string) $this->source->getUri(),
182
                    ]
183
                ]
184
            ],
185
            'to' => [
186
                'memberSet' => [
187
                    [
188
                        'uri' => (string) $this->getUri()
189
                    ]
190
                ]
191
            ]
192
        ];
193
194
        $fromScheme = $this->vocab->getDefaultConceptScheme();
195
        if (isset($fromScheme)) {
196
            $ret['fromScheme'] = [
197
                'uri' => (string) $fromScheme,
198
            ];
199
        }
200
201
        $exvocab = $this->getExvocab();
202
        if (isset($exvocab)) {
203
            $ret['toScheme'] = [
204
                'uri' => (string) $exvocab->getDefaultConceptScheme(),
205
            ];
206
        }
207
208
        $notation = $this->getNotation();
209
        if (isset($notation)) {
210
            $ret['to']['memberSet'][0]['notation'] = (string) $notation;
211
        }
212
213
        $label = $this->getLabel($queryExVocabs);
0 ignored issues
show
Documentation introduced by
$queryExVocabs is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
214
        if (isset($label)) {
215
            if (is_string($label)) {
216
                list($labelLang, $labelValue) = ['-', $label];
217
            } else {
218
                list($labelLang, $labelValue) = [$label->getLang(), $label->getValue()];
219
            }
220
            if ($labelValue != $this->getUri()) {
221
                // The `queryLabel()` method above will fallback to returning the URI
222
                // if no label was found. We don't want that here.
223
                $ret['to']['memberSet'][0]['prefLabel'] = [
224
                    $labelLang => $labelValue,
225
                ];
226
            }
227
        }
228
229
        return $ret;
230
    }
231
232
}
233
234