Test Failed
Pull Request — master (#10)
by
unknown
14:44
created

Contact::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 14
dl 0
loc 31
ccs 0
cts 16
cp 0
crap 2
rs 9.7666

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 Level23\Dynadot\Dto;
4
5
final class Contact implements DtoInterface
6
{
7
    public ?int $contactId;
8
    public string $organization;
9
    public string $name;
10
    public string $email;
11
    public string $phoneNumber;
12
    public string $phoneCc;
13
    public string $faxNumber;
14
    public string $faxCc;
15
    public string $address1;
16
    public string $address2;
17
    public string $city;
18
    public string $state;
19
    public string $zip;
20
    public string $country;
21
22
    private function __construct(
23
        ?int $contactId,
24
        string $organization,
25
        string $name,
26
        string $email,
27
        string $phoneNumber,
28
        string $phoneCc,
29
        string $faxNumber,
30
        string $faxCc,
31
        string $address1,
32
        string $address2,
33
        string $city,
34
        string $state,
35
        string $zip,
36
        string $country,
37
    ) {
38
        $this->contactId = $contactId;
39
        $this->organization = $organization;
40
        $this->name = $name;
41
        $this->email = $email;
42
        $this->phoneNumber = $phoneNumber;
43
        $this->phoneCc = $phoneCc;
44
        $this->faxNumber = $faxNumber;
45
        $this->faxCc = $faxCc;
46
        $this->address1 = $address1;
47
        $this->address2 = $address2;
48
        $this->city = $city;
49
        $this->state = $state;
50
        $this->zip = $zip;
51
        $this->country = $country;
52
    }
53
54
    /**
55
     * Create a new Contact instance.
56
     *
57
     * @param int|null $contactId
58
     * @param string $organization
59
     * @param string $name
60
     * @param string $email
61
     * @param string $phoneNumber
62
     * @param string $phoneCc
63
     * @param string $address1
64
     * @param string $city
65
     * @param string $state
66
     * @param string $zip
67
     * @param string $country
68
     * @param string $address2
69
     * @param string $faxNumber
70
     * @param string $faxCc
71
     * @param int|null $contactId
72
     * @return self
73
     */
74
    public static function create(
75
        string $organization,
76
        string $name,
77
        string $email,
78
        string $phoneNumber,
79
        string $phoneCc,
80
        string $address1,
81
        string $city,
82
        string $state,
83
        string $zip,
84
        string $country,
85
        string $address2 = '',
86
        string $faxNumber = '',
87
        string $faxCc = '',
88
        ?int $contactId = null
89
    ): self {
90
        return new self(
91
            $contactId,
92
            $organization,
93
            $name,
94
            $email,
95
            $phoneNumber,
96
            $phoneCc,
97
            $faxNumber,
98
            $faxCc,
99
            $address1,
100
            $address2,
101
            $city,
102
            $state,
103
            $zip,
104
            $country,
105
        );
106
    }
107
108
    /**
109
     * Hydrate from Dynadot's response data.
110
     *
111
     * @param array<string,mixed> $data
112
     * @return self
113
     */
114
    public static function fromArray(array $data): self
115
    {
116
        return new self(
117
            $data['contact_id'] ?? null,
118
            $data['organization'] ?? '',
119
            $data['name'] ?? '',
120
            $data['email'] ?? '',
121
            $data['phone_number'] ?? '',
122
            $data['phone_cc'] ?? '',
123
            $data['fax_number'] ?? '',
124
            $data['fax_cc'] ?? '',
125
            $data['address1'] ?? '',
126
            $data['address2'] ?? '',
127
            $data['city'] ?? '',
128
            $data['state'] ?? '',
129
            $data['zip'] ?? '',
130
            $data['country'] ?? '',
131
        );
132
    }
133
134
    /**
135
     * @return array<string, mixed>
136
     */
137
    public function jsonSerialize(): array
138
    {
139
        $data = [
140
            'organization' => $this->organization,
141
            'name' => $this->name,
142
            'email' => $this->email,
143
            'phone_number' => $this->phoneNumber,
144
            'phone_cc' => $this->phoneCc,
145
            'fax_number' => $this->faxNumber,
146
            'fax_cc' => $this->faxCc,
147
            'address1' => $this->address1,
148
            'address2' => $this->address2,
149
            'city' => $this->city,
150
            'state' => $this->state,
151
            'zip' => $this->zip,
152
            'country' => $this->country,
153
        ];
154
155
        // Only include contact_id if it's not null
156
        if ($this->contactId !== null) {
157
            $data['contact_id'] = $this->contactId;
158
        }
159
160
        return $data;
161
    }
162
}