AddressDetails::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 19
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 declare(strict_types=1);
2
3
namespace Getloy\TransactionDetails;
4
5
/**
6
 * Transaction address details.
7
 */
8
class AddressDetails
9
{
10
    protected $firstName;
11
    protected $lastName;
12
    protected $company;
13
    protected $address;
14
    protected $city;
15
    protected $state;
16
    protected $postcode;
17
    protected $country;
18
19
    /**
20
     * Instantiate address details.
21
     *
22
     * @param string $firstName First name
23
     * @param string $lastName  Last name
24
     * @param string $company   Company name (optional)
25
     * @param string $address   Street address (optional)
26
     * @param string $city      City name (optional)
27
     * @param string $state     State name (optional)
28
     * @param string $postcode  Postcode (optional)
29
     * @param string $country   Country name (optional)
30
     */
31
    public function __construct(
32
        string $firstName,
33
        string $lastName,
34
        string $company = '',
35
        string $address = '',
36
        string $city = '',
37
        string $state = '',
38
        string $postcode = '',
39
        string $country = ''
40
    ) {
41
42
        $this->firstName = $firstName;
43
        $this->lastName = $lastName;
44
        $this->company = $company;
45
        $this->address = $address;
46
        $this->city = $city;
47
        $this->state = $state;
48
        $this->postcode = $postcode;
49
        $this->country = $country;
50
    }
51
52
    /**
53
     * Generate partial GetLoy widget payload configuration for the instance.
54
     *
55
     * @return array Partial widget payload configuration.
56
     */
57
    public function payloadConfig(): array
58
    {
59
        $payload = [
60
            'first_name' => $this->firstName,
61
            'last_name' => $this->lastName,
62
        ];
63
        if ($this->company) {
64
            $payload['company'] = $this->company;
65
        }
66
        if ($this->address) {
67
            $payload['address'] = $this->address;
68
        }
69
        if ($this->city) {
70
            $payload['city'] = $this->city;
71
        }
72
        if ($this->state) {
73
            $payload['state'] = $this->state;
74
        }
75
        if ($this->postcode) {
76
            $payload['postcode'] = $this->postcode;
77
        }
78
        if ($this->country) {
79
            $payload['country'] = $this->country;
80
        }
81
        return $payload;
82
    }
83
}
84