PayeeDetails::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 23
rs 9.9332

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
 * Payee details
7
 */
8
class PayeeDetails
9
{
10
    protected $firstName;
11
    protected $lastName;
12
    protected $email;
13
    protected $phone;
14
    protected $company;
15
    protected $address;
16
    protected $city;
17
    protected $state;
18
    protected $postcode;
19
    protected $country;
20
21
    /**
22
     * Instantiate payee details.
23
     *
24
     * @param string $firstName First name (optional)
25
     * @param string $lastName  Last name (optional)
26
     * @param string $email     Email address (optional)
27
     * @param string $phone     Phone number (optional)
28
     * @param string $company   Company name (optional)
29
     * @param string $address   Street address (optional)
30
     * @param string $city      City name (optional)
31
     * @param string $state     State code (ISO 3166-2 code, optional)
32
     * @param string $postcode  Postcode (optional)
33
     * @param string $country   Country code (ISO 3166-1 alpha-2 code, optional)
34
     */
35
    public function __construct(
36
        string $firstName = '',
37
        string $lastName = '',
38
        string $email = '',
39
        string $phone = '',
40
        string $company = '',
41
        string $address = '',
42
        string $city = '',
43
        string $state = '',
44
        string $postcode = '',
45
        string $country = ''
46
    ) {
47
48
        $this->firstName = $firstName;
49
        $this->lastName = $lastName;
50
        $this->email = $email;
51
        $this->phone = $phone;
52
        $this->company = $company;
53
        $this->address = $address;
54
        $this->city = $city;
55
        $this->state = $state;
56
        $this->postcode = $postcode;
57
        $this->country = $country;
58
    }
59
60
    /**
61
     * Generate partial GetLoy widget payload configuration for the instance.
62
     *
63
     * @return array Partial widget payload configuration.
64
     */
65
    public function payloadConfig(): array
66
    {
67
        $payload = [];
68
        
69
        $payloadMapping = [
70
            'first_name' => 'firstName',
71
            'last_name' => 'lastName',
72
            'email_address' => 'email',
73
            'mobile_number' => 'phone',
74
            'company' => 'company',
75
            'address' => 'address',
76
            'city' => 'city',
77
            'state' => 'state',
78
            'country' => 'country',
79
        ];
80
        foreach ($payloadMapping as $payloadField => $propName) {
81
            if ($this->$propName) {
82
                $payload[$payloadField] = $this->$propName;
83
            }
84
        }
85
86
        return $payload;
87
    }
88
}
89