Completed
Push — master ( 0512a1...7c8694 )
by Tomáš
03:46
created

Branch::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 67
c 0
b 0
f 0
rs 8.72
ccs 34
cts 34
cp 1
cc 1
nc 1
nop 31
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
     * Branch constructor
174
     *
175
     * @param string      $shipper
176
     * @param string|null $service
177
     * @param string|null $id
178
     * @param string      $type
179
     * @param string      $name
180
     * @param string      $city
181
     * @param string      $street
182
     * @param string      $zip
183
     * @param string|null $country
184
     * @param string|null $cityPart
185
     * @param string|null $district
186
     * @param string|null $region
187
     * @param string|null $currency
188
     * @param string|null $photoSmall
189
     * @param string|null $photoBig
190
     * @param string|null $url
191
     * @param float|null  $latitude
192
     * @param float|null  $longitude
193
     * @param string|null $directionsGlobal
194
     * @param string|null $directionsCar
195
     * @param string|null $directionsPublic
196
     * @param bool|null   $wheelchairAccessible
197
     * @param bool|null   $claimAssistant
198
     * @param bool|null   $dressingRoom
199
     * @param string|null $openingMonday
200
     * @param string|null $openingTuesday
201
     * @param string|null $openingWednesday
202
     * @param string|null $openingThursday
203
     * @param string|null $openingFriday
204
     * @param string|null $openingSaturday
205
     * @param string|null $openingSunday
206
     */
207 16
    public function __construct(
208
        string $shipper,
209
        ?string $service,
210
        ?string $id,
211
        string $type,
212
        string $name,
213
        string $city,
214
        string $street,
215
        string $zip,
216
        string $country = null,
217
        string $cityPart = null,
218
        string $district = null,
219
        string $region = null,
220
        string $currency = null,
221
        string $photoSmall = null,
222
        string $photoBig = null,
223
        string $url = null,
224
        float $latitude = null,
225
        float $longitude = null,
226
        string $directionsGlobal = null,
227
        string $directionsCar = null,
228
        string $directionsPublic = null,
229
        bool $wheelchairAccessible = null,
230
        bool $claimAssistant = null,
231
        bool $dressingRoom = null,
232
        string $openingMonday = null,
233
        string $openingTuesday = null,
234
        string $openingWednesday = null,
235
        string $openingThursday = null,
236
        string $openingFriday = null,
237
        string $openingSaturday = null,
238
        string $openingSunday = null
239
    ) {
240 16
        $this->shipper              = $shipper;
241 16
        $this->service              = $service;
242 16
        $this->id                   = $id;
243 16
        $this->type                 = $type;
244 16
        $this->name                 = $name;
245 16
        $this->city                 = $city;
246 16
        $this->street               = $street;
247 16
        $this->zip                  = $zip;
248 16
        $this->cityPart             = $cityPart;
249 16
        $this->district             = $district;
250 16
        $this->region               = $region;
251 16
        $this->country              = $country;
252 16
        $this->currency             = $currency;
253 16
        $this->photoSmall           = $photoSmall;
254 16
        $this->photoBig             = $photoBig;
255 16
        $this->url                  = $url;
256 16
        $this->latitude             = $latitude;
257 16
        $this->longitude            = $longitude;
258 16
        $this->directionsGlobal     = $directionsGlobal;
259 16
        $this->directionsCar        = $directionsCar;
260 16
        $this->directionsPublic     = $directionsPublic;
261 16
        $this->wheelchairAccessible = $wheelchairAccessible;
262 16
        $this->claimAssistant       = $claimAssistant;
263 16
        $this->dressingRoom         = $dressingRoom;
264 16
        $this->openingMonday        = $openingMonday;
265 16
        $this->openingTuesday       = $openingTuesday;
266 16
        $this->openingWednesday     = $openingWednesday;
267 16
        $this->openingThursday      = $openingThursday;
268 16
        $this->openingFriday        = $openingFriday;
269 16
        $this->openingSaturday      = $openingSaturday;
270 16
        $this->openingSunday        = $openingSunday;
271
272 16
        $this->setBranchId();
273 16
    }
274
275
    /**
276
     * @return string
277
     */
278 3
    public function getShipper(): string
279
    {
280 3
        return $this->shipper;
281
    }
282
283
    /**
284
     * @return string|null
285
     */
286 3
    public function getServiceType(): ?string
287
    {
288 3
        return $this->service;
289
    }
290
291
    /**
292
     * @return string
293
     */
294 1
    public function getBranchId(): string
295
    {
296 1
        return $this->branchId;
297
    }
298
299
    /**
300
     * @return string|null
301
     */
302 5
    public function getId(): ?string
303
    {
304 5
        return $this->id;
305
    }
306
307
    /**
308
     * @return string
309
     */
310 4
    public function getType(): string
311
    {
312 4
        return $this->type;
313
    }
314
315
    /**
316
     * @return string
317
     */
318 4
    public function getName(): string
319
    {
320 4
        return $this->name;
321
    }
322
323
    /**
324
     * @return string
325
     */
326 3
    public function getCity(): string
327
    {
328 3
        return $this->city;
329
    }
330
331
    /**
332
     * @return string
333
     */
334 5
    public function getStreet(): string
335
    {
336 5
        return $this->street;
337
    }
338
339
    /**
340
     * @return string
341
     */
342 10
    public function getZip(): string
343
    {
344 10
        return $this->zip;
345
    }
346
347
    /**
348
     * @return string|null
349
     */
350 3
    public function getCityPart(): ?string
351
    {
352 3
        return $this->cityPart;
353
    }
354
355
    /**
356
     * @return string|null
357
     */
358 3
    public function getDistrict(): ?string
359
    {
360 3
        return $this->district;
361
    }
362
363
    /**
364
     * @return string|null
365
     */
366 3
    public function getRegion(): ?string
367
    {
368 3
        return $this->region;
369
    }
370
371
    /**
372
     * @return string|null
373
     */
374 6
    public function getCountry(): ?string
375
    {
376 6
        return $this->country;
377
    }
378
379
    /**
380
     * @return string|null
381
     */
382 3
    public function getCurrency(): ?string
383
    {
384 3
        return $this->currency;
385
    }
386
387
    /**
388
     * @return string|null
389
     */
390 3
    public function getPhotoSmall(): ?string
391
    {
392 3
        return $this->photoSmall;
393
    }
394
395
    /**
396
     * @return string|null
397
     */
398 3
    public function getPhotoBig(): ?string
399
    {
400 3
        return $this->photoBig;
401
    }
402
403
    /**
404
     * @return string|null
405
     */
406 3
    public function getUrl(): ?string
407
    {
408 3
        return $this->url;
409
    }
410
411
    /**
412
     * @return float|null
413
     */
414 3
    public function getLatitude(): ?float
415
    {
416 3
        return $this->latitude;
417
    }
418
419
    /**
420
     * @return float|null
421
     */
422 3
    public function getLongitude(): ?float
423
    {
424 3
        return $this->longitude;
425
    }
426
427
    /**
428
     * @return string|null
429
     */
430 3
    public function getDirectionsGlobal(): ?string
431
    {
432 3
        return $this->directionsGlobal;
433
    }
434
435
    /**
436
     * @return string|null
437
     */
438 3
    public function getDirectionsCar(): ?string
439
    {
440 3
        return $this->directionsCar;
441
    }
442
443
    /**
444
     * @return string|null
445
     */
446 3
    public function getDirectionsPublic(): ?string
447
    {
448 3
        return $this->directionsPublic;
449
    }
450
451
    /**
452
     * @return bool|null
453
     */
454 3
    public function getWheelchairAccessible(): ?bool
455
    {
456 3
        return $this->wheelchairAccessible;
457
    }
458
459
    /**
460
     * @return bool|null
461
     */
462 3
    public function getClaimAssistant(): ?bool
463
    {
464 3
        return $this->claimAssistant;
465
    }
466
467
    /**
468
     * @return bool|null
469
     */
470 3
    public function getDressingRoom(): ?bool
471
    {
472 3
        return $this->dressingRoom;
473
    }
474
475
    /**
476
     * @return string|null
477
     */
478 3
    public function getOpeningMonday(): ?string
479
    {
480 3
        return $this->openingMonday;
481
    }
482
483
    /**
484
     * @return string|null
485
     */
486 3
    public function getOpeningTuesday(): ?string
487
    {
488 3
        return $this->openingTuesday;
489
    }
490
491
    /**
492
     * @return string|null
493
     */
494 3
    public function getOpeningWednesday(): ?string
495
    {
496 3
        return $this->openingWednesday;
497
    }
498
499
    /**
500
     * @return string|null
501
     */
502 3
    public function getOpeningThursday(): ?string
503
    {
504 3
        return $this->openingThursday;
505
    }
506
507
    /**
508
     * @return string|null
509
     */
510 3
    public function getOpeningFriday(): ?string
511
    {
512 3
        return $this->openingFriday;
513
    }
514
515
    /**
516
     * @return string|null
517
     */
518 3
    public function getOpeningSaturday(): ?string
519
    {
520 3
        return $this->openingSaturday;
521
    }
522
523
    /**
524
     * @return string|null
525
     */
526 3
    public function getOpeningSunday(): ?string
527
    {
528 3
        return $this->openingSunday;
529
    }
530
531
    /**
532
     * Set branch ID
533
     *
534
     * @return void
535
     */
536 16
    private function setBranchId(): void
537
    {
538 16
        $this->branchId = $this->resolveBranchId();
539 16
    }
540
541
    /**
542
     * Resolve branch ID
543
     *
544
     * @return string
545
     */
546 16
    private function resolveBranchId(): string
547
    {
548
        // get key used in branch_id when calling add request
549
        if (
550 16
            $this->shipper === Shipper::CP
551 8
            || $this->shipper === Shipper::SP
552 16
            || ($this->shipper === Shipper::ULOZENKA && $this->service === ServiceType::ULOZENKA_CP_NP)
553
        ) {
554 11
            return str_replace(' ', '', $this->zip);
555
        }
556
557 8
        if ($this->shipper === Shipper::PPL) {
558 6
            return str_replace('KM', '', (string) $this->id);
559
        }
560
561 4
        if ($this->shipper === Shipper::INTIME) {
562 2
            return $this->name;
563
        }
564
565 4
        return (string) $this->id;
566
    }
567
568
    /**
569
     * New instance from data
570
     *
571
     * @param string              $shipper
572
     * @param string|null         $service
573
     * @param array<string,mixed> $data
574
     *
575
     * @return \Inspirum\Balikobot\Model\Values\Branch
576
     */
577 16
    public static function newInstanceFromData(string $shipper, ?string $service, array $data): self
578
    {
579 16
        if ($shipper === Shipper::CP && $service === ServiceType::CP_NP) {
580 10
            $data['country'] = $data['country'] ?? 'CZ';
581
        }
582
583 16
        if (isset($data['street']) && (isset($data['house_number']) || isset($data['orientation_number']))) {
584 2
            $houseNumber       = (int) ($data['house_number'] ?? 0);
585 2
            $orientationNumber = (int) ($data['orientation_number'] ?? 0);
586 2
            $streetNumber      = trim(
587
                sprintf(
588 2
                    '%s/%s',
589 2
                    $houseNumber > 0 ? $houseNumber : '',
590 2
                    $orientationNumber > 0 ? $orientationNumber : ''
591
                ),
592 2
                '/'
593
            );
594
595 2
            $data['street'] = trim(sprintf('%s %s', $data['street'] ?: ($data['city'] ?? ''), $streetNumber));
596
        }
597
598 16
        return new self(
599 16
            $shipper,
600
            $service,
601 16
            $data['id'] ?? null,
602 16
            $data['type'] ?? 'branch',
603 16
            $data['name'] ?? ($data['zip'] ?? '00000'),
604 16
            $data['city'] ?? '',
605 16
            $data['street'] ?? ($data['address'] ?? ''),
606 16
            $data['zip'] ?? '00000',
607 16
            $data['country'] ?? null,
608 16
            $data['city_part'] ?? null,
609 16
            $data['district'] ?? null,
610 16
            $data['region'] ?? null,
611 16
            $data['currency'] ?? null,
612 16
            $data['photo_small'] ?? null,
613 16
            $data['photo_big'] ?? null,
614 16
            $data['url'] ?? null,
615 16
            (isset($data['latitude']) ? (float) trim($data['latitude']) : null) ?: null,
616 16
            (isset($data['longitude']) ? (float) trim($data['longitude']) : null) ?: null,
617 16
            $data['directions_global'] ?? null,
618 16
            $data['directions_car'] ?? null,
619 16
            $data['directions_public'] ?? null,
620 16
            $data['wheelchair_accessible'] ?? null,
621 16
            $data['claim_assistant'] ?? null,
622 16
            $data['dressing_room'] ?? null,
623 16
            $data['opening_monday'] ?? null,
624 16
            $data['opening_tuesday'] ?? null,
625 16
            $data['opening_wednesday'] ?? null,
626 16
            $data['opening_thursday'] ?? null,
627 16
            $data['opening_friday'] ?? null,
628 16
            $data['opening_saturday'] ?? null,
629 16
            $data['opening_sunday'] ?? null
630
        );
631
    }
632
}
633