Completed
Pull Request — master (#8)
by Tomáš
02:25
created

Branch::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 71
c 0
b 0
f 0
rs 8.6327
ccs 36
cts 36
cp 1
cc 1
nc 1
nop 33
crap 1

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 19
    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 19
        $this->shipper              = $shipper;
255 19
        $this->service              = $service;
256 19
        $this->id                   = $id;
257 19
        $this->uid                   = $uid;
258 19
        $this->type                 = $type;
259 19
        $this->name                 = $name;
260 19
        $this->city                 = $city;
261 19
        $this->street               = $street;
262 19
        $this->zip                  = $zip;
263 19
        $this->cityPart             = $cityPart;
264 19
        $this->district             = $district;
265 19
        $this->region               = $region;
266 19
        $this->country              = $country;
267 19
        $this->currency             = $currency;
268 19
        $this->photoSmall           = $photoSmall;
269 19
        $this->photoBig             = $photoBig;
270 19
        $this->url                  = $url;
271 19
        $this->latitude             = $latitude;
272 19
        $this->longitude            = $longitude;
273 19
        $this->directionsGlobal     = $directionsGlobal;
274 19
        $this->directionsCar        = $directionsCar;
275 19
        $this->directionsPublic     = $directionsPublic;
276 19
        $this->wheelchairAccessible = $wheelchairAccessible;
277 19
        $this->claimAssistant       = $claimAssistant;
278 19
        $this->dressingRoom         = $dressingRoom;
279 19
        $this->openingMonday        = $openingMonday;
280 19
        $this->openingTuesday       = $openingTuesday;
281 19
        $this->openingWednesday     = $openingWednesday;
282 19
        $this->openingThursday      = $openingThursday;
283 19
        $this->openingFriday        = $openingFriday;
284 19
        $this->openingSaturday      = $openingSaturday;
285 19
        $this->openingSunday        = $openingSunday;
286 19
        $this->maxWeight            = $maxWeight;
287
288 19
        $this->setBranchId();
289 19
    }
290
291
    /**
292
     * @return string
293
     */
294 3
    public function getShipper(): string
295
    {
296 3
        return $this->shipper;
297
    }
298
299
    /**
300
     * @return string|null
301
     */
302 3
    public function getServiceType(): ?string
303
    {
304 3
        return $this->service;
305
    }
306
307
    /**
308
     * @return string
309
     */
310 2
    public function getBranchId(): string
311
    {
312 2
        return $this->branchId;
313
    }
314
315
    /**
316
     * @return string|null
317
     */
318 9
    public function getId(): ?string
319
    {
320 9
        return $this->id;
321
    }
322
323
    /**
324
     * @return string|null
325
     */
326 1
    public function getUId(): ?string
327
    {
328 1
        return $this->uid;
329
    }
330
331
    /**
332
     * @return string
333
     */
334 4
    public function getType(): string
335
    {
336 4
        return $this->type;
337
    }
338
339
    /**
340
     * @return string
341
     */
342 4
    public function getName(): string
343
    {
344 4
        return $this->name;
345
    }
346
347
    /**
348
     * @return string
349
     */
350 3
    public function getCity(): string
351
    {
352 3
        return $this->city;
353
    }
354
355
    /**
356
     * @return string
357
     */
358 5
    public function getStreet(): string
359
    {
360 5
        return $this->street;
361
    }
362
363
    /**
364
     * @return string
365
     */
366 10
    public function getZip(): string
367
    {
368 10
        return $this->zip;
369
    }
370
371
    /**
372
     * @return string|null
373
     */
374 3
    public function getCityPart(): ?string
375
    {
376 3
        return $this->cityPart;
377
    }
378
379
    /**
380
     * @return string|null
381
     */
382 3
    public function getDistrict(): ?string
383
    {
384 3
        return $this->district;
385
    }
386
387
    /**
388
     * @return string|null
389
     */
390 3
    public function getRegion(): ?string
391
    {
392 3
        return $this->region;
393
    }
394
395
    /**
396
     * @return string|null
397
     */
398 6
    public function getCountry(): ?string
399
    {
400 6
        return $this->country;
401
    }
402
403
    /**
404
     * @return string|null
405
     */
406 3
    public function getCurrency(): ?string
407
    {
408 3
        return $this->currency;
409
    }
410
411
    /**
412
     * @return string|null
413
     */
414 3
    public function getPhotoSmall(): ?string
415
    {
416 3
        return $this->photoSmall;
417
    }
418
419
    /**
420
     * @return string|null
421
     */
422 3
    public function getPhotoBig(): ?string
423
    {
424 3
        return $this->photoBig;
425
    }
426
427
    /**
428
     * @return string|null
429
     */
430 3
    public function getUrl(): ?string
431
    {
432 3
        return $this->url;
433
    }
434
435
    /**
436
     * @return float|null
437
     */
438 3
    public function getLatitude(): ?float
439
    {
440 3
        return $this->latitude;
441
    }
442
443
    /**
444
     * @return float|null
445
     */
446 3
    public function getLongitude(): ?float
447
    {
448 3
        return $this->longitude;
449
    }
450
451
    /**
452
     * @return string|null
453
     */
454 3
    public function getDirectionsGlobal(): ?string
455
    {
456 3
        return $this->directionsGlobal;
457
    }
458
459
    /**
460
     * @return string|null
461
     */
462 3
    public function getDirectionsCar(): ?string
463
    {
464 3
        return $this->directionsCar;
465
    }
466
467
    /**
468
     * @return string|null
469
     */
470 3
    public function getDirectionsPublic(): ?string
471
    {
472 3
        return $this->directionsPublic;
473
    }
474
475
    /**
476
     * @return bool|null
477
     */
478 3
    public function getWheelchairAccessible(): ?bool
479
    {
480 3
        return $this->wheelchairAccessible;
481
    }
482
483
    /**
484
     * @return bool|null
485
     */
486 3
    public function getClaimAssistant(): ?bool
487
    {
488 3
        return $this->claimAssistant;
489
    }
490
491
    /**
492
     * @return bool|null
493
     */
494 3
    public function getDressingRoom(): ?bool
495
    {
496 3
        return $this->dressingRoom;
497
    }
498
499
    /**
500
     * @return string|null
501
     */
502 3
    public function getOpeningMonday(): ?string
503
    {
504 3
        return $this->openingMonday;
505
    }
506
507
    /**
508
     * @return string|null
509
     */
510 3
    public function getOpeningTuesday(): ?string
511
    {
512 3
        return $this->openingTuesday;
513
    }
514
515
    /**
516
     * @return string|null
517
     */
518 3
    public function getOpeningWednesday(): ?string
519
    {
520 3
        return $this->openingWednesday;
521
    }
522
523
    /**
524
     * @return string|null
525
     */
526 3
    public function getOpeningThursday(): ?string
527
    {
528 3
        return $this->openingThursday;
529
    }
530
531
    /**
532
     * @return string|null
533
     */
534 3
    public function getOpeningFriday(): ?string
535
    {
536 3
        return $this->openingFriday;
537
    }
538
539
    /**
540
     * @return string|null
541
     */
542 3
    public function getOpeningSaturday(): ?string
543
    {
544 3
        return $this->openingSaturday;
545
    }
546
547
    /**
548
     * @return string|null
549
     */
550 3
    public function getOpeningSunday(): ?string
551
    {
552 3
        return $this->openingSunday;
553
    }
554
555
    /**
556
     * @return float|null
557
     */
558 3
    public function getMaxWeight(): ?float
559
    {
560 3
        return $this->maxWeight;
561
    }
562
563
    /**
564
     * Set branch ID
565
     *
566
     * @return void
567
     */
568 19
    private function setBranchId(): void
569
    {
570 19
        $this->branchId = $this->resolveBranchId();
571 19
    }
572
573
    /**
574
     * Resolve branch ID
575
     *
576
     * @return string
577
     */
578 19
    private function resolveBranchId(): string
579
    {
580
        // get key used in branch_id when calling add request
581
        if (
582 19
            $this->shipper === Shipper::CP
583 10
            || $this->shipper === Shipper::SP
584 19
            || ($this->shipper === Shipper::ULOZENKA && $this->service === ServiceType::ULOZENKA_CP_NP)
585
        ) {
586 12
            return str_replace(' ', '', $this->zip);
587
        }
588
589 10
        if ($this->shipper === Shipper::PPL) {
590 7
            return str_replace('KM', '', (string) $this->id);
591
        }
592
593 5
        if ($this->shipper === Shipper::INTIME) {
594 2
            return $this->name;
595
        }
596
597 5
        return (string) $this->id;
598
    }
599
600
    /**
601
     * New instance from data
602
     *
603
     * @param string              $shipper
604
     * @param string|null         $service
605
     * @param array<string,mixed> $data
606
     *
607
     * @return \Inspirum\Balikobot\Model\Values\Branch
608
     */
609 19
    public static function newInstanceFromData(string $shipper, ?string $service, array $data): self
610
    {
611 19
        if ($shipper === Shipper::CP && $service === ServiceType::CP_NP) {
612 10
            $data['country'] = $data['country'] ?? 'CZ';
613
        }
614
615 19
        if (isset($data['street']) && (isset($data['house_number']) || isset($data['orientation_number']))) {
616 2
            $houseNumber       = (int) ($data['house_number'] ?? 0);
617 2
            $orientationNumber = (int) ($data['orientation_number'] ?? 0);
618 2
            $streetNumber      = trim(
619 2
                sprintf(
620 2
                    '%s/%s',
621 2
                    $houseNumber > 0 ? $houseNumber : '',
622 2
                    $orientationNumber > 0 ? $orientationNumber : ''
623
                ),
624 2
                '/'
625
            );
626
627 2
            $data['street'] = trim(sprintf('%s %s', $data['street'] ?: ($data['city'] ?? ''), $streetNumber));
628
        }
629
630 19
        return new self(
631 19
            $shipper,
632
            $service,
633 19
            $data['branch_id'] ?? ($data['id'] ?? null),
634 19
            $data['branch_uid'] ?? null,
635 19
            $data['type'] ?? 'branch',
636 19
            $data['name'] ?? ($data['zip'] ?? '00000'),
637 19
            $data['city'] ?? '',
638 19
            $data['street'] ?? ($data['address'] ?? ''),
639 19
            $data['zip'] ?? '00000',
640 19
            $data['country'] ?? null,
641 19
            $data['city_part'] ?? null,
642 19
            $data['district'] ?? null,
643 19
            $data['region'] ?? null,
644 19
            $data['currency'] ?? null,
645 19
            $data['photo_small'] ?? null,
646 19
            $data['photo_big'] ?? null,
647 19
            $data['url'] ?? null,
648 19
            (isset($data['latitude']) ? (float) trim($data['latitude']) : null) ?: null,
649 19
            (isset($data['longitude']) ? (float) trim($data['longitude']) : null) ?: null,
650 19
            $data['directions_global'] ?? null,
651 19
            $data['directions_car'] ?? null,
652 19
            $data['directions_public'] ?? null,
653 19
            $data['wheelchair_accessible'] ?? null,
654 19
            $data['claim_assistant'] ?? null,
655 19
            $data['dressing_room'] ?? null,
656 19
            $data['opening_monday'] ?? null,
657 19
            $data['opening_tuesday'] ?? null,
658 19
            $data['opening_wednesday'] ?? null,
659 19
            $data['opening_thursday'] ?? null,
660 19
            $data['opening_friday'] ?? null,
661 19
            $data['opening_saturday'] ?? null,
662 19
            $data['opening_sunday'] ?? null,
663 19
            $data['max_weight'] ?? null
664
        );
665
    }
666
}
667