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

GeocoderResult   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 45
eloc 60
c 0
b 0
f 0
dl 0
loc 227
ccs 100
cts 100
cp 1
rs 8.8

33 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAddressComponents() 0 5 1
A addPostcodeLocality() 0 4 2
A getAddressComponents() 0 15 4
A removeAddressComponent() 0 4 2
A addPostcodeLocalities() 0 4 2
A addAddressComponent() 0 4 2
A setPlaceId() 0 3 1
A addAddressComponents() 0 4 2
A hasAddressComponent() 0 3 1
A setPartialMatch() 0 3 1
A addType() 0 4 2
A hasGeometry() 0 3 1
A hasType() 0 3 1
A setFormattedAddress() 0 3 1
A hasFormattedAddress() 0 3 1
A hasPartialMatch() 0 3 1
A setAddressComponents() 0 4 1
A getPostcodeLocalities() 0 3 1
A getFormattedAddress() 0 3 1
A getPlaceId() 0 3 1
A hasPostcodeLocalities() 0 3 1
A removePostcodeLocality() 0 4 2
A setPostcodeLocalities() 0 4 1
A addTypes() 0 4 2
A hasTypes() 0 3 1
A hasPlaceId() 0 3 1
A setTypes() 0 4 1
A setGeometry() 0 3 1
A getGeometry() 0 3 1
A getTypes() 0 3 1
A hasPostcodeLocality() 0 3 1
A isPartialMatch() 0 3 1
A removeType() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like GeocoderResult 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 GeocoderResult, 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\Service\Geocoder\Response;
15
16
use Ivory\GoogleMap\Service\Base\AddressComponent;
17
use Ivory\GoogleMap\Service\Base\Geometry;
18
19
/**
20
 * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#GeocoderResult
21
 */
22
class GeocoderResult
23
{
24
    /** @var string|null */
25
    private $placeId;
26
27
    /** @var AddressComponent[] */
28
    private $addressComponents = [];
29
30
    /** @var string|null */
31
    private $formattedAddress;
32
33
    /** @var string[] */
34
    private $postcodeLocalities = [];
35
36
    /** @var Geometry|null */
37
    private $geometry;
38
39
    /** @var bool|null */
40
    private $partialMatch;
41
42
    /** @var string[] */
43
    private $types = [];
44
45 2
    public function hasPlaceId(): bool
46
    {
47 2
        return null !== $this->placeId;
48
    }
49
50 2
    public function getPlaceId(): ?string
51
    {
52 2
        return $this->placeId;
53
    }
54
55 1
    public function setPlaceId(?string $placeId): void
56
    {
57 1
        $this->placeId = $placeId;
58 1
    }
59
60 6
    public function hasAddressComponents(string $type = null): bool
61
    {
62 6
        $addresses = $this->getAddressComponents($type);
63
64 6
        return !empty($addresses);
65
    }
66
67
    /** @return AddressComponent[] */
68 6
    public function getAddressComponents(string $type = null): array
69
    {
70 6
        if (null === $type) {
71 5
            return $this->addressComponents;
72
        }
73
74 1
        $addressComponents = [];
75
76 1
        foreach ($this->addressComponents as $addressComponent) {
77 1
            if (in_array($type, $addressComponent->getTypes(), true)) {
78 1
                $addressComponents[] = $addressComponent;
79
            }
80
        }
81
82 1
        return $addressComponents;
83
    }
84
85
    /** @param AddressComponent[] $addressComponents */
86 3
    public function setAddressComponents(array $addressComponents): void
87
    {
88 3
        $this->addressComponents = [];
89 3
        $this->addAddressComponents($addressComponents);
90 3
    }
91
92
    /** @param AddressComponent[] $addressComponents */
93 3
    public function addAddressComponents(array $addressComponents): void
94
    {
95 3
        foreach ($addressComponents as $addressComponent) {
96 3
            $this->addAddressComponent($addressComponent);
97
        }
98 3
    }
99
100 5
    public function hasAddressComponent(AddressComponent $addressComponent): bool
101
    {
102 5
        return in_array($addressComponent, $this->addressComponents, true);
103
    }
104
105 5
    public function addAddressComponent(AddressComponent $addressComponent): void
106
    {
107 5
        if (!$this->hasAddressComponent($addressComponent)) {
108 5
            $this->addressComponents[] = $addressComponent;
109
        }
110 5
    }
111
112 1
    public function removeAddressComponent(AddressComponent $addressComponent): void
113
    {
114 1
        unset($this->addressComponents[array_search($addressComponent, $this->addressComponents, true)]);
115 1
        $this->addressComponents = empty($this->addressComponents) ? [] : array_values($this->addressComponents);
116 1
    }
117
118 3
    public function hasFormattedAddress(): bool
119
    {
120 3
        return !empty($this->formattedAddress);
121
    }
122
123 3
    public function getFormattedAddress(): ?string
124
    {
125 3
        return $this->formattedAddress;
126
    }
127
128 2
    public function setFormattedAddress(string $formattedAddress = null): void
129
    {
130 2
        $this->formattedAddress = $formattedAddress;
131 2
    }
132
133 5
    public function hasPostcodeLocalities(): bool
134
    {
135 5
        return !empty($this->postcodeLocalities);
136
    }
137
138
    /** @return string[] */
139 5
    public function getPostcodeLocalities(): array
140
    {
141 5
        return $this->postcodeLocalities;
142
    }
143
144
    /** @param string[] $postcodeLocalities */
145 2
    public function setPostcodeLocalities(array $postcodeLocalities): void
146
    {
147 2
        $this->postcodeLocalities = [];
148 2
        $this->addPostcodeLocalities($postcodeLocalities);
149 2
    }
150
151
    /** @param string[] $postcodeLocalities */
152 2
    public function addPostcodeLocalities(array $postcodeLocalities): void
153
    {
154 2
        foreach ($postcodeLocalities as $postcodeLocality) {
155 2
            $this->addPostcodeLocality($postcodeLocality);
156
        }
157 2
    }
158
159 4
    public function hasPostcodeLocality(string $postcodeLocality): bool
160
    {
161 4
        return in_array($postcodeLocality, $this->postcodeLocalities, true);
162
    }
163
164 4
    public function addPostcodeLocality(string $postcodeLocality): void
165
    {
166 4
        if (!$this->hasPostcodeLocality($postcodeLocality)) {
167 4
            $this->postcodeLocalities[] = $postcodeLocality;
168
        }
169 4
    }
170
171 1
    public function removePostcodeLocality(string $postcodeLocality): void
172
    {
173 1
        unset($this->postcodeLocalities[array_search($postcodeLocality, $this->postcodeLocalities, true)]);
174 1
        $this->postcodeLocalities = empty($this->postcodeLocalities) ? [] : array_values($this->postcodeLocalities);
175 1
    }
176
177 3
    public function hasGeometry(): bool
178
    {
179 3
        return null !== $this->geometry;
180
    }
181
182 3
    public function getGeometry(): ?Geometry
183
    {
184 3
        return $this->geometry;
185
    }
186
187 2
    public function setGeometry(Geometry $geometry = null): void
188
    {
189 2
        $this->geometry = $geometry;
190 2
    }
191
192 3
    public function hasPartialMatch(): bool
193
    {
194 3
        return null !== $this->partialMatch;
195
    }
196
197 3
    public function isPartialMatch(): ?bool
198
    {
199 3
        return $this->partialMatch;
200
    }
201
202 2
    public function setPartialMatch(bool $partialMatch = null): void
203
    {
204 2
        $this->partialMatch = $partialMatch;
205 2
    }
206
207 5
    public function hasTypes(): bool
208
    {
209 5
        return !empty($this->types);
210
    }
211
212
    /** @return string[] */
213 5
    public function getTypes(): array
214
    {
215 5
        return $this->types;
216
    }
217
218
    /** @param string[] $types */
219 2
    public function setTypes(array $types): void
220
    {
221 2
        $this->types = [];
222 2
        $this->addTypes($types);
223 2
    }
224
225
    /** @param string[] $types */
226 2
    public function addTypes(array $types): void
227
    {
228 2
        foreach ($types as $type) {
229 2
            $this->addType($type);
230
        }
231 2
    }
232
233 4
    public function hasType(string $type): bool
234
    {
235 4
        return in_array($type, $this->types, true);
236
    }
237
238 4
    public function addType(string $type): void
239
    {
240 4
        if (!$this->hasType($type)) {
241 4
            $this->types[] = $type;
242
        }
243 4
    }
244
245 1
    public function removeType(string $type): void
246
    {
247 1
        unset($this->types[array_search($type, $this->types, true)]);
248 1
        $this->types = empty($this->types) ? [] : array_values($this->types);
249 1
    }
250
}
251