Completed
Push — master ( bcbd9b...28b3c2 )
by Osma
09:04 queued 02:58
created

ConceptPropertyValue   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 153
Duplicated Lines 14.38 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 22
loc 153
rs 8.96
c 0
b 0
f 0
wmc 43
lcom 2
cbo 7

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 9 3
A getLang() 0 4 1
A __construct() 0 7 1
C getLabel() 14 41 16
A getType() 0 4 1
A getUri() 0 4 1
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 4 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
15
    public function __construct($model, $vocab, $resource, $prop, $clang = '')
16
    {
17
        parent::__construct($model, $vocab, $resource);
18
        $this->submembers = array();
19
        $this->type = $prop;
20
        $this->clang = $clang;
21
    }
22
23
    public function __toString()
24
    {
25
        $label = is_string($this->getLabel()) ? $this->getLabel() : $this->getLabel()->getValue();
26
        if ($this->vocab->getConfig()->sortByNotation()) {
27
            $label = $this->getNotation() . $label;
28
        }
29
30
        return $label;
31
    }
32
33
    public function getLang()
34
    {
35
        return $this->getEnvLang();
36
    }
37
38
    public function getLabel($lang = '', $fallback = 'uri')
39
    {
40
        if ($this->clang) {
41
            $lang = $this->clang;
42
        }
43
        if ($this->vocab->getConfig()->getLanguageOrder($lang)) {
44 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...
45
                if ($this->resource->label($fallback) !== null) {
46
                    return $this->resource->label($fallback);
47
                }
48
                // We need to check all the labels in case one of them matches a subtag of the current language
49
                if ($this->resource->allLiterals('skos:prefLabel')) {
50
                    foreach($this->resource->allLiterals('skos:prefLabel') as $label) {
51
                        // the label lang code is a subtag of the UI lang eg. en-GB - create a new literal with the main language
52
                        if ($label !== null && strpos($label->getLang(), $fallback . '-') === 0) {
53
                            return EasyRdf\Literal::create($label, $fallback);
54
                        }
55
                    }
56
                }
57
            }
58
        }
59
60
        if ($this->resource->label($lang) !== null) { // current language
61
            return $this->resource->label($lang);
62
        } elseif ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { // vocab default language
63
            return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage());
64
        } elseif ($this->resource->label() !== null) { // any language
65
            return $this->resource->label();
66
        } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language
67
            return $this->resource->getLiteral('rdf:value', $lang);
68
        } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language
69
            return $this->resource->getLiteral('rdf:value');
70
        }
71
72
        if ($fallback == 'uri') {
73
            // return uri if no label is found
74
            $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
75
            return $label;
76
        }
77
        return null;
78
    }
79
80
    public function getType()
81
    {
82
        return $this->type;
83
    }
84
85
    public function getUri()
86
    {
87
        return $this->resource->getUri();
88
    }
89
90
    public function getVocab()
91
    {
92
        return $this->vocab;
93
    }
94
95
    public function getVocabName()
96
    {
97
        return $this->vocab->getTitle();
98
    }
99
100
    public function addSubMember($member, $lang = '')
101
    {
102
        $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel();
103
        $this->submembers[$label->getValue()] = $member;
104
        $this->sortSubMembers();
105
    }
106
107
    public function getSubMembers()
108
    {
109
        if (empty($this->submembers)) {
110
            return null;
111
        }
112
113
        return $this->submembers;
114
    }
115
116
    private function sortSubMembers()
117
    {
118
        if (!empty($this->submembers)) {
119
            ksort($this->submembers);
120
        }
121
122
    }
123
124
    public function isExternal() {
125
        $propertyUris = $this->resource->propertyUris();
126
        return empty($propertyUris);
127
    }
128
129
    public function getNotation()
130
    {
131
        if ($this->vocab->getConfig()->showNotation() && $this->resource->get('skos:notation')) {
132
            return $this->resource->get('skos:notation')->getValue();
133
        }
134
135
    }
136
137
    public function isReified() {
138
        return (!$this->resource->label() && $this->resource->getLiteral('rdf:value'));
139
    }
140
141
    public function getReifiedPropertyValues() {
142
        $ret = array();
143
        $props = $this->resource->propertyUris();
144
        foreach($props as $prop) {
145
            $prop = (EasyRdf\RdfNamespace::shorten($prop) !== null) ? EasyRdf\RdfNamespace::shorten($prop) : $prop;
146 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...
147
                if ($prop !== 'rdf:value') { // shown elsewhere
148
                    $ret[gettext($prop)] = new ConceptPropertyValueLiteral($this->model, $this->vocab, $this->resource, $val, $prop);
149
                }
150
            }
151 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...
152
                $ret[gettext($prop)] = new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang);
153
            }
154
        }
155
        return $ret;
156
    }
157
158
}
159