Completed
Push — master ( 4ef44e...f0613e )
by Henri
03:01
created

ConceptPropertyValue   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 149
Duplicated Lines 14.09 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 43
lcom 2
cbo 7
dl 21
loc 149
rs 8.3157
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A __toString() 0 9 3
A getLang() 0 4 1
C getLabel() 14 37 15
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
B getReifiedPropertyValues() 0 16 7

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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /** submembers */
9
    private $submembers;
10
    /** property type */
11
    private $type;
12
    /** content language */
13
    private $clang;
14
15 View Code Duplication
    public function __construct($model, $vocab, $resource, $prop, $clang = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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 = '')
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
        // uri if no label is found
72
        $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
73
        return $label;
74
    }
75
76
    public function getType()
77
    {
78
        return $this->type;
79
    }
80
81
    public function getUri()
82
    {
83
        return $this->resource->getUri();
84
    }
85
86
    public function getVocab()
87
    {
88
        return $this->vocab;
89
    }
90
91
    public function getVocabName()
92
    {
93
        return $this->vocab->getTitle();
94
    }
95
96
    public function addSubMember($member, $lang = '')
97
    {
98
        $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel();
99
        $this->submembers[$label->getValue()] = $member;
100
        $this->sortSubMembers();
101
    }
102
103
    public function getSubMembers()
104
    {
105
        if (empty($this->submembers)) {
106
            return null;
107
        }
108
109
        return $this->submembers;
110
    }
111
112
    private function sortSubMembers()
113
    {
114
        if (!empty($this->submembers)) {
115
            ksort($this->submembers);
116
        }
117
118
    }
119
120
    public function isExternal() {
121
        $propertyUris = $this->resource->propertyUris();
122
        return empty($propertyUris);
123
    }
124
125
    public function getNotation()
126
    {
127
        if ($this->vocab->getConfig()->showNotation() && $this->resource->get('skos:notation')) {
128
            return $this->resource->get('skos:notation')->getValue();
129
        }
130
131
    }
132
133
    public function isReified() {
134
        return (!$this->resource->label() && $this->resource->getLiteral('rdf:value'));
135
    }
136
    
137
    public function getReifiedPropertyValues() {
138
        $ret = array();
139
        $props = $this->resource->propertyUris();
140
        foreach($props as $prop) {
141
            $prop = (EasyRdf\RdfNamespace::shorten($prop) !== null) ? EasyRdf\RdfNamespace::shorten($prop) : $prop;
142
            foreach ($this->resource->allLiterals($prop) as $val) {
143
                if ($prop !== 'rdf:value' && $this->resource->get($prop)) { // shown elsewhere
144
                    $ret[gettext($prop)] = new ConceptPropertyValueLiteral($this->resource->get($prop), $prop);
0 ignored issues
show
Bug introduced by
The call to ConceptPropertyValueLiteral::__construct() misses some required arguments starting with $resource.
Loading history...
145
                }
146
            }
147
            foreach ($this->resource->allResources($prop) as $val) {
148
                $ret[gettext($prop)] = new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang);
149
            }
150
        }
151
        return $ret;
152
    }
153
154
}
155