Passed
Push — feature/v4 ( e5e380...dac4aa )
by Samuel
11:37
created

Autocomplete   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 51
eloc 63
c 1
b 0
f 0
dl 0
loc 258
ccs 114
cts 114
cp 1
rs 7.92

41 Methods

Rating   Name   Duplication   Size   Complexity  
A hasValue() 0 3 1
A addComponents() 0 4 2
A addInputAttributes() 0 4 2
A getComponents() 0 3 1
A getLibraries() 0 3 1
A getInputAttribute() 0 3 2
A getEventManager() 0 3 1
A hasBound() 0 3 1
A hasComponent() 0 3 1
A setBound() 0 3 1
A removeLibrary() 0 4 2
A addLibraries() 0 4 2
A hasType() 0 3 1
A hasTypes() 0 3 1
A setLibraries() 0 4 1
A addType() 0 4 2
A removeType() 0 4 2
A __construct() 0 3 1
A hasInputAttribute() 0 3 1
A hasLibraries() 0 3 1
A removeInputAttribute() 0 3 1
A setInputId() 0 3 1
A hasLibrary() 0 3 1
A getTypes() 0 3 1
A hasInputAttributes() 0 3 1
A hasComponents() 0 3 1
A setComponents() 0 4 1
A removeComponent() 0 3 1
A setTypes() 0 4 1
A setValue() 0 3 1
A getInputAttributes() 0 3 1
A getBound() 0 3 1
A setComponent() 0 3 1
A setInputAttribute() 0 3 1
A getComponent() 0 3 2
A getHtmlId() 0 3 1
A setEventManager() 0 3 1
A setInputAttributes() 0 4 1
A addTypes() 0 4 2
A addLibrary() 0 4 2
A getValue() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Autocomplete 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.

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

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Ivory Google Map package.
7
 *
8
 * (c) Eric GELOEN <[email protected]>
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ivory\GoogleMap\Place;
15
16
use Ivory\GoogleMap\Base\Bound;
17
use Ivory\GoogleMap\Event\EventManager;
18
use Ivory\GoogleMap\Utility\VariableAwareInterface;
19
use Ivory\GoogleMap\Utility\VariableAwareTrait;
20
21
class Autocomplete implements VariableAwareInterface
22
{
23
    use VariableAwareTrait;
24
25
    /** @var string */
26
    private $inputId = 'place_input';
27
28
    /** @var EventManager */
29
    private $eventManager;
30
31
    /** @var Bound|null */
32
    private $bound;
33
34
    /** @var string[] */
35
    private $types = [];
36
37
    /** @var array */
38
    private $components = [];
39
40
    /** @var string */
41
    private $value;
42
43
    /** @var string[] */
44
    private $inputAttributes = [];
45
46
    /** @var string[] */
47
    private $libraries = [];
48
49 34
    public function __construct()
50
    {
51 34
        $this->setEventManager(new EventManager());
52 34
    }
53
54 6
    public function getHtmlId(): string
55
    {
56 6
        return $this->inputId;
57
    }
58
59 1
    public function setInputId(string $inputId): void
60
    {
61 1
        $this->inputId = $inputId;
62 1
    }
63
64 6
    public function getEventManager(): EventManager
65
    {
66 6
        return $this->eventManager;
67
    }
68
69 34
    public function setEventManager(EventManager $eventManager): void
70
    {
71 34
        $this->eventManager = $eventManager;
72 34
    }
73
74 9
    public function hasBound(): bool
75
    {
76 9
        return null !== $this->bound;
77
    }
78
79 6
    public function getBound(): ?Bound
80
    {
81 6
        return $this->bound;
82
    }
83
84 5
    public function setBound(Bound $bound = null): void
85
    {
86 5
        $this->bound = $bound;
87 5
    }
88
89 7
    public function hasTypes(): bool
90
    {
91 7
        return !empty($this->types);
92
    }
93
94
    /** @return string[] */
95 6
    public function getTypes(): array
96
    {
97 6
        return $this->types;
98
    }
99
100
    /** @param string[] $types */
101 3
    public function setTypes(array $types): void
102
    {
103 3
        $this->types = [];
104 3
        $this->addTypes($types);
105 3
    }
106
107
    /** @param string[] $types */
108 3
    public function addTypes(array $types): void
109
    {
110 3
        foreach ($types as $type) {
111 3
            $this->addType($type);
112
        }
113 3
    }
114
115 5
    public function hasType(string $type): bool
116
    {
117 5
        return in_array($type, $this->types, true);
118
    }
119
120 5
    public function addType(string $type): void
121
    {
122 5
        if (!$this->hasType($type)) {
123 5
            $this->types[] = $type;
124
        }
125 5
    }
126
127 1
    public function removeType(string $type): void
128
    {
129 1
        unset($this->types[array_search($type, $this->types, true)]);
130 1
        $this->types = empty($this->types) ? [] : array_values($this->types);
131 1
    }
132
133 7
    public function hasComponents(): bool
134
    {
135 7
        return !empty($this->components);
136
    }
137
138 6
    public function getComponents(): array
139
    {
140 6
        return $this->components;
141
    }
142
143 3
    public function setComponents(array $components): void
144
    {
145 3
        $this->components = [];
146 3
        $this->addComponents($components);
147 3
    }
148
149 3
    public function addComponents(array $components): void
150
    {
151 3
        foreach ($components as $type => $value) {
152 3
            $this->setComponent($type, $value);
153
        }
154 3
    }
155
156 3
    public function hasComponent(string $type): bool
157
    {
158 3
        return isset($this->components[$type]);
159
    }
160
161 3
    public function getComponent(string $type)
162
    {
163 3
        return $this->hasComponent($type) ? $this->components[$type] : null;
164
    }
165
166 5
    public function setComponent(string $type, $value): void
167
    {
168 5
        $this->components[$type] = $value;
169 5
    }
170
171 1
    public function removeComponent(string $type): void
172
    {
173 1
        unset($this->components[$type]);
174 1
    }
175
176 3
    public function hasValue(): bool
177
    {
178 3
        return null !== $this->value;
179
    }
180
181 2
    public function getValue(): ?string
182
    {
183 2
        return $this->value;
184
    }
185
186 1
    public function setValue(string $value = null): void
187
    {
188 1
        $this->value = $value;
189 1
    }
190
191 5
    public function hasInputAttributes(): bool
192
    {
193 5
        return !empty($this->inputAttributes);
194
    }
195
196
    /** @return string[] */
197 7
    public function getInputAttributes(): array
198
    {
199 7
        return $this->inputAttributes;
200
    }
201
202
    /** @param string[] $inputAttributes */
203 2
    public function setInputAttributes(array $inputAttributes): void
204
    {
205 2
        $this->inputAttributes = [];
206 2
        $this->addInputAttributes($inputAttributes);
207 2
    }
208
209
    /** @param string[] $inputAttributes */
210 2
    public function addInputAttributes(array $inputAttributes): void
211
    {
212 2
        foreach ($inputAttributes as $name => $value) {
213 2
            $this->setInputAttribute($name, $value);
214
        }
215 2
    }
216
217 3
    public function hasInputAttribute(string $name): bool
218
    {
219 3
        return isset($this->inputAttributes[$name]);
220
    }
221
222 3
    public function getInputAttribute(string $name): ?string
223
    {
224 3
        return $this->hasInputAttribute($name) ? $this->inputAttributes[$name] : null;
225
    }
226
227 4
    public function setInputAttribute(string $name, string $value): void
228
    {
229 4
        $this->inputAttributes[$name] = $value;
230 4
    }
231
232 1
    public function removeInputAttribute(string $name): void
233
    {
234 1
        unset($this->inputAttributes[$name]);
235 1
    }
236
237 5
    public function hasLibraries(): bool
238
    {
239 5
        return !empty($this->libraries);
240
    }
241
242
    /** @return string[] */
243 5
    public function getLibraries(): array
244
    {
245 5
        return $this->libraries;
246
    }
247
248
    /** @param string[] $libraries */
249 2
    public function setLibraries(array $libraries): void
250
    {
251 2
        $this->libraries = [];
252 2
        $this->addLibraries($libraries);
253 2
    }
254
255
    /** @param string[] $libraries */
256 2
    public function addLibraries(array $libraries): void
257
    {
258 2
        foreach ($libraries as $library) {
259 2
            $this->addLibrary($library);
260
        }
261 2
    }
262
263 4
    public function hasLibrary(string $library): bool
264
    {
265 4
        return in_array($library, $this->libraries, true);
266
    }
267
268 4
    public function addLibrary(string $library): void
269
    {
270 4
        if (!$this->hasLibrary($library)) {
271 4
            $this->libraries[] = $library;
272
        }
273 4
    }
274
275 1
    public function removeLibrary(string $library): void
276
    {
277 1
        unset($this->libraries[array_search($library, $this->libraries, true)]);
278 1
        $this->libraries = empty($this->libraries) ? [] : array_values($this->libraries);
279 1
    }
280
}
281