| Total Complexity | 54 |
| Total Lines | 396 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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 |
||
|
|
|||
| 75 | * @param null $city |
||
| 76 | * @param null $state |
||
| 77 | * @param null $postal_code |
||
| 78 | * @param null $additional_locator |
||
| 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 | { |
||
| 89 | if ($street_address_line instanceof StreetAddressLine) |
||
| 90 | { |
||
| 91 | $this->setStreetAddressLine($street_address_line); |
||
| 92 | } |
||
| 93 | elseif (\is_string($street_address_line)) |
||
| 94 | { |
||
| 95 | $this->setStreetAddressLine(new StreetAddressLine($street_address_line)); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | if ($city) |
||
| 99 | { |
||
| 100 | if ($city instanceof City) |
||
| 101 | { |
||
| 102 | $this->setCity($city); |
||
| 103 | } |
||
| 104 | elseif (\is_string($city)) |
||
| 105 | { |
||
| 106 | $this->setCity(new City($city)); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | if ($state) |
||
| 110 | { |
||
| 111 | if ($state instanceof State) |
||
| 112 | { |
||
| 113 | $this->setState($state); |
||
| 114 | } |
||
| 115 | elseif (\is_string($state)) |
||
| 116 | { |
||
| 117 | $this->setState(new State($state)); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | if ($postal_code) |
||
| 121 | { |
||
| 122 | if ($postal_code instanceof PostalCode) |
||
| 123 | { |
||
| 124 | $this->setPostalCode($postal_code); |
||
| 125 | } |
||
| 126 | elseif (\is_string($postal_code)) |
||
| 127 | { |
||
| 128 | $this->setPostalCode(new PostalCode($postal_code)); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | if ($additional_locator) |
||
| 132 | { |
||
| 133 | if ($additional_locator instanceof AdditionalLocator) |
||
| 134 | { |
||
| 135 | $this->setAdditionalLocator($additional_locator); |
||
| 136 | } |
||
| 137 | elseif (\is_string($additional_locator)) |
||
| 138 | { |
||
| 139 | $this->setAdditionalLocator(new AdditionalLocator($additional_locator)); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | $this->address_code_use_attribute = new AddressCodeType(''); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return AddressCodeType |
||
| 147 | */ |
||
| 148 | public function getUseAttribute (): AddressCodeType |
||
| 149 | { |
||
| 150 | return $this->address_code_use_attribute; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $address_code_type |
||
| 155 | * |
||
| 156 | * @return Addr |
||
| 157 | */ |
||
| 158 | public function setUseAttribute ($address_code_type): Addr |
||
| 159 | { |
||
| 160 | $this->address_code_use_attribute = $address_code_type instanceof AddressCodeType |
||
| 161 | ? $address_code_type |
||
| 162 | : new AddressCodeType($address_code_type); |
||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param \DOMDocument $doc |
||
| 168 | * |
||
| 169 | * @return \DOMElement |
||
| 170 | */ |
||
| 171 | public function toDOMElement (\DOMDocument $doc): \DOMElement |
||
| 172 | { |
||
| 173 | $el = $this->createElement($doc, ['address_code_use_attribute']); |
||
| 174 | // if the street address line is present use it, otherwise see if the individual components were set. |
||
| 175 | if ($this->hasStreetAddressLine()) |
||
| 176 | { |
||
| 177 | $el->appendChild($this->getStreetAddressLine()->toDOMElement($doc)); |
||
| 178 | } |
||
| 179 | else |
||
| 180 | { |
||
| 181 | if (NULL !== $this->house_number) |
||
| 182 | { |
||
| 183 | $el->appendChild($this->getHouseNumber()->toDOMElement($doc)); |
||
| 184 | } |
||
| 185 | if (NULL !== $this->street_name) |
||
| 186 | { |
||
| 187 | $el->appendChild($this->getStreetName()->toDOMElement($doc)); |
||
| 188 | } |
||
| 189 | if (NULL !== $this->street_name_type) |
||
| 190 | { |
||
| 191 | $el->appendChild($this->getStreetNameType()->toDOMElement($doc)); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | if ($this->hasCity()) |
||
| 196 | { |
||
| 197 | $el->appendChild($this->getCity()->toDOMElement($doc)); |
||
| 198 | } |
||
| 199 | if ($this->hasState()) |
||
| 200 | { |
||
| 201 | $el->appendChild($this->getState()->toDOMElement($doc)); |
||
| 202 | } |
||
| 203 | if ($this->hasPostalCode()) |
||
| 204 | { |
||
| 205 | $el->appendChild($this->getPostalCode()->toDOMElement($doc)); |
||
| 206 | } |
||
| 207 | if ($this->hasAdditionalLocator()) |
||
| 208 | { |
||
| 209 | $el->appendChild($this->getAdditionalLocator()->toDOMElement($doc)); |
||
| 210 | } |
||
| 211 | if ($this->hasCountry()) |
||
| 212 | { |
||
| 213 | $el->appendChild($this->getCountry()->toDOMElement($doc)); |
||
| 214 | } |
||
| 215 | return $el; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function hasStreetAddressLine (): bool |
||
| 222 | { |
||
| 223 | return $this->street_address_line !== NULL; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return StreetAddressLine |
||
| 228 | */ |
||
| 229 | public function getStreetAddressLine (): StreetAddressLine |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param StreetAddressLine $street_address_line |
||
| 236 | * |
||
| 237 | * @return Addr |
||
| 238 | */ |
||
| 239 | public function setStreetAddressLine (StreetAddressLine $street_address_line): Addr |
||
| 240 | { |
||
| 241 | $this->street_address_line = $street_address_line; |
||
| 242 | return $this; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return HouseNumber |
||
| 247 | */ |
||
| 248 | public function getHouseNumber (): HouseNumber |
||
| 249 | { |
||
| 250 | return $this->house_number; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param HouseNumber $house_number |
||
| 255 | * |
||
| 256 | * @return Addr |
||
| 257 | */ |
||
| 258 | public function setHouseNumber (HouseNumber $house_number): Addr |
||
| 259 | { |
||
| 260 | $this->house_number = $house_number; |
||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return StreetName |
||
| 266 | */ |
||
| 267 | public function getStreetName (): StreetName |
||
| 268 | { |
||
| 269 | return $this->street_name; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param StreetName $street_name |
||
| 274 | * |
||
| 275 | * @return Addr |
||
| 276 | */ |
||
| 277 | public function setStreetName (StreetName $street_name): Addr |
||
| 278 | { |
||
| 279 | $this->street_name = $street_name; |
||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return StreetNameType |
||
| 285 | */ |
||
| 286 | public function getStreetNameType (): StreetNameType |
||
| 287 | { |
||
| 288 | return $this->street_name_type; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param StreetNameType $street_name_type |
||
| 293 | * |
||
| 294 | * @return Addr |
||
| 295 | */ |
||
| 296 | public function setStreetNameType (StreetNameType $street_name_type): Addr |
||
| 297 | { |
||
| 298 | $this->street_name_type = $street_name_type; |
||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function hasCity (): bool |
||
| 306 | { |
||
| 307 | return NULL !== $this->city; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return City |
||
| 312 | */ |
||
| 313 | public function getCity (): City |
||
| 314 | { |
||
| 315 | return $this->city; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param City $city |
||
| 320 | * |
||
| 321 | * @return Addr |
||
| 322 | */ |
||
| 323 | public function setCity (City $city): Addr |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function hasState (): bool |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return State |
||
| 339 | */ |
||
| 340 | public function getState (): State |
||
| 341 | { |
||
| 342 | return $this->state; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param State $state |
||
| 347 | * |
||
| 348 | * @return Addr |
||
| 349 | */ |
||
| 350 | public function setState (State $state): Addr |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function hasPostalCode (): bool |
||
| 360 | { |
||
| 361 | return NULL !== $this->postal_code; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return PostalCode |
||
| 366 | */ |
||
| 367 | public function getPostalCode (): PostalCode |
||
| 368 | { |
||
| 369 | return $this->postal_code; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param PostalCode $postal_code |
||
| 374 | * |
||
| 375 | * @return Addr |
||
| 376 | */ |
||
| 377 | public function setPostalCode (PostalCode $postal_code): Addr |
||
| 378 | { |
||
| 379 | $this->postal_code = $postal_code; |
||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function hasAdditionalLocator (): bool |
||
| 387 | { |
||
| 388 | return NULL !== $this->additional_locator; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return AdditionalLocator |
||
| 393 | */ |
||
| 394 | public function getAdditionalLocator (): AdditionalLocator |
||
| 395 | { |
||
| 396 | return $this->additional_locator; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param AdditionalLocator $additional_locator |
||
| 401 | * |
||
| 402 | * @return Addr |
||
| 403 | */ |
||
| 404 | public function setAdditionalLocator (AdditionalLocator $additional_locator): Addr |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function hasCountry (): bool |
||
| 414 | { |
||
| 415 | return NULL !== $this->country; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return Country |
||
| 420 | */ |
||
| 421 | public function getCountry (): Country |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param Country $country |
||
| 428 | * |
||
| 429 | * @return Addr |
||
| 430 | */ |
||
| 431 | public function setCountry (Country $country): Addr |
||
| 432 | { |
||
| 433 | $this->country = $country; |
||
| 434 | return $this; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | protected function getElementTag (): string |
||
| 443 | } |
||
| 444 | } |
||
| 445 |