Failed Conditions
Pull Request — master (#229)
by
unknown
06:06
created

Address::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 11

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 Sylius\ShopApiPlugin\Model;
6
7
use Symfony\Component\HttpFoundation\Request;
8
use Webmozart\Assert\Assert;
9
10
final class Address
11
{
12
    /**
13
     * @var int
14
     */
15
    private $id;
16
17
    /**
18
     * @var string
19
     */
20
    private $firstName;
21
22
    /**
23
     * @var string
24
     */
25
    private $lastName;
26
27
    /**
28
     * @var string
29
     */
30
    private $city;
31
32
    /**
33
     * @var string
34
     */
35
    private $countryCode;
36
37
    /**
38
     * @var string
39
     */
40
    private $street;
41
42
    /**
43
     * @var string
44
     */
45
    private $postcode;
46
47
    /**
48
     * @var string
49
     */
50
    private $provinceName;
51
52
    /**
53
     * @var string
54
     */
55
    private $provinceCode;
56
57
    /**
58
     * @var string
59
     */
60
    private $company;
61
62
    /**
63
     * @var string
64
     */
65
    private $phoneNumber;
66
67
    /**
68
     * @param null $id
69
     * @param string $firstName
70
     * @param string $lastName
71
     * @param string $city
72
     * @param string $street
73
     * @param string $countryCode
74
     * @param string $postcode
75
     * @param string $provinceName
76
     * @param null $provinceCode
77
     * @param null $company
78
     * @param null $phoneNumber
79
     */
80
    private function __construct(
81
        $id = null,
82
        $firstName,
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
83
        $lastName,
84
        $city,
85
        $street,
86
        $countryCode,
87
        $postcode,
88
        $provinceName = null,
89
        $provinceCode = null,
90
        $phoneNumber = null,
91
        $company = null
92
    ) {
93
        $this->id = $id;
94
        $this->firstName = $firstName;
95
        $this->lastName = $lastName;
96
        $this->city = $city;
97
        $this->street = $street;
98
        $this->countryCode = $countryCode;
99
        $this->postcode = $postcode;
100
        $this->provinceName = $provinceName;
101
        $this->provinceCode = $provinceCode;
102
        $this->phoneNumber = $phoneNumber;
103
        $this->company = $company;
104
    }
105
106
    /**
107
     * @param array $address
108
     *
109
     * @return Address
110
     */
111
    public static function createFromArray(array $address)
112
    {
113
        Assert::keyExists($address, 'firstName');
114
        Assert::keyExists($address, 'lastName');
115
        Assert::keyExists($address, 'city');
116
        Assert::keyExists($address, 'street');
117
        Assert::keyExists($address, 'countryCode');
118
        Assert::keyExists($address, 'postcode');
119
120
        return new self(
121
            $address['id'] ?? null,
122
            $address['firstName'],
123
            $address['lastName'],
124
            $address['city'],
125
            $address['street'],
126
            $address['countryCode'],
127
            $address['postcode'],
128
            $address['provinceName'] ?? null,
129
            $address['provinceCode'] ?? null,
130
            $address['phoneNumber'] ?? null,
131
            $address['company'] ?? null
132
        );
133
    }
134
135
    /**
136
     * @param Request $request
137
     *
138
     * @return Address
139
     */
140
    public static function createFromRequest(Request $request)
141
    {
142
        return new self(
143
            $request->attributes->get('id') ?? $request->request->get('id'),
144
            $request->request->get('firstName'),
145
            $request->request->get('lastName'),
146
            $request->request->get('city'),
147
            $request->request->get('street'),
148
            $request->request->get('countryCode'),
149
            $request->request->get('postcode'),
150
            $request->request->get('provinceName'),
151
            $request->request->get('provinceCode'),
152
            $request->request->get('phoneNumber'),
153
            $request->request->get('company')
154
        );
155
    }
156
157
    /**
158
     * @return mixed
159
     */
160
    public function id()
161
    {
162
        return $this->id;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function firstName()
169
    {
170
        return $this->firstName;
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function lastName()
177
    {
178
        return $this->lastName;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function city()
185
    {
186
        return $this->city;
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function street()
193
    {
194
        return $this->street;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function countryCode()
201
    {
202
        return $this->countryCode;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function postcode()
209
    {
210
        return $this->postcode;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function provinceName()
217
    {
218
        return $this->provinceName;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function provinceCode()
225
    {
226
        return $this->provinceCode;
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function company()
233
    {
234
        return $this->company;
235
    }
236
237
    /**
238
     * @return string
239
     */
240
    public function phoneNumber()
241
    {
242
        return $this->phoneNumber;
243
    }
244
}
245