Passed
Push — master ( 26e0e4...4a85b9 )
by Peter
10:37
created

Addr   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 365
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 54
eloc 97
dl 0
loc 365
rs 6.4799
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreetAddressLine() 0 3 1
A setCity() 0 4 1
A getStreetNameType() 0 3 1
A hasState() 0 3 1
C toDOMElement() 0 34 10
A getHouseNumber() 0 3 1
A setStreetAddressLine() 0 4 1
A setStreetNameType() 0 4 1
A setState() 0 4 1
A hasCity() 0 3 1
A getCity() 0 3 1
A getStreetName() 0 3 1
A setAdditionalLocator() 0 4 1
A getCountry() 0 3 1
A hasPostalCode() 0 3 1
A setPostalCode() 0 4 1
A getState() 0 3 1
A getElementTag() 0 3 1
A getAdditionalLocator() 0 3 1
A hasAdditionalLocator() 0 3 1
A hasCountry() 0 3 1
F __construct() 0 43 16
A setUseAttribute() 0 6 2
A getPostalCode() 0 3 1
A setStreetName() 0 4 1
A setCountry() 0 4 1
A getUseAttribute() 0 3 1
A setHouseNumber() 0 4 1
A hasStreetAddressLine() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Addr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Addr, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
4
/**
5
 * The MIT License
6
 *
7
 * Copyright 2018  Peter Gee <https://github.com/pgee70>.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27
28
namespace i3Soft\CDA\Elements\Address;
29
30
/**
31
 *
32
 * @package     i3Soft\CDA
33
 * @author      Peter Gee <https://github.com/pgee70>
34
 * @link        https://github.com/pgee70/cda
35
 *
36
 */
37
38
39
use i3Soft\CDA\DataType\Code\AddressCodeType;
40
use i3Soft\CDA\Elements\AbstractElement;
41
42
/**
43
 * Class Addr
44
 *
45
 * @package i3Soft\CDA\Elements\Address
46
 */
47
class Addr extends AbstractElement
48
{
49
50
    /** @var AddressCodeType $address_code_use_attribute */
51
    protected $address_code_use_attribute;
52
    /** @var HouseNumber */
53
    private $house_number;
54
    /** @var StreetName */
55
    private $street_name;
56
    /** @var StreetNameType */
57
    private $street_name_type;
58
    /** @var StreetAddressLine */
59
    private $street_address_line;
60
    /** @var City */
61
    private $city;
62
    /** @var PostalCode */
63
    private $postal_code;
64
    /** @var State */
65
    private $state;
66
    /** @var Country */
67
    private $country;
68
    /** @var AdditionalLocator */
69
    private $additional_locator;
70
71
    /**
72
     * Addr constructor.
73
     *
74
     * @param null $street_address_line
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $street_address_line is correct as it would always require null to be passed?
Loading history...
75
     * @param null $city
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $city is correct as it would always require null to be passed?
Loading history...
76
     * @param null $state
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $state is correct as it would always require null to be passed?
Loading history...
77
     * @param null $postal_code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $postal_code is correct as it would always require null to be passed?
Loading history...
78
     * @param null $additional_locator
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $additional_locator is correct as it would always require null to be passed?
Loading history...
79
     */
80
    public function __construct(
81
      $street_address_line = null,
82
      $city = null,
83
      $state = null,
84
      $postal_code = null,
85
      $additional_locator = null
86
    ) {
87
        if ($street_address_line) {
88
            if ($street_address_line instanceof StreetAddressLine) {
89
                $this->setStreetAddressLine($street_address_line);
90
            } elseif (\is_string($street_address_line)) {
91
                $this->setStreetAddressLine(new StreetAddressLine($street_address_line));
92
            }
93
        }
94
        if ($city) {
95
            if ($city instanceof City) {
96
                $this->setCity($city);
97
            } elseif (\is_string($city)) {
98
                $this->setCity(new City($city));
99
            }
100
        }
101
        if ($state) {
102
            if ($state instanceof State) {
103
                $this->setState($state);
104
            } elseif (\is_string($state)) {
105
                $this->setState(new State($state));
106
            }
107
        }
108
        if ($postal_code) {
109
            if ($postal_code instanceof PostalCode) {
110
                $this->setPostalCode($postal_code);
111
            } elseif (\is_string($postal_code)) {
112
                $this->setPostalCode(new PostalCode($postal_code));
113
            }
114
        }
115
        if ($additional_locator) {
116
            if ($additional_locator instanceof AdditionalLocator) {
117
                $this->setAdditionalLocator($additional_locator);
118
            } elseif (\is_string($additional_locator)) {
119
                $this->setAdditionalLocator(new AdditionalLocator($additional_locator));
120
            }
121
        }
122
        $this->address_code_use_attribute = new AddressCodeType('');
123
    }
124
125
    /**
126
     * @return AddressCodeType
127
     */
128
    public function getUseAttribute(): AddressCodeType
129
    {
130
        return $this->address_code_use_attribute;
131
    }
132
133
    /**
134
     * @param $address_code_type
135
     *
136
     * @return Addr
137
     */
138
    public function setUseAttribute($address_code_type): Addr
139
    {
140
        $this->address_code_use_attribute = $address_code_type instanceof AddressCodeType
141
          ? $address_code_type
142
          : new AddressCodeType($address_code_type);
143
        return $this;
144
    }
145
146
    /**
147
     * @param \DOMDocument $doc
148
     *
149
     * @return \DOMElement
150
     */
151
    public function toDOMElement(\DOMDocument $doc): \DOMElement
152
    {
153
        $el = $this->createElement($doc, ['address_code_use_attribute']);
154
        // if the street address line is present use it, otherwise see if the individual components were set.
155
        if ($this->hasStreetAddressLine()) {
156
            $el->appendChild($this->getStreetAddressLine()->toDOMElement($doc));
157
        } else {
158
            if (null !== $this->house_number) {
159
                $el->appendChild($this->getHouseNumber()->toDOMElement($doc));
160
            }
161
            if (null !== $this->street_name) {
162
                $el->appendChild($this->getStreetName()->toDOMElement($doc));
163
            }
164
            if (null !== $this->street_name_type) {
165
                $el->appendChild($this->getStreetNameType()->toDOMElement($doc));
166
            }
167
        }
168
169
        if ($this->hasCity()) {
170
            $el->appendChild($this->getCity()->toDOMElement($doc));
171
        }
172
        if ($this->hasState()) {
173
            $el->appendChild($this->getState()->toDOMElement($doc));
174
        }
175
        if ($this->hasPostalCode()) {
176
            $el->appendChild($this->getPostalCode()->toDOMElement($doc));
177
        }
178
        if ($this->hasAdditionalLocator()) {
179
            $el->appendChild($this->getAdditionalLocator()->toDOMElement($doc));
180
        }
181
        if ($this->hasCountry()) {
182
            $el->appendChild($this->getCountry()->toDOMElement($doc));
183
        }
184
        return $el;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public function hasStreetAddressLine(): bool
191
    {
192
        return $this->street_address_line !== null;
193
    }
194
195
    /**
196
     * @return StreetAddressLine
197
     */
198
    public function getStreetAddressLine(): StreetAddressLine
199
    {
200
        return $this->street_address_line;
201
    }
202
203
    /**
204
     * @param StreetAddressLine $street_address_line
205
     *
206
     * @return Addr
207
     */
208
    public function setStreetAddressLine(StreetAddressLine $street_address_line): Addr
209
    {
210
        $this->street_address_line = $street_address_line;
211
        return $this;
212
    }
213
214
    /**
215
     * @return HouseNumber
216
     */
217
    public function getHouseNumber(): HouseNumber
218
    {
219
        return $this->house_number;
220
    }
221
222
    /**
223
     * @param HouseNumber $house_number
224
     *
225
     * @return Addr
226
     */
227
    public function setHouseNumber(HouseNumber $house_number): Addr
228
    {
229
        $this->house_number = $house_number;
230
        return $this;
231
    }
232
233
    /**
234
     * @return StreetName
235
     */
236
    public function getStreetName(): StreetName
237
    {
238
        return $this->street_name;
239
    }
240
241
    /**
242
     * @param StreetName $street_name
243
     *
244
     * @return Addr
245
     */
246
    public function setStreetName(StreetName $street_name): Addr
247
    {
248
        $this->street_name = $street_name;
249
        return $this;
250
    }
251
252
    /**
253
     * @return StreetNameType
254
     */
255
    public function getStreetNameType(): StreetNameType
256
    {
257
        return $this->street_name_type;
258
    }
259
260
    /**
261
     * @param StreetNameType $street_name_type
262
     *
263
     * @return Addr
264
     */
265
    public function setStreetNameType(StreetNameType $street_name_type): Addr
266
    {
267
        $this->street_name_type = $street_name_type;
268
        return $this;
269
    }
270
271
    /**
272
     * @return bool
273
     */
274
    public function hasCity(): bool
275
    {
276
        return null !== $this->city;
277
    }
278
279
    /**
280
     * @return City
281
     */
282
    public function getCity(): City
283
    {
284
        return $this->city;
285
    }
286
287
    /**
288
     * @param City $city
289
     *
290
     * @return Addr
291
     */
292
    public function setCity(City $city): Addr
293
    {
294
        $this->city = $city;
295
        return $this;
296
    }
297
298
    /**
299
     * @return bool
300
     */
301
    public function hasState(): bool
302
    {
303
        return null !== $this->state;
304
    }
305
306
    /**
307
     * @return State
308
     */
309
    public function getState(): State
310
    {
311
        return $this->state;
312
    }
313
314
    /**
315
     * @param State $state
316
     *
317
     * @return Addr
318
     */
319
    public function setState(State $state): Addr
320
    {
321
        $this->state = $state;
322
        return $this;
323
    }
324
325
    /**
326
     * @return bool
327
     */
328
    public function hasPostalCode(): bool
329
    {
330
        return null !== $this->postal_code;
331
    }
332
333
    /**
334
     * @return PostalCode
335
     */
336
    public function getPostalCode(): PostalCode
337
    {
338
        return $this->postal_code;
339
    }
340
341
    /**
342
     * @param PostalCode $postal_code
343
     *
344
     * @return Addr
345
     */
346
    public function setPostalCode(PostalCode $postal_code): Addr
347
    {
348
        $this->postal_code = $postal_code;
349
        return $this;
350
    }
351
352
    /**
353
     * @return bool
354
     */
355
    public function hasAdditionalLocator(): bool
356
    {
357
        return null !== $this->additional_locator;
358
    }
359
360
    /**
361
     * @return AdditionalLocator
362
     */
363
    public function getAdditionalLocator(): AdditionalLocator
364
    {
365
        return $this->additional_locator;
366
    }
367
368
    /**
369
     * @param AdditionalLocator $additional_locator
370
     *
371
     * @return Addr
372
     */
373
    public function setAdditionalLocator(AdditionalLocator $additional_locator): Addr
374
    {
375
        $this->additional_locator = $additional_locator;
376
        return $this;
377
    }
378
379
    /**
380
     * @return bool
381
     */
382
    public function hasCountry(): bool
383
    {
384
        return null !== $this->country;
385
    }
386
387
    /**
388
     * @return Country
389
     */
390
    public function getCountry(): Country
391
    {
392
        return $this->country;
393
    }
394
395
    /**
396
     * @param Country $country
397
     *
398
     * @return Addr
399
     */
400
    public function setCountry(Country $country): Addr
401
    {
402
        $this->country = $country;
403
        return $this;
404
    }
405
406
    /**
407
     * @return string
408
     */
409
    protected function getElementTag(): string
410
    {
411
        return 'addr';
412
    }
413
}
414