Passed
Push — master ( 44ac55...cf05d8 )
by Teye
02:01 queued 35s
created

Contact::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

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

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 17
    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 17
        $this->contactId    = $contactId;
39 17
        $this->organization = $organization;
40 17
        $this->name         = $name;
41 17
        $this->email        = $email;
42 17
        $this->phoneNumber  = $phoneNumber;
43 17
        $this->phoneCc      = $phoneCc;
44 17
        $this->faxNumber    = $faxNumber;
45 17
        $this->faxCc        = $faxCc;
46 17
        $this->address1     = $address1;
47 17
        $this->address2     = $address2;
48 17
        $this->city         = $city;
49 17
        $this->state        = $state;
50 17
        $this->zip          = $zip;
51 17
        $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 7
    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 7
        return new self(
91 7
            $contactId,
92 7
            $organization,
93 7
            $name,
94 7
            $email,
95 7
            $phoneNumber,
96 7
            $phoneCc,
97 7
            $faxNumber,
98 7
            $faxCc,
99 7
            $address1,
100 7
            $address2,
101 7
            $city,
102 7
            $state,
103 7
            $zip,
104 7
            $country,
105 7
        );
106
    }
107
108
    /**
109
     * Hydrate from Dynadot's response data.
110
     *
111
     * @param array<string,mixed> $data
112
     * @return self
113
     */
114 10
    public static function fromArray(array $data): self
115
    {
116 10
        return new self(
117 10
            $data['contact_id'] ?? null,
118 10
            $data['organization'] ?? '',
119 10
            $data['name'] ?? '',
120 10
            $data['email'] ?? '',
121 10
            $data['phone_number'] ?? '',
122 10
            $data['phone_cc'] ?? '',
123 10
            $data['fax_number'] ?? '',
124 10
            $data['fax_cc'] ?? '',
125 10
            $data['address1'] ?? '',
126 10
            $data['address2'] ?? '',
127 10
            $data['city'] ?? '',
128 10
            $data['state'] ?? '',
129 10
            $data['zip'] ?? '',
130 10
            $data['country'] ?? '',
131 10
        );
132
    }
133
134
    /**
135
     * @return array<string, mixed>
136
     */
137 8
    public function jsonSerialize(): array
138
    {
139 8
        $data = [
140 8
            'organization' => $this->organization,
141 8
            'name'         => $this->name,
142 8
            'email'        => $this->email,
143 8
            'phone_number' => $this->phoneNumber,
144 8
            'phone_cc'     => $this->phoneCc,
145 8
            'fax_number'   => $this->faxNumber,
146 8
            'fax_cc'       => $this->faxCc,
147 8
            'address1'     => $this->address1,
148 8
            'address2'     => $this->address2,
149 8
            'city'         => $this->city,
150 8
            'state'        => $this->state,
151 8
            'zip'          => $this->zip,
152 8
            'country'      => $this->country,
153 8
        ];
154
155
        // Only include contact_id if it's not null
156 8
        if ($this->contactId !== null) {
157 1
            $data['contact_id'] = $this->contactId;
158
        }
159
160 8
        return $data;
161
    }
162
}
163