Completed
Push — master ( 312eb0...ebf3ad )
by Henri
08:11
created

ConceptSearchParameters   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 162
Duplicated Lines 8.64 %

Coupling/Cohesion

Components 2
Dependencies 0
Metric Value
wmc 55
lcom 2
cbo 0
dl 14
loc 162
rs 6.8

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getLang() 0 7 3
A getVocabs() 0 10 3
B getVocabIds() 8 16 8
A setVocabularies() 0 4 1
A getArrayClass() 0 8 2
B getSearchTerm() 0 8 5
A getContentLang() 0 4 1
A getConceptGroups() 0 4 1
A getSearchLang() 0 7 3
B getTypeLimit() 6 20 11
A getGroupLimit() 0 4 2
A getParentLimit() 0 4 2
A getOffset() 0 4 4
A getSearchLimit() 0 4 1
A getUnique() 0 3 1
A setUnique() 0 3 1
A getAdditionalFields() 0 3 2
A getHidden() 0 3 1
A setHidden() 0 3 1
A setAdditionalFields() 0 3 1

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 ConceptSearchParameters 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 ConceptSearchParameters, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * ConceptSearchParameters is used for search parameters.
5
 */
6
class ConceptSearchParameters
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
9
    private $config;
10
    private $request;
11
    private $vocabs;
12
    private $rest;
13
    private $hidden;
14
    private $unique;
15
16
    public function __construct($request, $config, $rest = false) 
17
    {
18
        $this->request = $request;
19
        $this->config = $config;
20
        $this->rest = $rest;
21
        $this->hidden = true;
22
        $this->unique = $request->getQueryParamBoolean('unique', false);
23
    }
24
25
    public function getLang() 
26
    {
27
        if ($this->rest && $this->request->getQueryParam('labellang')) {
28
            return $this->request->getQueryParam('labellang');
29
        }
30
        return $this->request->getLang();
31
    } 
32
33
    public function getVocabs() 
34
    {
35
        if ($this->vocabs) {
36
            return $this->vocabs;
37
        }
38
        if ($this->request->getVocab()) {
39
            return array($this->request->getVocab());
40
        }
41
        return array();
42
    } 
43
44
    public function getVocabIds()
45
    {
46 View Code Duplication
        if ($this->rest) {
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...
47
            $vocabs = $this->request->getQueryParam('vocab');
48
            return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
49
        }
50 View Code Duplication
        if ($this->request->getQueryParam('vocabs')) {
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...
51
            $vocabs = $this->request->getQueryParam('vocabs'); 
52
            return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
53
        }
54
        $vocabs = $this->getVocabs();
55
        if ($vocabs[0]) {
56
            return array($vocabs[0]->getId());
57
        }
58
        return null;
59
    }
60
61
    public function setVocabularies($vocabs) 
62
    {
63
        $this->vocabs = $vocabs;
64
    }
65
    
66
    public function getArrayClass() 
67
    {
68
        if (sizeof($this->getVocabs()) == 1) { // search within vocabulary
69
            $vocabs = $this->getVocabs();
70
            return $vocabs[0]->getConfig()->getArrayClassURI();
71
        }
72
        return null;
73
    }
74
    
75
    public function getSearchTerm() 
76
    {
77
        $term = $this->request->getQueryParam('q') ? $this->request->getQueryParam('q') : $this->request->getQueryParam('query');
78
        if (!$term && $this->rest)
79
            $term = $this->request->getQueryParam('label');
80
        $term = trim($term); // surrounding whitespace is not considered significant
81
        return strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search
82
    }
83
    
84
    public function getContentLang() 
85
    {
86
        return $this->request->getContentLang();
87
    }
88
        
89
    public function getConceptGroups() 
90
    {
91
        return $this->vocab->listConceptGroups($content_lang);
0 ignored issues
show
Bug introduced by
The property vocab does not seem to exist. Did you mean vocabs?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The variable $content_lang does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
92
    }
93
    
94
    public function getSearchLang() 
95
    {
96
        if ($this->rest) {
97
            return $this->request->getQueryParam('lang');
98
        }
99
        return $this->request->getQueryParam('anylang') ? '' : $this->getContentLang();
100
    }
101
102
    public function getTypeLimit() 
103
    {
104
        $type = $this->request->getQueryParam('type') !== '' ? $this->request->getQueryParam('type') : null;
105
        if ($type && strpos($type, '+')) {
106
            $type = explode('+', $type);
107
        } else if ($type && !is_array($type)) {
108
            // if only one type param given place it into an array regardless
109
            $type = array($type);
110
        }
111
        if ($type === null) {
112
            $type = array('skos:Concept');
113 View Code Duplication
            if ($this->request->getVocab() && $this->request->getVocab()->getConfig()->getArrayClassURI()) {
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...
114
                array_push($type, $this->request->getVocab()->getConfig()->getArrayClassURI());
115
            }
116 View Code Duplication
            if ($this->request->getVocab() && $this->request->getVocab()->getConfig()->getGroupClassURI()) {
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...
117
                array_push($type, $this->request->getVocab()->getConfig()->getGroupClassURI());
118
            }
119
        }
120
        return $type;
121
    }
122
123
    public function getGroupLimit() 
124
    {
125
        return $this->request->getQueryParam('group') !== '' ? $this->request->getQueryParam('group') : null;
126
    }
127
    
128
    public function getParentLimit() 
129
    {
130
        return $this->request->getQueryParam('parent') !== '' ? $this->request->getQueryParam('parent') : null;
131
    }
132
133
    public function getOffset() 
134
    {
135
        return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0;
136
    }
137
138
    public function getSearchLimit()
139
    {
140
        return $this->config->getDefaultSearchLimit();
141
    }
142
143
    public function getUnique() {
144
        return $this->unique;
145
    }
146
147
    public function setUnique($unique) {
148
        $this->unique = $unique;
149
    }
150
151
    public function getAdditionalFields() {
152
        return $this->request->getQueryParam('fields') ? explode(' ', $request->getQueryParam('fields')) : null;
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
153
    }
154
    
155
    public function getHidden() {
156
        return $this->hidden;
157
    }
158
    
159
    public function setHidden($hidden) {
160
        $this->hidden = $hidden;
161
    }
162
    
163
    public function setAdditionalFields($fields) {
164
        $this->fields = $fields;
0 ignored issues
show
Bug introduced by
The property fields does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
165
    }
166
167
}
168