InvoiceTransfer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 19
dl 0
loc 21
rs 10

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
declare(strict_types=1);
4
5
namespace PhpLightning\Shared\Transfer;
6
7
use function array_key_exists;
8
use function is_array;
9
10
final class InvoiceTransfer
11
{
12
    public function __construct(
13
        public string $checkingId = '',
14
        public string $paymentHash = '',
15
        public string $walletId = '',
16
        public int $amount = 0,
17
        public int $fee = 0,
18
        public string $bolt11 = '',
19
        public string $status = 'OK',
20
        public string $memo = '',
21
        public ?string $expiry = null,
22
        public ?string $webhook = null,
23
        public ?string $webhookStatus = null,
24
        public ?string $preimage = null,
25
        public ?string $tag = null,
26
        public ?string $extension = null,
27
        public string $time = '',
28
        public string $createdAt = '',
29
        public string $updatedAt = '',
30
        public ?string $error = null,
31
        public ?InvoiceExtraTransfer $extra = null,
32
    ) {
33
    }
34
35
    public static function fromArray(array $array): self
36
    {
37
        return new self(
38
            checkingId: (string) ($array['checking_id'] ?? ''),
39
            paymentHash: (string) ($array['payment_hash'] ?? ''),
40
            walletId: (string) ($array['wallet_id'] ?? ''),
41
            amount: (int) ($array['amount'] ?? 0),
42
            fee: (int) ($array['fee'] ?? 0),
43
            bolt11: (string) ($array['bolt11'] ?? ''),
44
            status: (string) ($array['status'] ?? ''),
45
            memo: (string) ($array['memo'] ?? ''),
46
            expiry: array_key_exists('expiry', $array) ? (string) $array['expiry'] : null,
47
            webhook: array_key_exists('webhook', $array) ? (string) $array['webhook'] : null,
48
            webhookStatus: array_key_exists('webhook_status', $array) ? (string) $array['webhook_status'] : null,
49
            preimage: array_key_exists('preimage', $array) ? (string) $array['preimage'] : null,
50
            tag: array_key_exists('tag', $array) ? (string) $array['tag'] : null,
51
            extension: array_key_exists('extension', $array) ? (string) $array['extension'] : null,
52
            time: (string) ($array['time'] ?? ''),
53
            createdAt: (string) ($array['created_at'] ?? ''),
54
            updatedAt: (string) ($array['updated_at'] ?? ''),
55
            extra: isset($array['extra']) && is_array($array['extra'])
56
                ? InvoiceExtraTransfer::fromArray($array['extra'])
57
                : null,
58
        );
59
    }
60
}
61