Completed
Push — master ( 0932c1...0a23f4 )
by Tobias
23:05
created

GoogleAddress   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 512
Duplicated Lines 0 %

Coupling/Cohesion

Components 19
Dependencies 3

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
wmc 41
lcom 19
cbo 3
dl 0
loc 512
ccs 104
cts 116
cp 0.8966
rs 9.1199
c 0
b 0
f 0

38 Methods

Rating   Name   Duplication   Size   Complexity  
A withId() 0 7 1
A getId() 0 4 1
A withLocationType() 0 7 1
A getLocationType() 0 4 1
A getResultType() 0 4 1
A withResultType() 0 7 1
A getFormattedAddress() 0 4 1
A withFormattedAddress() 0 7 1
A getAirport() 0 4 1
A withAirport() 0 7 1
A getColloquialArea() 0 4 1
A withColloquialArea() 0 7 1
A getIntersection() 0 4 1
A withIntersection() 0 7 1
A getNaturalFeature() 0 4 1
A withNaturalFeature() 0 7 1
A getNeighborhood() 0 4 1
A withNeighborhood() 0 7 1
A getPark() 0 4 1
A withPark() 0 7 1
A getPointOfInterest() 0 4 1
A withPointOfInterest() 0 7 1
A getPolitical() 0 4 1
A withPolitical() 0 7 1
A getPremise() 0 4 1
A withPremise() 0 7 1
A getStreetAddress() 0 4 1
A withStreetAddress() 0 7 1
A getSubpremise() 0 4 1
A withSubpremise() 0 7 1
A getWard() 0 4 1
A withWard() 0 7 1
A getEstablishment() 0 4 1
A withEstablishment() 0 7 1
A getSubLocalityLevels() 0 4 1
A withSubLocalityLevels() 0 21 4
A isPartialMatch() 0 4 1
A withPartialMatch() 0 7 1

How to fix   Complexity   

Complex Class

Complex classes like GoogleAddress 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 GoogleAddress, 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 Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\GoogleMaps\Model;
14
15
use Geocoder\Model\Address;
16
use Geocoder\Model\AdminLevel;
17
use Geocoder\Model\AdminLevelCollection;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
final class GoogleAddress extends Address
23
{
24
    /**
25
     * @var string|null
26
     */
27
    private $id;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $locationType;
33
34
    /**
35
     * @var array
36
     */
37
    private $resultType = [];
38
39
    /**
40
     * @var string|null
41
     */
42
    private $formattedAddress;
43
44
    /**
45
     * @var string|null
46
     */
47
    private $streetAddress;
48
49
    /**
50
     * @var string|null
51
     */
52
    private $intersection;
53
54
    /**
55
     * @var string|null
56
     */
57
    private $political;
58
59
    /**
60
     * @var string|null
61
     */
62
    private $colloquialArea;
63
64
    /**
65
     * @var string|null
66
     */
67
    private $ward;
68
69
    /**
70
     * @var string|null
71
     */
72
    private $neighborhood;
73
74
    /**
75
     * @var string|null
76
     */
77
    private $premise;
78
79
    /**
80
     * @var string|null
81
     */
82
    private $subpremise;
83
84
    /**
85
     * @var string|null
86
     */
87
    private $naturalFeature;
88
89
    /**
90
     * @var string|null
91
     */
92
    private $airport;
93
94
    /**
95
     * @var string|null
96
     */
97
    private $park;
98
99
    /**
100
     * @var string|null
101
     */
102
    private $pointOfInterest;
103
104
    /**
105
     * @var string|null
106
     */
107
    private $establishment;
108
109
    /**
110
     * @var AdminLevelCollection
111
     */
112
    private $subLocalityLevels;
113
114
    /**
115
     * @var bool
116
     */
117
    private $partialMatch;
118
119 17
    /**
120
     * @param null|string $id
121 17
     *
122 17
     * @return GoogleAddress
123
     */
124 17
    public function withId(string $id = null)
125
    {
126
        $new = clone $this;
127
        $new->id = $id;
128
129
        return $new;
130
    }
131
132 3
    /**
133
     * @see https://developers.google.com/places/place-id
134 3
     *
135
     * @return null|string
136
     */
137
    public function getId()
138
    {
139
        return $this->id;
140
    }
141
142 17
    /**
143
     * @param null|string $locationType
144 17
     *
145 17
     * @return GoogleAddress
146
     */
147 17
    public function withLocationType(string $locationType = null)
148
    {
149
        $new = clone $this;
150
        $new->locationType = $locationType;
151
152
        return $new;
153
    }
154
155
    /**
156
     * @return null|string
157
     */
158
    public function getLocationType()
159
    {
160
        return $this->locationType;
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    public function getResultType(): array
167
    {
168
        return $this->resultType;
169
    }
170
171 17
    /**
172
     * @param array $resultType
173 17
     *
174 17
     * @return GoogleAddress
175
     */
176 17
    public function withResultType(array $resultType)
177
    {
178
        $new = clone $this;
179
        $new->resultType = $resultType;
180
181
        return $new;
182
    }
183
184
    /**
185
     * @return null|string
186
     */
187
    public function getFormattedAddress()
188
    {
189
        return $this->formattedAddress;
190
    }
191
192 17
    /**
193
     * @param string|null $formattedAddress
194 17
     *
195 17
     * @return GoogleAddress
196
     */
197 17
    public function withFormattedAddress(string $formattedAddress = null)
198
    {
199
        $new = clone $this;
200
        $new->formattedAddress = $formattedAddress;
201
202
        return $new;
203 1
    }
204
205 1
    /**
206
     * @return null|string
207
     */
208
    public function getAirport()
209
    {
210
        return $this->airport;
211
    }
212
213 17
    /**
214
     * @param string|null $airport
215 17
     *
216 17
     * @return GoogleAddress
217
     */
218 17
    public function withAirport(string $airport = null)
219
    {
220
        $new = clone $this;
221
        $new->airport = $airport;
222
223
        return $new;
224 1
    }
225
226 1
    /**
227
     * @return null|string
228
     */
229
    public function getColloquialArea()
230
    {
231
        return $this->colloquialArea;
232
    }
233
234 17
    /**
235
     * @param string|null $colloquialArea
236 17
     *
237 17
     * @return GoogleAddress
238
     */
239 17
    public function withColloquialArea(string $colloquialArea = null)
240
    {
241
        $new = clone $this;
242
        $new->colloquialArea = $colloquialArea;
243
244
        return $new;
245
    }
246
247
    /**
248
     * @return null|string
249
     */
250
    public function getIntersection()
251
    {
252
        return $this->intersection;
253
    }
254
255 17
    /**
256
     * @param string|null $intersection
257 17
     *
258 17
     * @return GoogleAddress
259
     */
260 17
    public function withIntersection(string $intersection = null)
261
    {
262
        $new = clone $this;
263
        $new->intersection = $intersection;
264
265
        return $new;
266 1
    }
267
268 1
    /**
269
     * @return null|string
270
     */
271
    public function getNaturalFeature()
272
    {
273
        return $this->naturalFeature;
274
    }
275
276 17
    /**
277
     * @param string|null $naturalFeature
278 17
     *
279 17
     * @return GoogleAddress
280
     */
281 17
    public function withNaturalFeature(string $naturalFeature = null)
282
    {
283
        $new = clone $this;
284
        $new->naturalFeature = $naturalFeature;
285
286
        return $new;
287 1
    }
288
289 1
    /**
290
     * @return null|string
291
     */
292
    public function getNeighborhood()
293
    {
294
        return $this->neighborhood;
295
    }
296
297 17
    /**
298
     * @param string|null $neighborhood
299 17
     *
300 17
     * @return GoogleAddress
301
     */
302 17
    public function withNeighborhood(string $neighborhood = null)
303
    {
304
        $new = clone $this;
305
        $new->neighborhood = $neighborhood;
306
307
        return $new;
308 1
    }
309
310 1
    /**
311
     * @return null|string
312
     */
313
    public function getPark()
314
    {
315
        return $this->park;
316
    }
317
318 17
    /**
319
     * @param string|null $park
320 17
     *
321 17
     * @return GoogleAddress
322
     */
323 17
    public function withPark(string $park = null)
324
    {
325
        $new = clone $this;
326
        $new->park = $park;
327
328
        return $new;
329 2
    }
330
331 2
    /**
332
     * @return null|string
333
     */
334
    public function getPointOfInterest()
335
    {
336
        return $this->pointOfInterest;
337
    }
338
339 17
    /**
340
     * @param string|null $pointOfInterest
341 17
     *
342 17
     * @return GoogleAddress
343
     */
344 17
    public function withPointOfInterest(string $pointOfInterest = null)
345
    {
346
        $new = clone $this;
347
        $new->pointOfInterest = $pointOfInterest;
348
349
        return $new;
350 1
    }
351
352 1
    /**
353
     * @return null|string
354
     */
355
    public function getPolitical()
356
    {
357
        return $this->political;
358
    }
359
360 17
    /**
361
     * @param string|null $political
362 17
     *
363 17
     * @return GoogleAddress
364
     */
365 17
    public function withPolitical(string $political = null)
366
    {
367
        $new = clone $this;
368
        $new->political = $political;
369
370
        return $new;
371 1
    }
372
373 1
    /**
374
     * @return null|string
375
     */
376
    public function getPremise()
377
    {
378
        return $this->premise;
379
    }
380
381 17
    /**
382
     * @param string $premise
383 17
     *
384 17
     * @return GoogleAddress
385
     */
386 17
    public function withPremise(string $premise = null)
387
    {
388
        $new = clone $this;
389
        $new->premise = $premise;
390
391
        return $new;
392
    }
393
394
    /**
395
     * @return null|string
396
     */
397
    public function getStreetAddress()
398
    {
399
        return $this->streetAddress;
400
    }
401
402 17
    /**
403
     * @param string|null $streetAddress
404 17
     *
405 17
     * @return GoogleAddress
406
     */
407 17
    public function withStreetAddress(string $streetAddress = null)
408
    {
409
        $new = clone $this;
410
        $new->streetAddress = $streetAddress;
411
412
        return $new;
413 1
    }
414
415 1
    /**
416
     * @return null|string
417
     */
418
    public function getSubpremise()
419
    {
420
        return $this->subpremise;
421
    }
422
423 17
    /**
424
     * @param string|null $subpremise
425 17
     *
426 17
     * @return GoogleAddress
427
     */
428 17
    public function withSubpremise(string $subpremise = null)
429
    {
430
        $new = clone $this;
431
        $new->subpremise = $subpremise;
432
433
        return $new;
434 1
    }
435
436 1
    /**
437
     * @return null|string
438
     */
439
    public function getWard()
440
    {
441
        return $this->ward;
442
    }
443
444 17
    /**
445
     * @param string|null $ward
446 17
     *
447 17
     * @return GoogleAddress
448
     */
449 17
    public function withWard(string $ward = null)
450
    {
451
        $new = clone $this;
452
        $new->ward = $ward;
453
454
        return $new;
455 1
    }
456
457 1
    /**
458
     * @return null|string
459
     */
460
    public function getEstablishment()
461
    {
462
        return $this->establishment;
463
    }
464
465 17
    /**
466
     * @param string|null $establishment
467 17
     *
468 17
     * @return GoogleAddress
469
     */
470 17
    public function withEstablishment(string $establishment = null)
471
    {
472
        $new = clone $this;
473
        $new->establishment = $establishment;
474
475
        return $new;
476 1
    }
477
478 1
    /**
479
     * @return AdminLevelCollection
480
     */
481
    public function getSubLocalityLevels()
482
    {
483
        return $this->subLocalityLevels;
484
    }
485
486 17
    /**
487
     * @param array $subLocalityLevel
488 17
     *
489 17
     * @return $this
490 6
     */
491
    public function withSubLocalityLevels(array $subLocalityLevel)
492
    {
493
        $subLocalityLevels = [];
494 6
        foreach ($subLocalityLevel as $level) {
495 6
            if (empty($level['level'])) {
496
                continue;
497
            }
498
499 6
            $name = $level['name'] ?? $level['code'] ?? '';
500
            if (empty($name)) {
501
                continue;
502 17
            }
503 17
504
            $subLocalityLevels[] = new AdminLevel($level['level'], $name, $level['code'] ?? null);
505 17
        }
506
507
        $new = clone $this;
508
        $new->subLocalityLevels = new AdminLevelCollection($subLocalityLevels);
509
510
        return $new;
511
    }
512
513
    /**
514
     * @return bool
515
     */
516
    public function isPartialMatch()
517
    {
518
        return $this->partialMatch;
519
    }
520
521
    /**
522
     * @param bool $partialMatch
523
     *
524
     * @return $this
525
     */
526
    public function withPartialMatch(bool $partialMatch)
527
    {
528
        $new = clone $this;
529
        $new->partialMatch = $partialMatch;
530
531
        return $new;
532
    }
533
}
534