Passed
Push — master ( d53d6f...24ad68 )
by Tomáš
09:09
created

Branch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 34
c 1
b 0
f 0
nc 1
nop 33
dl 0
loc 69
ccs 35
cts 35
cp 1
crap 1
rs 9.376

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\Values;
6
7
use Inspirum\Balikobot\Definitions\ServiceType;
8
use Inspirum\Balikobot\Definitions\Shipper;
9
use function sprintf;
10
use function str_replace;
11
use function trim;
12
13
class Branch
14
{
15
    /**
16
     * @var string
17
     */
18
    private string $shipper;
19
20
    /**
21
     * @var string|null
22
     */
23
    private ?string $service;
24
25
    /**
26
     * @var string
27
     */
28
    private string $branchId;
29
30
    /**
31
     * @var string|null
32
     */
33
    private ?string $id;
34
35
    /**
36
     * @var string|null
37
     */
38
    private ?string $uid;
39
40
    /**
41
     * @var string
42
     */
43
    private string $type;
44
45
    /**
46
     * @var string
47
     */
48
    private string $name;
49
50
    /**
51
     * @var string
52
     */
53
    private string $city;
54
55
    /**
56
     * @var string
57
     */
58
    private string $street;
59
60
    /**
61
     * @var string
62
     */
63
    private string $zip;
64
65
    /**
66
     * @var string|null
67
     */
68
    private ?string $cityPart;
69
70
    /**
71
     * @var string|null
72
     */
73
    private ?string $district;
74
75
    /**
76
     * @var string|null
77
     */
78
    private ?string $region;
79
80
    /**
81
     * ISO 3166-1 alpha-2 http://cs.wikipedia.org/wiki/ISO_3166-1
82
     *
83
     * @var string|null
84
     */
85
    private ?string $country;
86
87
    /**
88
     * @var string|null
89
     */
90
    private ?string $currency;
91
92
    /**
93
     * @var string|null
94
     */
95
    private ?string $photoSmall;
96
97
    /**
98
     * @var string|null
99
     */
100
    private ?string $photoBig;
101
102
    /**
103
     * @var string|null
104
     */
105
    private ?string $url;
106
107
    /**
108
     * @var float|null
109
     */
110
    private ?float $latitude;
111
112
    /**
113
     * @var float|null
114
     */
115
    private ?float $longitude;
116
117
    /**
118
     * @var string|null
119
     */
120
    private ?string $directionsGlobal;
121
122
    /**
123
     * @var string|null
124
     */
125
    private ?string $directionsCar;
126
127
    /**
128
     * @var string|null
129
     */
130
    private ?string $directionsPublic;
131
132
    /**
133
     * @var bool|null
134
     */
135
    private ?bool $wheelchairAccessible;
136
137
    /**
138
     * @var bool|null
139
     */
140
    private ?bool $claimAssistant;
141
142
    /**
143
     * @var bool|null
144
     */
145
    private ?bool $dressingRoom;
146
147
    /**
148
     * @var string|null
149
     */
150
    private ?string $openingMonday;
151
152
    /**
153
     * @var string|null
154
     */
155
    private ?string $openingTuesday;
156
157
    /**
158
     * @var string|null
159
     */
160
    private ?string $openingWednesday;
161
162
    /**
163
     * @var string|null
164
     */
165
    private ?string $openingThursday;
166
167
    /**
168
     * @var string|null
169
     */
170
    private ?string $openingFriday;
171
172
    /**
173
     * @var string|null
174
     */
175
    private ?string $openingSaturday;
176
177
    /**
178
     * @var string|null
179
     */
180
    private ?string $openingSunday;
181
182
    /**
183
     * @var float|null
184
     */
185
    private ?float $maxWeight;
186
187
    /**
188
     * Branch constructor
189
     *
190
     * @param string      $shipper
191
     * @param string|null $service
192
     * @param string|null $id
193
     * @param string|null $uid
194
     * @param string      $type
195
     * @param string      $name
196
     * @param string      $city
197
     * @param string      $street
198
     * @param string      $zip
199
     * @param string|null $country
200
     * @param string|null $cityPart
201
     * @param string|null $district
202
     * @param string|null $region
203
     * @param string|null $currency
204
     * @param string|null $photoSmall
205
     * @param string|null $photoBig
206
     * @param string|null $url
207
     * @param float|null  $latitude
208
     * @param float|null  $longitude
209
     * @param string|null $directionsGlobal
210
     * @param string|null $directionsCar
211
     * @param string|null $directionsPublic
212
     * @param bool|null   $wheelchairAccessible
213
     * @param bool|null   $claimAssistant
214
     * @param bool|null   $dressingRoom
215
     * @param string|null $openingMonday
216
     * @param string|null $openingTuesday
217
     * @param string|null $openingWednesday
218
     * @param string|null $openingThursday
219
     * @param string|null $openingFriday
220
     * @param string|null $openingSaturday
221
     * @param string|null $openingSunday
222
     * @param float|null  $maxWeight
223
     */
224 20
    public function __construct(
225
        string $shipper,
226
        ?string $service,
227
        ?string $id,
228
        ?string $uid,
229
        string $type,
230
        string $name,
231
        string $city,
232
        string $street,
233
        string $zip,
234
        ?string $country = null,
235
        ?string $cityPart = null,
236
        ?string $district = null,
237
        ?string $region = null,
238
        ?string $currency = null,
239
        ?string $photoSmall = null,
240
        ?string $photoBig = null,
241
        ?string $url = null,
242
        ?float $latitude = null,
243
        ?float $longitude = null,
244
        ?string $directionsGlobal = null,
245
        ?string $directionsCar = null,
246
        ?string $directionsPublic = null,
247
        ?bool $wheelchairAccessible = null,
248
        ?bool $claimAssistant = null,
249
        ?bool $dressingRoom = null,
250
        ?string $openingMonday = null,
251
        ?string $openingTuesday = null,
252
        ?string $openingWednesday = null,
253
        ?string $openingThursday = null,
254
        ?string $openingFriday = null,
255
        ?string $openingSaturday = null,
256
        ?string $openingSunday = null,
257
        ?float $maxWeight = null
258
    ) {
259 20
        $this->shipper              = $shipper;
260 20
        $this->service              = $service;
261 20
        $this->id                   = $id;
262 20
        $this->uid                  = $uid;
263 20
        $this->type                 = $type;
264 20
        $this->name                 = $name;
265 20
        $this->city                 = $city;
266 20
        $this->street               = $street;
267 20
        $this->zip                  = $zip;
268 20
        $this->cityPart             = $cityPart;
269 20
        $this->district             = $district;
270 20
        $this->region               = $region;
271 20
        $this->country              = $country;
272 20
        $this->currency             = $currency;
273 20
        $this->photoSmall           = $photoSmall;
274 20
        $this->photoBig             = $photoBig;
275 20
        $this->url                  = $url;
276 20
        $this->latitude             = $latitude;
277 20
        $this->longitude            = $longitude;
278 20
        $this->directionsGlobal     = $directionsGlobal;
279 20
        $this->directionsCar        = $directionsCar;
280 20
        $this->directionsPublic     = $directionsPublic;
281 20
        $this->wheelchairAccessible = $wheelchairAccessible;
282 20
        $this->claimAssistant       = $claimAssistant;
283 20
        $this->dressingRoom         = $dressingRoom;
284 20
        $this->openingMonday        = $openingMonday;
285 20
        $this->openingTuesday       = $openingTuesday;
286 20
        $this->openingWednesday     = $openingWednesday;
287 20
        $this->openingThursday      = $openingThursday;
288 20
        $this->openingFriday        = $openingFriday;
289 20
        $this->openingSaturday      = $openingSaturday;
290 20
        $this->openingSunday        = $openingSunday;
291 20
        $this->maxWeight            = $maxWeight;
292 20
        $this->branchId             = $this->resolveBranchId();
293 20
    }
294
295
    /**
296
     * @return string
297
     */
298 3
    public function getShipper(): string
299
    {
300 3
        return $this->shipper;
301
    }
302
303
    /**
304
     * @return string|null
305
     */
306 3
    public function getServiceType(): ?string
307
    {
308 3
        return $this->service;
309
    }
310
311
    /**
312
     * @return string
313
     */
314 2
    public function getBranchId(): string
315
    {
316 2
        return $this->branchId;
317
    }
318
319
    /**
320
     * @return string|null
321
     */
322 9
    public function getId(): ?string
323
    {
324 9
        return $this->id;
325
    }
326
327
    /**
328
     * @return string|null
329
     */
330 1
    public function getUId(): ?string
331
    {
332 1
        return $this->uid;
333
    }
334
335
    /**
336
     * @return string
337
     */
338 4
    public function getType(): string
339
    {
340 4
        return $this->type;
341
    }
342
343
    /**
344
     * @return string
345
     */
346 4
    public function getName(): string
347
    {
348 4
        return $this->name;
349
    }
350
351
    /**
352
     * @return string
353
     */
354 3
    public function getCity(): string
355
    {
356 3
        return $this->city;
357
    }
358
359
    /**
360
     * @return string
361
     */
362 5
    public function getStreet(): string
363
    {
364 5
        return $this->street;
365
    }
366
367
    /**
368
     * @return string
369
     */
370 11
    public function getZip(): string
371
    {
372 11
        return $this->zip;
373
    }
374
375
    /**
376
     * @return string|null
377
     */
378 3
    public function getCityPart(): ?string
379
    {
380 3
        return $this->cityPart;
381
    }
382
383
    /**
384
     * @return string|null
385
     */
386 3
    public function getDistrict(): ?string
387
    {
388 3
        return $this->district;
389
    }
390
391
    /**
392
     * @return string|null
393
     */
394 3
    public function getRegion(): ?string
395
    {
396 3
        return $this->region;
397
    }
398
399
    /**
400
     * @return string|null
401
     */
402 7
    public function getCountry(): ?string
403
    {
404 7
        return $this->country;
405
    }
406
407
    /**
408
     * @return string|null
409
     */
410 3
    public function getCurrency(): ?string
411
    {
412 3
        return $this->currency;
413
    }
414
415
    /**
416
     * @return string|null
417
     */
418 3
    public function getPhotoSmall(): ?string
419
    {
420 3
        return $this->photoSmall;
421
    }
422
423
    /**
424
     * @return string|null
425
     */
426 3
    public function getPhotoBig(): ?string
427
    {
428 3
        return $this->photoBig;
429
    }
430
431
    /**
432
     * @return string|null
433
     */
434 3
    public function getUrl(): ?string
435
    {
436 3
        return $this->url;
437
    }
438
439
    /**
440
     * @return float|null
441
     */
442 3
    public function getLatitude(): ?float
443
    {
444 3
        return $this->latitude;
445
    }
446
447
    /**
448
     * @return float|null
449
     */
450 3
    public function getLongitude(): ?float
451
    {
452 3
        return $this->longitude;
453
    }
454
455
    /**
456
     * @return string|null
457
     */
458 3
    public function getDirectionsGlobal(): ?string
459
    {
460 3
        return $this->directionsGlobal;
461
    }
462
463
    /**
464
     * @return string|null
465
     */
466 3
    public function getDirectionsCar(): ?string
467
    {
468 3
        return $this->directionsCar;
469
    }
470
471
    /**
472
     * @return string|null
473
     */
474 3
    public function getDirectionsPublic(): ?string
475
    {
476 3
        return $this->directionsPublic;
477
    }
478
479
    /**
480
     * @return bool|null
481
     */
482 3
    public function getWheelchairAccessible(): ?bool
483
    {
484 3
        return $this->wheelchairAccessible;
485
    }
486
487
    /**
488
     * @return bool|null
489
     */
490 3
    public function getClaimAssistant(): ?bool
491
    {
492 3
        return $this->claimAssistant;
493
    }
494
495
    /**
496
     * @return bool|null
497
     */
498 3
    public function getDressingRoom(): ?bool
499
    {
500 3
        return $this->dressingRoom;
501
    }
502
503
    /**
504
     * @return string|null
505
     */
506 3
    public function getOpeningMonday(): ?string
507
    {
508 3
        return $this->openingMonday;
509
    }
510
511
    /**
512
     * @return string|null
513
     */
514 3
    public function getOpeningTuesday(): ?string
515
    {
516 3
        return $this->openingTuesday;
517
    }
518
519
    /**
520
     * @return string|null
521
     */
522 3
    public function getOpeningWednesday(): ?string
523
    {
524 3
        return $this->openingWednesday;
525
    }
526
527
    /**
528
     * @return string|null
529
     */
530 3
    public function getOpeningThursday(): ?string
531
    {
532 3
        return $this->openingThursday;
533
    }
534
535
    /**
536
     * @return string|null
537
     */
538 3
    public function getOpeningFriday(): ?string
539
    {
540 3
        return $this->openingFriday;
541
    }
542
543
    /**
544
     * @return string|null
545
     */
546 3
    public function getOpeningSaturday(): ?string
547
    {
548 3
        return $this->openingSaturday;
549
    }
550
551
    /**
552
     * @return string|null
553
     */
554 3
    public function getOpeningSunday(): ?string
555
    {
556 3
        return $this->openingSunday;
557
    }
558
559
    /**
560
     * @return float|null
561
     */
562 3
    public function getMaxWeight(): ?float
563
    {
564 3
        return $this->maxWeight;
565
    }
566
567
    /**
568
     * Resolve branch ID
569
     *
570
     * @return string
571
     */
572 20
    private function resolveBranchId(): string
573
    {
574
        // get key used in branch_id when calling add request
575
        if (
576 20
            $this->shipper === Shipper::CP
577 11
            || $this->shipper === Shipper::SP
578 20
            || ($this->shipper === Shipper::ULOZENKA && $this->service === ServiceType::ULOZENKA_CP_NP)
579
        ) {
580 12
            return str_replace(' ', '', $this->zip);
581
        }
582
583 11
        if ($this->shipper === Shipper::PPL) {
584 7
            return str_replace('KM', '', (string) $this->id);
585
        }
586
587 6
        if ($this->shipper === Shipper::INTIME) {
588 2
            return $this->name;
589
        }
590
591 6
        return (string) $this->id;
592
    }
593
594
    /**
595
     * New instance from data
596
     *
597
     * @param string              $shipper
598
     * @param string|null         $service
599
     * @param array<string,mixed> $data
600
     *
601
     * @return \Inspirum\Balikobot\Model\Values\Branch
602
     */
603 20
    public static function newInstanceFromData(string $shipper, ?string $service, array $data): self
604
    {
605 20
        if ($shipper === Shipper::CP && $service === ServiceType::CP_NP) {
606 10
            $data['country'] ??= 'CZ';
607
        }
608
609 20
        if (isset($data['street']) && (isset($data['house_number']) || isset($data['orientation_number']))) {
610 2
            $houseNumber       = (int) ($data['house_number'] ?? 0);
611 2
            $orientationNumber = (int) ($data['orientation_number'] ?? 0);
612 2
            $streetNumber      = trim(
613 2
                sprintf(
614 2
                    '%s/%s',
615 2
                    $houseNumber > 0 ? $houseNumber : '',
616 2
                    $orientationNumber > 0 ? $orientationNumber : ''
617
                ),
618 2
                '/'
619
            );
620
621 2
            $data['street'] = trim(sprintf('%s %s', $data['street'] ?: ($data['city'] ?? ''), $streetNumber));
622
        }
623
624 20
        return new self(
625 20
            $shipper,
626
            $service,
627 20
            $data['branch_id'] ?? (isset($data['id']) ? (string) $data['id'] : null),
628 20
            $data['branch_uid'] ?? null,
629 20
            $data['type'] ?? 'branch',
630 20
            $data['name'] ?? ($data['zip'] ?? '00000'),
631 20
            $data['city'] ?? '',
632 20
            $data['street'] ?? ($data['address'] ?? ''),
633 20
            $data['zip'] ?? '00000',
634 20
            $data['country'] ?? null,
635 20
            $data['city_part'] ?? null,
636 20
            $data['district'] ?? null,
637 20
            $data['region'] ?? null,
638 20
            $data['currency'] ?? null,
639 20
            $data['photo_small'] ?? null,
640 20
            $data['photo_big'] ?? null,
641 20
            $data['url'] ?? null,
642 20
            (isset($data['latitude']) ? (float) trim((string) $data['latitude']) : null) ?: null,
643 20
            (isset($data['longitude']) ? (float) trim((string) $data['longitude']) : null) ?: null,
644 20
            $data['directions_global'] ?? null,
645 20
            $data['directions_car'] ?? null,
646 20
            $data['directions_public'] ?? null,
647 20
            isset($data['wheelchair_accessible']) ? (bool) $data['wheelchair_accessible'] : null,
648 20
            isset($data['claim_assistant']) ? (bool) $data['claim_assistant'] : null,
649 20
            isset($data['dressing_room']) ? (bool) $data['dressing_room'] : null,
650 20
            $data['opening_monday'] ?? null,
651 20
            $data['opening_tuesday'] ?? null,
652 20
            $data['opening_wednesday'] ?? null,
653 20
            $data['opening_thursday'] ?? null,
654 20
            $data['opening_friday'] ?? null,
655 20
            $data['opening_saturday'] ?? null,
656 20
            $data['opening_sunday'] ?? null,
657 20
            isset($data['max_weight']) ? (float) $data['max_weight'] : null
658
        );
659
    }
660
}
661