Completed
Pull Request — master (#2)
by Paulius
02:11 queued 41s
created

ShipmentRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 11
c 2
b 0
f 2
nc 1
nop 11
dl 0
loc 24
rs 9.9

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