Completed
Push — master ( 0ead96...92ce45 )
by Tomáš
01:59
created

Branch::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 8.6545
c 0
b 0
f 0
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 19
        $this->branchId             = $this->resolveBranchId();
288 19
    }
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 10
    public function getZip(): string
366
    {
367 10
        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 6
    public function getCountry(): ?string
398
    {
399 6
        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 19
    private function resolveBranchId(): string
568
    {
569
        // get key used in branch_id when calling add request
570
        if (
571 19
            $this->shipper === Shipper::CP
572 10
            || $this->shipper === Shipper::SP
573 19
            || ($this->shipper === Shipper::ULOZENKA && $this->service === ServiceType::ULOZENKA_CP_NP)
574
        ) {
575 12
            return str_replace(' ', '', $this->zip);
576
        }
577
578 10
        if ($this->shipper === Shipper::PPL) {
579 7
            return str_replace('KM', '', (string) $this->id);
580
        }
581
582 5
        if ($this->shipper === Shipper::INTIME) {
583 2
            return $this->name;
584
        }
585
586 5
        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 19
    public static function newInstanceFromData(string $shipper, ?string $service, array $data): self
599
    {
600 19
        if ($shipper === Shipper::CP && $service === ServiceType::CP_NP) {
601 10
            $data['country'] = $data['country'] ?? 'CZ';
602
        }
603
604 19
        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 19
        return new self(
620 19
            $shipper,
621
            $service,
622 19
            $data['branch_id'] ?? ($data['id'] ?? null),
623 19
            $data['branch_uid'] ?? null,
624 19
            $data['type'] ?? 'branch',
625 19
            $data['name'] ?? ($data['zip'] ?? '00000'),
626 19
            $data['city'] ?? '',
627 19
            $data['street'] ?? ($data['address'] ?? ''),
628 19
            $data['zip'] ?? '00000',
629 19
            $data['country'] ?? null,
630 19
            $data['city_part'] ?? null,
631 19
            $data['district'] ?? null,
632 19
            $data['region'] ?? null,
633 19
            $data['currency'] ?? null,
634 19
            $data['photo_small'] ?? null,
635 19
            $data['photo_big'] ?? null,
636 19
            $data['url'] ?? null,
637 19
            (isset($data['latitude']) ? (float) trim($data['latitude']) : null) ?: null,
638 19
            (isset($data['longitude']) ? (float) trim($data['longitude']) : null) ?: null,
639 19
            $data['directions_global'] ?? null,
640 19
            $data['directions_car'] ?? null,
641 19
            $data['directions_public'] ?? null,
642 19
            $data['wheelchair_accessible'] ?? null,
643 19
            $data['claim_assistant'] ?? null,
644 19
            $data['dressing_room'] ?? null,
645 19
            $data['opening_monday'] ?? null,
646 19
            $data['opening_tuesday'] ?? null,
647 19
            $data['opening_wednesday'] ?? null,
648 19
            $data['opening_thursday'] ?? null,
649 19
            $data['opening_friday'] ?? null,
650 19
            $data['opening_saturday'] ?? null,
651 19
            $data['opening_sunday'] ?? null,
652 19
            $data['max_weight'] ?? null
653
        );
654
    }
655
}
656