UsPhysicalAddress::equals()   F
last analyzed

Complexity

Conditions 37
Paths > 20000

Size

Total Lines 48
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 37
eloc 38
nc 1787667
nop 1
dl 0
loc 48
rs 0
c 0
b 0
f 0

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
2
3
declare(strict_types=1);
4
5
namespace Talentify\ValueObject\Geography\Address\Physical\ByCountry\Us;
6
7
use Talentify\ValueObject\Geography\Address\ByCountry\Us\County;
8
use Talentify\ValueObject\Geography\Address\ByCountry\Us\District;
9
use Talentify\ValueObject\Geography\Address\ByCountry\Us\State;
10
use Talentify\ValueObject\Geography\Address\ByCountry\Us\ZipCode;
11
use Talentify\ValueObject\Geography\Address\City;
12
use Talentify\ValueObject\Geography\Address\Country;
13
use Talentify\ValueObject\Geography\Address\Physical\PhysicalAddress;
14
use Talentify\ValueObject\Geography\Address\PostalCode;
15
use Talentify\ValueObject\Geography\Address\Region;
16
use Talentify\ValueObject\Geography\Address\Street;
17
use Talentify\ValueObject\Geography\CountryList;
18
use Talentify\ValueObject\StringUtils;
19
use Talentify\ValueObject\ValueObject;
20
use function is_object;
21
22
/**
23
 * Represents an address on the United States of America (USA).
24
 *
25
 * Example:
26
 *  - new UsPhysicalAddress(new Street('Broad St', '400'), new City('Seattle'), new County('King County'), new State('Washington'));
27
 *
28
 * @see https://en.wikipedia.org/wiki/Address#United_States
29
 */
30
final class UsPhysicalAddress implements PhysicalAddress
31
{
32
    /** @var \Talentify\ValueObject\Geography\Address\Street|null */
33
    private $street;
34
    /** @var \Talentify\ValueObject\Geography\Address\City|null */
35
    private $city;
36
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Us\County|null */
37
    private $county;
38
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Us\State|null */
39
    private $state;
40
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Us\ZipCode|null */
41
    private $zipCode;
42
    /** @var \Talentify\ValueObject\Geography\Address\Country */
43
    private $country;
44
    /** @var string */
45
    private $formattedAddress;
46
47
    /**
48
     * @throws \InvalidArgumentException
49
     */
50
    public function __construct(
51
        ?Street $street = null,
52
        ?City $town = null,
53
        ?County $county = null,
54
        ?State $state = null,
55
        ?ZipCode $zipCode = null,
56
        ?string $formattedAddress = null
57
    ) {
58
        $this->street           = $street;
59
        $this->city             = $town;
60
        $this->county           = $county;
61
        $this->state            = $state;
62
        $this->zipCode          = $zipCode;
63
        $this->country          = CountryList::US();
64
        $this->formattedAddress = $formattedAddress;
65
    }
66
67
    public function getStreet() : ?Street
68
    {
69
        return $this->street;
70
    }
71
72
    public function getDistrict() : ?District
73
    {
74
        return null;
75
    }
76
77
    public function getCity() : ?City
78
    {
79
        return $this->city;
80
    }
81
82
    public function getTown() : ?City
83
    {
84
        return $this->city;
85
    }
86
87
    public function getCounty() : ?County
88
    {
89
        return $this->county;
90
    }
91
92
    public function getRegion() : ?Region
93
    {
94
        return $this->state;
95
    }
96
97
    public function getState() : ?Region
98
    {
99
        return $this->state;
100
    }
101
102
    public function getCountry() : Country
103
    {
104
        return $this->country;
105
    }
106
107
    public function getPostalCode() : ?PostalCode
108
    {
109
        return $this->zipCode;
110
    }
111
112
    public function getZipCode() : ?ZipCode
113
    {
114
        return $this->zipCode;
115
    }
116
117
    public function equals(?ValueObject $object) : bool
118
    {
119
        if (!$object instanceof self) {
120
            return false;
121
        }
122
123
        return (
124
            (
125
                (null === $this->getStreet() && null === $object->getStreet()) ||
126
                (is_object($this->getStreet()) && $this->getStreet()
127
                                                       ->equals($object->getStreet())) ||
128
                (is_object($object->getStreet()) && $object->getStreet()
129
                                                           ->equals($this->getStreet()))
130
            ) &&
131
            (
132
                (null === $this->getCity() && null === $object->getCity()) ||
133
                (is_object($this->getCity()) && $this->getCity()
134
                                                     ->equals($object->getCity())) ||
135
                (is_object($object->getCity()) && $object->getCity()
136
                                                         ->equals($this->getCity()))
137
            ) &&
138
            (
139
                (null === $this->getCounty() && null === $object->getCounty()) ||
140
                (is_object($this->getCounty()) && $this->getCounty()
141
                                                       ->equals($object->getCounty())) ||
142
                (is_object($object->getCounty()) && $object->getCounty()
143
                                                           ->equals($this->getCounty()))
144
            ) &&
145
            (
146
                (null === $this->getState() && null === $object->getState()) ||
147
                (is_object($this->getState()) && $this->getState()
148
                                                      ->equals($object->getState())) ||
149
                (is_object($object->getState()) && $object->getState()
150
                                                          ->equals($this->getState()))
151
            ) &&
152
            (
153
                (null === $this->getZipCode() && null === $object->getZipCode()) ||
154
                (is_object($this->getZipCode()) && $this->getZipCode()
155
                                                        ->equals($object->getZipCode())) ||
156
                (is_object($object->getZipCode()) && $object->getZipCode()
157
                                                            ->equals($this->getZipCode()))
158
            ) &&
159
            (
160
                (null === $this->getCountry() && null === $object->getCountry()) ||
161
                (is_object($this->getCountry()) && $this->getCountry()
162
                                                        ->equals($object->getCountry())) ||
163
                (is_object($object->getCountry()) && $object->getCountry()
164
                                                            ->equals($this->getCountry()))
165
            )
166
        );
167
    }
168
169
    /**
170
     *  Example: 400 Broad St, Seattle, WA 98109, EUA
171
     */
172
    public function getAddress() : string
173
    {
174
        if ($this->formattedAddress !== null) {
175
            return $this->formattedAddress;
176
        }
177
178
        $street     = $this->getStreet() ? $this->getStreet()->getFormatted() : '';
179
        $city       = $this->getCity() ? $this->getCity()->getFormatted() : '';
180
        $county     = $this->getCounty() ? $this->getCounty()->getFormatted() : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $county is dead and can be removed.
Loading history...
181
        $state      = $this->getState() ? $this->getState()->__toString() : '';
182
        $postalCode = $this->getPostalCode() ? $this->getPostalCode()->getFormatted() : '';
183
        $country    = $this->getCountry() ? $this->getCountry()->__toString() : '';
184
185
        $values = [$street, $city, $state, $postalCode, $country];
186
        $nonEmptyValues = [];
187
        foreach ($values as $value) {
188
            if($value !== ''){
189
                $nonEmptyValues[] = $value;
190
            }
191
        }
192
193
        $formatted = implode(', ', $nonEmptyValues);
194
195
        return StringUtils::trimSpacesWisely($formatted);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Talentify\ValueOb...pacesWisely($formatted) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
196
    }
197
198
    public function __toString() : string
199
    {
200
        return $this->getAddress();
201
    }
202
}
203