AccountInfo::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 16
c 1
b 0
f 1
nc 1
nop 16
dl 0
loc 34
ccs 17
cts 17
cp 1
crap 1
rs 9.7333

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 AccountInfo implements DtoInterface
6
{
7
    public string $username;
8
    public string $forumName;
9
    public string $avatarUrl;
10
    public AccountContact $accountContact;
11
    public int $customerSince;
12
    public string $accountLock;
13
    public string $customTimeZone;
14
    public int $defaultRegistrantContactId;
15
    public int $defaultAdminContactId;
16
    public int $defaultTechnicalContactId;
17
    public int $defaultBillingContactId;
18
    public DefaultNameServerSettings $defaultNameServerSettings;
19
    public string $totalSpending;
20
    public string $priceLevel;
21
    public string $accountBalance;
22
    /** @var array<BalanceListItem> */
23
    public array $balanceList;
24
25
    /**
26
     * @param array<BalanceListItem> $balanceList
27
     */
28 1
    public function __construct(
29
        string $username = '',
30
        string $forumName = '',
31
        string $avatarUrl = '',
32
        ?AccountContact $accountContact = null,
33
        int $customerSince = 0,
34
        string $accountLock = '',
35
        string $customTimeZone = '',
36
        int $defaultRegistrantContactId = 0,
37
        int $defaultAdminContactId = 0,
38
        int $defaultTechnicalContactId = 0,
39
        int $defaultBillingContactId = 0,
40
        ?DefaultNameServerSettings $defaultNameServerSettings = null,
41
        string $totalSpending = '',
42
        string $priceLevel = '',
43
        string $accountBalance = '',
44
        array $balanceList = []
45
    ) {
46 1
        $this->username                   = $username;
47 1
        $this->forumName                  = $forumName;
48 1
        $this->avatarUrl                  = $avatarUrl;
49 1
        $this->accountContact             = $accountContact ?? new AccountContact();
50 1
        $this->customerSince              = $customerSince;
51 1
        $this->accountLock                = $accountLock;
52 1
        $this->customTimeZone             = $customTimeZone;
53 1
        $this->defaultRegistrantContactId = $defaultRegistrantContactId;
54 1
        $this->defaultAdminContactId      = $defaultAdminContactId;
55 1
        $this->defaultTechnicalContactId  = $defaultTechnicalContactId;
56 1
        $this->defaultBillingContactId    = $defaultBillingContactId;
57 1
        $this->defaultNameServerSettings  = $defaultNameServerSettings ?? new DefaultNameServerSettings();
58 1
        $this->totalSpending              = $totalSpending;
59 1
        $this->priceLevel                 = $priceLevel;
60 1
        $this->accountBalance             = $accountBalance;
61 1
        $this->balanceList                = $balanceList;
62
    }
63
64 1
    public static function fromArray(array $data): self
65
    {
66 1
        $balanceList = [];
67 1
        if (isset($data['balance_list']) && is_array($data['balance_list'])) {
68 1
            foreach ($data['balance_list'] as $item) {
69 1
                $balanceList[] = BalanceListItem::fromArray($item);
70
            }
71
        }
72
73 1
        return new self(
74 1
            $data['username'] ?? '',
75 1
            $data['forum_name'] ?? '',
76 1
            $data['avatar_url'] ?? '',
77 1
            isset($data['account_contact']) ? AccountContact::fromArray($data['account_contact']) : new AccountContact(),
78 1
            $data['customer_since'] ?? 0,
79 1
            $data['account_lock'] ?? '',
80 1
            $data['custom_time_zone'] ?? '',
81 1
            $data['default_registrant_contact_id'] ?? 0,
82 1
            $data['default_admin_contact_id'] ?? 0,
83 1
            $data['default_technical_contact_id'] ?? 0,
84 1
            $data['default_billing_contact_id'] ?? 0,
85 1
            isset($data['default_name_server_settings']) ? DefaultNameServerSettings::fromArray($data['default_name_server_settings']) : new DefaultNameServerSettings(),
86 1
            $data['total_spending'] ?? '',
87 1
            $data['price_level'] ?? '',
88 1
            $data['account_balance'] ?? '',
89 1
            $balanceList
90 1
        );
91
    }
92
93
    public static function empty(): self
94
    {
95
        return new self();
96
    }
97
98
    /**
99
     * @return array<string, mixed>
100
     */
101
    public function jsonSerialize(): array
102
    {
103
        return [
104
            'username'                      => $this->username,
105
            'forum_name'                    => $this->forumName,
106
            'avatar_url'                    => $this->avatarUrl,
107
            'account_contact'               => $this->accountContact->jsonSerialize(),
108
            'customer_since'                => $this->customerSince,
109
            'account_lock'                  => $this->accountLock,
110
            'custom_time_zone'              => $this->customTimeZone,
111
            'default_registrant_contact_id' => $this->defaultRegistrantContactId,
112
            'default_admin_contact_id'      => $this->defaultAdminContactId,
113
            'default_technical_contact_id'  => $this->defaultTechnicalContactId,
114
            'default_billing_contact_id'    => $this->defaultBillingContactId,
115
            'default_name_server_settings'  => $this->defaultNameServerSettings->jsonSerialize(),
116
            'total_spending'                => $this->totalSpending,
117
            'price_level'                   => $this->priceLevel,
118
            'account_balance'               => $this->accountBalance,
119
            'balance_list'                  => array_map(fn ($item) => $item->jsonSerialize(), $this->balanceList),
120
        ];
121
    }
122
}
123