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

Address::city()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 string $firstName
69
     * @param string $lastName
70
     * @param string $city
71
     * @param string $street
72
     * @param string $countryCode
73
     * @param string $postcode
74
     * @param string $id
75
     * @param string $provinceName
76
     * @param string $provinceCode
77
     * @param string $company
78
     * @param string $phoneNumber
79
     */
80
    private function __construct(
81
        $firstName,
82
        $lastName,
83
        $city,
84
        $street,
85
        $countryCode,
86
        $postcode,
87
        $id = null,
88
        $provinceName = null,
89
        $provinceCode = null,
90
        $phoneNumber = null,
91
        $company = null
92
    ) {
93
        $this->firstName = $firstName;
94
        $this->lastName = $lastName;
95
        $this->city = $city;
96
        $this->street = $street;
97
        $this->countryCode = $countryCode;
98
        $this->postcode = $postcode;
99
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type string. However, the property $id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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['firstName'],
122
            $address['lastName'],
123
            $address['city'],
124
            $address['street'],
125
            $address['countryCode'],
126
            $address['postcode'],
127
            $address['id'] ?? null,
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->request->get('firstName'),
144
            $request->request->get('lastName'),
145
            $request->request->get('city'),
146
            $request->request->get('street'),
147
            $request->request->get('countryCode'),
148
            $request->request->get('postcode'),
149
            $request->attributes->get('id') ?? $request->request->get('id'),
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