Completed
Push — master ( 751599...c13dee )
by Osma
02:10 queued 10s
created

ConceptPropertyValue   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 170
Duplicated Lines 12.94 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 22
loc 170
rs 8.72
c 0
b 0
f 0
wmc 46
lcom 2
cbo 7

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __toString() 0 9 3
A getLang() 0 4 1
C getLabel() 14 47 17
A getType() 0 4 1
A getUri() 0 4 1
A getExVocab() 0 8 2
A getVocab() 0 4 1
A getVocabName() 0 4 1
A addSubMember() 0 6 2
A getSubMembers() 0 8 2
A sortSubMembers() 0 7 2
A isExternal() 0 3 1
A getNotation() 0 7 3
A isReified() 0 3 2
A getReifiedPropertyValues() 8 16 6

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ConceptPropertyValue 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 ConceptPropertyValue, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Class for handling concept property values.
5
 */
6
class ConceptPropertyValue extends VocabularyDataObject
7
{
8
    /** submembers */
9
    private $submembers;
10
    /** property type */
11
    private $type;
12
    /** content language */
13
    private $clang;
14
    /** whether the property value is external w.r.t. to the subject resource */
15
    private $external;
16
17
    public function __construct($model, $vocab, $resource, $prop, $clang = '', $external = false)
18
    {
19
        parent::__construct($model, $vocab, $resource);
20
        $this->submembers = array();
21
        $this->type = $prop;
22
        $this->clang = $clang;
23
        $this->external = $external;
24
    }
25
26
    public function __toString()
27
    {
28
        $label = is_string($this->getLabel()) ? $this->getLabel() : $this->getLabel()->getValue();
29
        if ($this->vocab->getConfig()->sortByNotation()) {
30
            $label = $this->getNotation() . $label;
31
        }
32
33
        return $label;
34
    }
35
36
    public function getLang()
37
    {
38
        return $this->getEnvLang();
39
    }
40
41
    public function getLabel($lang = '', $fallbackToUri = 'uri')
42
    {
43
        if ($this->clang) {
44
            $lang = $this->clang;
45
        }
46
        if ($this->vocab->getConfig()->getLanguageOrder($lang)) {
47 View Code Duplication
            foreach ($this->vocab->getConfig()->getLanguageOrder($lang) as $fallback) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
                if ($this->resource->label($fallback) !== null) {
49
                    return $this->resource->label($fallback);
50
                }
51
                // We need to check all the labels in case one of them matches a subtag of the current language
52
                if ($this->resource->allLiterals('skos:prefLabel')) {
53
                    foreach($this->resource->allLiterals('skos:prefLabel') as $label) {
54
                        // the label lang code is a subtag of the UI lang eg. en-GB - create a new literal with the main language
55
                        if ($label !== null && strpos($label->getLang(), $fallback . '-') === 0) {
56
                            return EasyRdf\Literal::create($label, $fallback);
57
                        }
58
                    }
59
                }
60
            }
61
        }
62
63
        if ($this->resource->label($lang) !== null) { // current language
64
            return $this->resource->label($lang);
65
        } elseif ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { // vocab default language
66
            return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage());
67
        } elseif ($this->resource->label() !== null) { // any language
68
            return $this->resource->label();
69
        } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language
70
            return $this->resource->getLiteral('rdf:value', $lang);
71
        } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language
72
            return $this->resource->getLiteral('rdf:value');
73
        }
74
75
        // see if we can find a label in another vocabulary known by the skosmos instance
76
        $label = $this->getExternalLabel($this->vocab, $this->getUri(), $lang);
77
        if ($label) {
78
            return $label;
79
        }
80
81
        if ($fallbackToUri == 'uri') {
82
            // return uri if no label is found
83
            $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
84
            return $label;
85
        }
86
        return null;
87
    }
88
89
    public function getType()
90
    {
91
        return $this->type;
92
    }
93
94
    public function getUri()
95
    {
96
        return $this->resource->getUri();
97
    }
98
99
    public function getExVocab()
100
    {
101
        if ($this->isExternal()) {
102
            return $this->vocab;
103
        } else {
104
            return null;
105
        }
106
    }
107
108
    public function getVocab()
109
    {
110
        return $this->vocab;
111
    }
112
113
    public function getVocabName()
114
    {
115
        return $this->vocab->getTitle();
116
    }
117
118
    public function addSubMember($member, $lang = '')
119
    {
120
        $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel();
121
        $this->submembers[$label->getValue()] = $member;
122
        $this->sortSubMembers();
123
    }
124
125
    public function getSubMembers()
126
    {
127
        if (empty($this->submembers)) {
128
            return null;
129
        }
130
131
        return $this->submembers;
132
    }
133
134
    private function sortSubMembers()
135
    {
136
        if (!empty($this->submembers)) {
137
            ksort($this->submembers);
138
        }
139
140
    }
141
142
    public function isExternal() {
143
        return $this->external;
144
    }
145
146
    public function getNotation()
147
    {
148
        if ($this->vocab->getConfig()->showNotation() && $this->resource->get('skos:notation')) {
149
            return $this->resource->get('skos:notation')->getValue();
150
        }
151
152
    }
153
154
    public function isReified() {
155
        return (!$this->resource->label() && $this->resource->getLiteral('rdf:value'));
156
    }
157
158
    public function getReifiedPropertyValues() {
159
        $ret = array();
160
        $props = $this->resource->propertyUris();
161
        foreach($props as $prop) {
162
            $prop = (EasyRdf\RdfNamespace::shorten($prop) !== null) ? EasyRdf\RdfNamespace::shorten($prop) : $prop;
163 View Code Duplication
            foreach ($this->resource->allLiterals($prop) as $val) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
                if ($prop !== 'rdf:value') { // shown elsewhere
165
                    $ret[gettext($prop)] = new ConceptPropertyValueLiteral($this->model, $this->vocab, $this->resource, $val, $prop);
166
                }
167
            }
168 View Code Duplication
            foreach ($this->resource->allResources($prop) as $val) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
                $ret[gettext($prop)] = new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang);
170
            }
171
        }
172
        return $ret;
173
    }
174
175
}
176