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