Completed
Pull Request — master (#90)
by
unknown
04:57 queued 01:40
created

AVAddress::__construct()   F

Complexity

Conditions 20
Paths 2049

Size

Total Lines 29
Code Lines 21

Duplication

Lines 6
Ratio 20.69 %

Code Coverage

Tests 20
CRAP Score 26.9737

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 6
loc 29
ccs 20
cts 27
cp 0.7407
rs 2.6863
cc 20
eloc 21
nc 2049
nop 1
crap 26.9737

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Ups\Entity\AddressValidation;
2
3
class AVAddress
4
{
5
    /**
6
     * @var null|AddressClassification
7
     */
8
    public $addressClassification;
9
    /**
10
     * @var string
11
     */
12
    public $consigneeName;
13
    /**
14
     * @var string
15
     */
16
    public $buildingName;
17
    /**
18
     * @var string
19
     */
20
    public $addressLine;
21
    /**
22
     * @var string
23
     */
24
    public $addressLine2;
25
    /**
26
     * @var string
27
     */
28
    public $addressLine3;
29
    /**
30
     * @var string
31
     */
32
    public $region;
33
    /**
34
     * @var string
35
     */
36
    public $politicalDivision2;
37
    /**
38
     * @var string
39
     */
40
    public $politicalDivision1;
41
    /**
42
     * @var string
43
     */
44
    public $postcodePrimaryLow;
45
    /**
46
     * @var string
47
     */
48
    public $postcodeExtendedLow;
49
    /**
50
     * @var string
51
     */
52
    public $urbanization;
53
    /**
54
     * @var
55
     */
56
    public $countryCode;
57
58
    /**
59
     * Address constructor.
60
     * @param \SimpleXMLElement $xmlDoc
61
     */
62 2
    public function __construct(\SimpleXMLElement $xmlDoc)
63
    {
64 2
        if ($xmlDoc->count() == 0) {
65
            throw new \InvalidArgumentException(__METHOD__ . ': The passed object does not have any child nodes.');
66
        }
67 2
        $this->addressClassification = isset($xmlDoc->AddressClassification) ? new AddressClassification($xmlDoc->AddressClassification) : null;
68 2
        $this->consigneeName = isset($xmlDoc->ConsigneeName) ? (string)$xmlDoc->ConsigneeName : '';
69 2
        $this->buildingName = isset($xmlDoc->BuildingName) ? (string)$xmlDoc->BuildingName : '';
70 2
        if (isset($xmlDoc->AddressLine)) {
71 2
            for ($i = 0, $len = count($xmlDoc->AddressLine); $i < $len; $i++) {
72 2
                if ($i === 0) {
73 2
                    $this->addressLine = isset($xmlDoc->AddressLine[$i]) ? (string) $xmlDoc->AddressLine[$i] : '';
74 2
                }
75 View Code Duplication
                elseif ($i === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                    $this->addressLine2 = isset($xmlDoc->AddressLine[$i]) ? (string) $xmlDoc->AddressLine[$i] : '';
77
                }
78 View Code Duplication
                elseif ($i === 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
                    $this->addressLine3 = isset($xmlDoc->AddressLine[$i]) ? (string) $xmlDoc->AddressLine[$i] : '';
80
                }
81 2
            }
82 2
        }
83 2
        $this->region = isset($xmlDoc->Region) ? (string)$xmlDoc->Region : '';
84 2
        $this->politicalDivision2 = isset($xmlDoc->PoliticalDivision2) ? (string)$xmlDoc->PoliticalDivision2 : '';
85 2
        $this->politicalDivision1 = isset($xmlDoc->PoliticalDivision1) ? (string)$xmlDoc->PoliticalDivision1 : '';
86 2
        $this->postcodePrimaryLow = isset($xmlDoc->PostcodePrimaryLow) ? (string)$xmlDoc->PostcodePrimaryLow : '';
87 2
        $this->postcodeExtendedLow = isset($xmlDoc->PostcodeExtendedLow) ? (string)$xmlDoc->PostcodeExtendedLow : '';
88 2
        $this->urbanization = isset($xmlDoc->Urbanization) ? (string)$xmlDoc->Urbanization : '';
89 2
        $this->countryCode = isset($xmlDoc->CountryCode) ? (string)$xmlDoc->CountryCode : '';
90 2
    }
91
92
    /**
93
     * Convenience methods. Even though all properties are public, these methods provide a convenient interface to
94
     * retrieve commonly requested parts so that the user doesn't have to remember which API fields reference
95
     * which piece of information. For example, I won't have to remember that the city is in 'PoliticalDivision2'.
96
     */
97
98
    /**
99
     * @return string
100
     */
101 1
    public function getCity()
102
    {
103 1
        return $this->politicalDivision2;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 1
    public function getStateProvince()
110
    {
111 1
        return $this->politicalDivision1;
112
    }
113
114
    /**
115
     * Return the address postal code
116
     *
117
     * @return string
118
     */
119 1
    public function getPostalCode()
120
    {
121 1
        return $this->postcodePrimaryLow;
122
    }
123
124
    /**
125
     * Return the address postal code with extension (i.e. the U.S. extended zip+4 postal code)
126
     *
127
     * @param string $divider
128
     * @return string
129
     */
130 1
    public function getPostalCodeWithExtension($divider = '-')
131
    {
132 1
        return $this->postcodePrimaryLow . $divider . $this->postcodeExtendedLow;
133
    }
134
}
135