Completed
Push — master ( 452d21...8391b8 )
by Tomáš
09:19 queued 07:48
created

Branch::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 1

Importance

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