ShipmentRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
eloc 13
c 5
b 0
f 2
dl 0
loc 29
rs 9.8333
cc 1
nc 1
nop 13

How to fix   Many Parameters   

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 DPD\Interconnector\Request;
4
5
use DPD\Interconnector\Authentication;
6
7
8
class ShipmentRequest extends Request implements RequestInterface
9
{
10
    private $name;
11
    private $street;
12
    private $city;
13
    private $country;
14
    private $postalCode;
15
    private $numberOfParcels;
16
    private $phone;
17
    private $orderNumber;
18
    private $remark;
19
    private $codAmount;
20
    private $parcelShopId;
21
    private $name2;
22
23
    public function __construct(
24
        Authentication $auth,
25
        string $name,
26
        string $street, 
27
        string $city,
28
        string $country,
29
        string $postalCode,
30
        int $numberOfParcels,
31
        string $phone,
32
        string $orderNumber,
33
        string $remark,
34
        float $codAmount = 0.0,
35
        ?string $parcelShopId = null,
36
        string $name2 = null
37
    ) {
38
        parent::__construct($auth);
39
40
        $this->name = $name;
41
        $this->name2 = $name2;
42
        $this->street = $street;
43
        $this->city = $city;
44
        $this->country = $country;
45
        $this->postalCode = $postalCode;
46
        $this->numberOfParcels = $numberOfParcels;
47
        $this->phone = $phone;
48
        $this->orderNumber = $orderNumber;
49
        $this->remark = $remark;
50
        $this->codAmount = $codAmount;
51
        $this->parcelShopId = $parcelShopId;
52
    }
53
54
    public function toArray(): array
55
    {
56
        $request = array_merge(
57
            $this->authentication->toArray(),
58
            [
59
                'name1' => $this->name,
60
                'name2' => $this->name2,
61
                'street' => $this->street,
62
                'city' => $this->city,
63
                'country' => $this->country,
64
                'pcode' => $this->postalCode,
65
                'num_of_parcel' => $this->numberOfParcels,
66
                'phone' => $this->phone,
67
                'idm_sms_number' => $this->phone,
68
                'order_number' => $this->orderNumber,
69
                'remark' => $this->remark
70
            ]
71
        );
72
73
        $parcelType = 'D-B2C';
74
        if ($this->parcelShopId) {
75
            $request['parcelshop_id'] = $this->parcelShopId;
76
            $parcelType = 'PS';
77
        }
78
79
        if ($this->codAmount) {
80
            $parcelType .= '-COD';
81
            $request['cod_amount'] = $this->codAmount;
82
        }
83
84
        $request['parcel_type'] = $parcelType;
85
86
        return $request;
87
    }
88
}