BrPhysicalAddress::equals()   F
last analyzed

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 22
nc 162516
nop 1
dl 0
loc 31
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\Br;
6
7
use Talentify\ValueObject\Geography\Address\ByCountry\Br\CEP as BrPostalCode;
8
use Talentify\ValueObject\Geography\Address\ByCountry\Br\Municipality;
9
use Talentify\ValueObject\Geography\Address\ByCountry\Br\Neighbourhood as BrNeighbourhood;
10
use Talentify\ValueObject\Geography\Address\ByCountry\Br\State as BrState;
11
use Talentify\ValueObject\Geography\Address\ByCountry\Br\Street as BrStreet;
12
use Talentify\ValueObject\Geography\Address\City;
13
use Talentify\ValueObject\Geography\Address\Country;
14
use Talentify\ValueObject\Geography\Address\Physical\PhysicalAddress;
15
use Talentify\ValueObject\Geography\Address\PostalCode;
16
use Talentify\ValueObject\Geography\Address\Region;
17
use Talentify\ValueObject\Geography\Address\Street as BaseStreet;
18
use Talentify\ValueObject\Geography\CountryList;
19
use Talentify\ValueObject\StringUtils;
20
use Talentify\ValueObject\ValueObject;
21
22
/**
23
 * Represents an address on Brazil (BRA).
24
 *
25
 * @see https://en.wikipedia.org/wiki/Address#Brazil
26
 */
27
final class BrPhysicalAddress implements PhysicalAddress
28
{
29
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Br\Street|null */
30
    private $street;
31
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Br\Neighbourhood|null */
32
    private $neighbourhood;
33
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Br\Municipality|null */
34
    private $municipality;
35
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Br\State|null */
36
    private $state;
37
    /** @var \Talentify\ValueObject\Geography\Address\ByCountry\Br\CEP|null */
38
    private $postalCode;
39
    /** @var \Talentify\ValueObject\Geography\Address\Country */
40
    private $country;
41
    /** @var string */
42
    private $formattedAddress;
43
44
    /**
45
     * @throws \InvalidArgumentException
46
     */
47
    public function __construct(
48
        ?BrStreet $street = null,
49
        ?BrNeighbourhood $neighbourhood = null,
50
        ?Municipality $municipality = null,
51
        ?BrState $state = null,
52
        ?BrPostalCode $postalCode = null,
53
        ?string $formattedAddress = null
54
    ) {
55
        $this->street           = $street;
56
        $this->neighbourhood    = $neighbourhood;
57
        $this->municipality     = $municipality;
58
        $this->state            = $state;
59
        $this->postalCode       = $postalCode;
60
        $this->country          = CountryList::BR();
61
        $this->formattedAddress = $formattedAddress;
62
    }
63
64
    /**
65
     * @return \Talentify\ValueObject\Geography\Address\ByCountry\Br\Street
66
     */
67
    public function getStreet() : ?BaseStreet
68
    {
69
        return $this->street;
70
    }
71
72
    public function getNeighbourhood() : ?BrNeighbourhood
73
    {
74
        return $this->neighbourhood;
75
    }
76
77
    public function getMunicipality() : ?Municipality
78
    {
79
        return $this->municipality;
80
    }
81
82
    /**
83
     * @return \Talentify\ValueObject\Geography\Address\ByCountry\Br\Municipality|null
84
     */
85
    public function getCity() : ?City
86
    {
87
        return $this->municipality;
88
    }
89
90
    /**
91
     * @return \Talentify\ValueObject\Geography\Address\ByCountry\Br\State|null
92
     */
93
    public function getRegion() : ?Region
94
    {
95
        return $this->state;
96
    }
97
98
    /**
99
     * @return \Talentify\ValueObject\Geography\Address\ByCountry\Br\State|null
100
     */
101
    public function getState() : ?BrState
102
    {
103
        return $this->state;
104
    }
105
106
    /**
107
     * @return \Talentify\ValueObject\Geography\Address\ByCountry\Br\CEP|null
108
     */
109
    public function getPostalCode() : ?PostalCode
110
    {
111
        return $this->postalCode;
112
    }
113
114
    /**
115
     * @return \Talentify\ValueObject\Geography\Address\ByCountry\Br\CEP|null
116
     */
117
    public function getCep() : ? BrPostalCode
118
    {
119
        return $this->postalCode;
120
    }
121
122
    public function getCountry() : Country
123
    {
124
        return $this->country;
125
    }
126
127
    public function equals(?ValueObject $object) : bool
128
    {
129
        if (!$object instanceof self) {
130
            return false;
131
        }
132
133
        return (
134
            (
135
                (null === $this->getStreet() && null === $object->getStreet()) ||
136
                (\is_object($this->getStreet()) && $this->getStreet()->equals($object->getStreet())) ||
137
                (\is_object($object->getStreet()) && $object->getStreet()->equals($this->getStreet()))
138
            ) &&
139
            (
140
                (null === $this->getMunicipality() && null === $object->getMunicipality()) ||
141
                (\is_object($this->getMunicipality()) && $this->getMunicipality()->equals($object->getMunicipality())) ||
142
                (\is_object($object->getMunicipality()) && $object->getMunicipality()->equals($this->getMunicipality()))
143
            ) &&
144
            (
145
                (null === $this->getState() && null === $object->getState()) ||
146
                (\is_object($this->getState()) && $this->getState()->equals($object->getState())) ||
147
                (\is_object($object->getState()) && $object->getState()->equals($this->getState()))
148
            ) &&
149
            (
150
                (null === $this->getCep() && null === $object->getCep()) ||
151
                (\is_object($this->getCep()) && $this->getCep()->equals($object->getCep())) ||
152
                (\is_object($object->getCep()) && $object->getCep()->equals($this->getCep()))
153
            ) &&
154
            (
155
                (null === $this->getCountry() && null === $object->getCountry()) ||
156
                (\is_object($this->getCountry()) && $this->getCountry()->equals($object->getCountry())) ||
157
                (\is_object($object->getCountry()) && $object->getCountry()->equals($this->getCountry()))
158
            )
159
        );
160
    }
161
162
    public function getAddress() : string
163
    {
164
        if ($this->formattedAddress !== null) {
165
            return $this->formattedAddress;
166
        }
167
168
        $street       = $this->getStreet() ? $this->getStreet()->getFormatted() : '';
169
        $municipality = $this->getMunicipality() ? $this->getMunicipality()->getFormatted() : '';
170
        $state        = $this->getState() ? $this->getState()->__toString() : '';
171
        $postalCode   = $this->getPostalCode() ? $this->getPostalCode()->getFormatted() : '';
172
        $country      = $this->getCountry() ? $this->getCountry()->__toString() : '';
173
174
        $values = [$street, $municipality, $state, $postalCode, $country];
175
        $nonEmptyValues = [];
176
        foreach ($values as $value) {
177
            if($value !== ''){
178
                $nonEmptyValues[] = $value;
179
            }
180
        }
181
182
        $formatted = implode(', ', $nonEmptyValues);
183
184
        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...
185
    }
186
187
    public function __toString() : string
188
    {
189
        return $this->getAddress();
190
    }
191
}
192