Complex classes like CustomerInfo 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CustomerInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class CustomerInfo extends Payload implements CustomerInfoInterface |
||
| 8 | { |
||
| 9 | const GENDER_MALE = 'male'; |
||
| 10 | const GENDER_MALE_2 = 'm'; |
||
| 11 | const GENDER_FEMALE = 'female'; |
||
| 12 | const GENDER_FEMALE_2 = 'f'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $bankName; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $bankPhone; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $billingAddress; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $billingCity; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $billingCountry; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $billingFirstName; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $billingLastName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $billingPostal; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $billingRegion; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \DateTimeInterface |
||
| 61 | */ |
||
| 62 | private $birthDate; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $customerPhone; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $email; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $gender; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $shippingAddress; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $shippingCity; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | private $shippingCountry; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $shippingFirstName; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $shippingLastName; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $shippingPostal; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private $shippingRegion; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private $username; |
||
| 118 | |||
| 119 | 12 | public function getPayload() : array |
|
| 120 | { |
||
| 121 | $payload = [ |
||
| 122 | 12 | 'bank_name' => $this->bankName, |
|
| 123 | 12 | 'bank_phone' => $this->bankPhone, |
|
| 124 | 12 | 'billing_address' => $this->billingAddress, |
|
| 125 | 12 | 'billing_city' => $this->billingCity, |
|
| 126 | 12 | 'billing_country' => $this->billingCountry, |
|
| 127 | 12 | 'billing_firstname' => $this->billingFirstName, |
|
| 128 | 12 | 'billing_lastname' => $this->billingLastName, |
|
| 129 | 12 | 'billing_postal' => $this->billingPostal, |
|
| 130 | 12 | 'billing_region' => $this->billingRegion, |
|
| 131 | 12 | 'birthdate' => $this->birthDate ? $this->birthDate->format('Y-m-d') : null, |
|
| 132 | 12 | 'customer_phone' => $this->customerPhone, |
|
| 133 | 12 | 'email' => $this->email, |
|
| 134 | 12 | 'gender' => $this->gender, |
|
| 135 | 12 | 'shipping_address' => $this->shippingAddress, |
|
| 136 | 12 | 'shipping_city' => $this->shippingCity, |
|
| 137 | 12 | 'shipping_country' => $this->shippingCountry, |
|
| 138 | 12 | 'shipping_firstname' => $this->shippingFirstName, |
|
| 139 | 12 | 'shipping_lastname' => $this->shippingLastName, |
|
| 140 | 12 | 'shipping_postal' => $this->shippingPostal, |
|
| 141 | 12 | 'shipping_region' => $this->shippingRegion, |
|
| 142 | 12 | 'username' => $this->username, |
|
| 143 | ]; |
||
| 144 | |||
| 145 | 12 | $this->validate(); |
|
| 146 | |||
| 147 | 12 | return static::simplePayload($payload); |
|
| 148 | } |
||
| 149 | |||
| 150 | 12 | public function validate() |
|
| 151 | { |
||
| 152 | 12 | Assert::thatNullOr($this->bankName)->string(); |
|
| 153 | 12 | Assert::thatNullOr($this->bankPhone)->string(); |
|
| 154 | 12 | Assert::thatNullOr($this->billingAddress)->string(); |
|
| 155 | 12 | Assert::thatNullOr($this->billingCity)->string(); |
|
| 156 | 12 | Assert::thatNullOr($this->billingCountry)->string(); |
|
| 157 | 12 | Assert::thatNullOr($this->billingFirstName)->string(); |
|
| 158 | 12 | Assert::thatNullOr($this->billingLastName)->string(); |
|
| 159 | 12 | Assert::thatNullOr($this->billingPostal)->string(); |
|
| 160 | 12 | Assert::thatNullOr($this->billingRegion)->string(); |
|
| 161 | 12 | Assert::thatNullOr($this->birthDate)->isInstanceOf(\DateTimeInterface::class); |
|
| 162 | 12 | Assert::thatNullOr($this->customerPhone)->string(); |
|
| 163 | 12 | Assert::thatNullOr($this->email)->email(); |
|
| 164 | 12 | Assert::thatNullOr($this->gender)->inArray(static::getGenders()); |
|
| 165 | 12 | Assert::thatNullOr($this->shippingAddress)->string(); |
|
| 166 | 12 | Assert::thatNullOr($this->shippingCity)->string(); |
|
| 167 | 12 | Assert::thatNullOr($this->shippingCountry)->string(); |
|
| 168 | 12 | Assert::thatNullOr($this->shippingFirstName)->string(); |
|
| 169 | 12 | Assert::thatNullOr($this->shippingLastName)->string(); |
|
| 170 | 12 | Assert::thatNullOr($this->shippingPostal)->string(); |
|
| 171 | 12 | Assert::thatNullOr($this->shippingRegion)->string(); |
|
| 172 | 12 | Assert::thatNullOr($this->username)->string(); |
|
| 173 | 12 | } |
|
| 174 | |||
| 175 | 15 | public static function getGenders() : array |
|
| 176 | { |
||
| 177 | return [ |
||
| 178 | 15 | self::GENDER_MALE, |
|
| 179 | 15 | self::GENDER_MALE_2, |
|
| 180 | 15 | self::GENDER_FEMALE, |
|
| 181 | 15 | self::GENDER_FEMALE_2, |
|
| 182 | ]; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 3 | public function getBankName() : ?string |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $bankName |
||
| 195 | * @return CustomerInfo |
||
| 196 | */ |
||
| 197 | 6 | public function setBankName(string $bankName) : self |
|
| 198 | { |
||
| 199 | 6 | $this->bankName = $bankName; |
|
| 200 | 6 | return $this; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 3 | public function getBankPhone() : ?string |
|
| 207 | { |
||
| 208 | 3 | return $this->bankPhone; |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $bankPhone |
||
| 213 | * @return CustomerInfo |
||
| 214 | */ |
||
| 215 | 6 | public function setBankPhone(string $bankPhone) : self |
|
| 216 | { |
||
| 217 | 6 | $this->bankPhone = $bankPhone; |
|
| 218 | 6 | return $this; |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | 3 | public function getBillingAddress() : ?string |
|
| 225 | { |
||
| 226 | 3 | return $this->billingAddress; |
|
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $billingAddress |
||
| 231 | * @return CustomerInfo |
||
| 232 | */ |
||
| 233 | 6 | public function setBillingAddress(string $billingAddress) : self |
|
| 234 | { |
||
| 235 | 6 | $this->billingAddress = $billingAddress; |
|
| 236 | 6 | return $this; |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 3 | public function getBillingCity() : ?string |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $billingCity |
||
| 249 | * @return CustomerInfo |
||
| 250 | */ |
||
| 251 | 6 | public function setBillingCity(string $billingCity) : self |
|
| 252 | { |
||
| 253 | 6 | $this->billingCity = $billingCity; |
|
| 254 | 6 | return $this; |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 3 | public function getBillingCountry() : ?string |
|
| 261 | { |
||
| 262 | 3 | return $this->billingCountry; |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $billingCountry |
||
| 267 | * @return CustomerInfo |
||
| 268 | */ |
||
| 269 | 6 | public function setBillingCountry(string $billingCountry) : self |
|
| 270 | { |
||
| 271 | 6 | $this->billingCountry = $billingCountry; |
|
| 272 | 6 | return $this; |
|
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 3 | public function getBillingFirstName() : ?string |
|
| 279 | { |
||
| 280 | 3 | return $this->billingFirstName; |
|
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $billingFirstName |
||
| 285 | * @return CustomerInfo |
||
| 286 | */ |
||
| 287 | 12 | public function setBillingFirstName(string $billingFirstName) : self |
|
| 288 | { |
||
| 289 | 12 | $this->billingFirstName = $billingFirstName; |
|
| 290 | 12 | return $this; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 3 | public function getBillingLastName() : ?string |
|
| 297 | { |
||
| 298 | 3 | return $this->billingLastName; |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $billingLastName |
||
| 303 | * @return CustomerInfo |
||
| 304 | */ |
||
| 305 | 12 | public function setBillingLastName(string $billingLastName) : self |
|
| 306 | { |
||
| 307 | 12 | $this->billingLastName = $billingLastName; |
|
| 308 | 12 | return $this; |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 3 | public function getBillingPostal() : ?string |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $billingPostal |
||
| 321 | * @return CustomerInfo |
||
| 322 | */ |
||
| 323 | 6 | public function setBillingPostal(string $billingPostal) : self |
|
| 324 | { |
||
| 325 | 6 | $this->billingPostal = $billingPostal; |
|
| 326 | 6 | return $this; |
|
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 3 | public function getBillingRegion() : ?string |
|
| 333 | { |
||
| 334 | 3 | return $this->billingRegion; |
|
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $billingRegion |
||
| 339 | * @return CustomerInfo |
||
| 340 | */ |
||
| 341 | 6 | public function setBillingRegion(string $billingRegion) : self |
|
| 342 | { |
||
| 343 | 6 | $this->billingRegion = $billingRegion; |
|
| 344 | 6 | return $this; |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return \DateTimeInterface |
||
| 349 | */ |
||
| 350 | 3 | public function getBirthDate() |
|
| 351 | { |
||
| 352 | 3 | return $this->birthDate; |
|
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param \DateTimeInterface $birthDate |
||
| 357 | * @return CustomerInfo |
||
| 358 | */ |
||
| 359 | 6 | public function setBirthDate(\DateTimeInterface $birthDate) : self |
|
| 360 | { |
||
| 361 | 6 | $this->birthDate = $birthDate; |
|
| 362 | 6 | return $this; |
|
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 3 | public function getCustomerPhone() : ?string |
|
| 369 | { |
||
| 370 | 3 | return $this->customerPhone; |
|
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $customerPhone |
||
| 375 | * @return CustomerInfo |
||
| 376 | */ |
||
| 377 | 6 | public function setCustomerPhone(string $customerPhone) : self |
|
| 378 | { |
||
| 379 | 6 | $this->customerPhone = $customerPhone; |
|
| 380 | 6 | return $this; |
|
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | 3 | public function getEmail() : ?string |
|
| 387 | { |
||
| 388 | 3 | return $this->email; |
|
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $email |
||
| 393 | * @return CustomerInfo |
||
| 394 | */ |
||
| 395 | 6 | public function setEmail(string $email) : self |
|
| 396 | { |
||
| 397 | 6 | $this->email = $email; |
|
| 398 | 6 | return $this; |
|
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | 3 | public function getGender() : ?string |
|
| 405 | { |
||
| 406 | 3 | return $this->gender; |
|
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param string $gender |
||
| 411 | * @return CustomerInfo |
||
| 412 | */ |
||
| 413 | 6 | public function setGender(string $gender) : self |
|
| 414 | { |
||
| 415 | 6 | Assert::that($gender)->nullOr()->inArray(static::getGenders()); |
|
| 416 | 6 | $this->gender = $gender; |
|
| 417 | 6 | return $this; |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 3 | public function getShippingAddress() : ?string |
|
| 424 | { |
||
| 425 | 3 | return $this->shippingAddress; |
|
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $shippingAddress |
||
| 430 | * @return CustomerInfo |
||
| 431 | */ |
||
| 432 | 6 | public function setShippingAddress(string $shippingAddress) : self |
|
| 433 | { |
||
| 434 | 6 | $this->shippingAddress = $shippingAddress; |
|
| 435 | 6 | return $this; |
|
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 3 | public function getShippingCity() : ?string |
|
| 442 | { |
||
| 443 | 3 | return $this->shippingCity; |
|
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param string $shippingCity |
||
| 448 | * @return CustomerInfo |
||
| 449 | */ |
||
| 450 | 6 | public function setShippingCity(string $shippingCity) : self |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | 3 | public function getShippingCountry() : ?string |
|
| 460 | { |
||
| 461 | 3 | return $this->shippingCountry; |
|
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $shippingCountry |
||
| 466 | * @return CustomerInfo |
||
| 467 | */ |
||
| 468 | 6 | public function setShippingCountry(string $shippingCountry) : self |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | 3 | public function getShippingFirstName() : ?string |
|
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $shippingFirstName |
||
| 484 | * @return CustomerInfo |
||
| 485 | */ |
||
| 486 | 6 | public function setShippingFirstName(string $shippingFirstName) : self |
|
| 487 | { |
||
| 488 | 6 | $this->shippingFirstName = $shippingFirstName; |
|
| 489 | 6 | return $this; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | 3 | public function getShippingLastName() : ?string |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $shippingLastName |
||
| 502 | * @return CustomerInfo |
||
| 503 | */ |
||
| 504 | 6 | public function setShippingLastName(string $shippingLastName) : self |
|
| 509 | |||
| 510 | /** |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | 3 | public function getShippingPostal() : ?string |
|
| 514 | { |
||
| 515 | 3 | return $this->shippingPostal; |
|
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param string $shippingPostal |
||
| 520 | * @return CustomerInfo |
||
| 521 | */ |
||
| 522 | 6 | public function setShippingPostal(string $shippingPostal) : self |
|
| 523 | { |
||
| 524 | 6 | $this->shippingPostal = $shippingPostal; |
|
| 525 | 6 | return $this; |
|
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | 3 | public function getShippingRegion() : ?string |
|
| 532 | { |
||
| 533 | 3 | return $this->shippingRegion; |
|
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $shippingRegion |
||
| 538 | * @return CustomerInfo |
||
| 539 | */ |
||
| 540 | 6 | public function setShippingRegion(string $shippingRegion) : self |
|
| 541 | { |
||
| 542 | 6 | $this->shippingRegion = $shippingRegion; |
|
| 543 | 6 | return $this; |
|
| 545 | |||
| 546 | /** |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | 3 | public function getUsername() : ?string |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @param string $username |
||
| 556 | * @return CustomerInfo |
||
| 557 | */ |
||
| 558 | 6 | public function setUsername(string $username) : self |
|
| 563 | } |
||
| 564 |