| Total Complexity | 59 |
| Total Lines | 171 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UsPhysicalAddress 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 UsPhysicalAddress, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 |
||
| 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 |
||
| 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() : ''; |
||
|
|
|||
| 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); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function __toString() : string |
||
| 201 | } |
||
| 202 | } |
||
| 203 |