Complex classes like Customer 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 Customer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Customer |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The customer ID. |
||
| 14 | * |
||
| 15 | * @var integer |
||
| 16 | * |
||
| 17 | * @JMS\Type("integer") |
||
| 18 | * @JMS\SerializedName("customer_id") |
||
| 19 | */ |
||
| 20 | protected $customerId; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The external customer ID. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | * |
||
| 27 | * @JMS\Type("string") |
||
| 28 | * @JMS\SerializedName("customer_ext_uid") |
||
| 29 | */ |
||
| 30 | protected $customerExternalId; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Hash. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | * |
||
| 37 | * @JMS\Type("string") |
||
| 38 | * @JMS\SerializedName("hash") |
||
| 39 | */ |
||
| 40 | protected $hash; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The own customer number. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | * |
||
| 47 | * @JMS\Type("string") |
||
| 48 | * @JMS\SerializedName("customer_number") |
||
| 49 | */ |
||
| 50 | protected $customerNumber; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The company name of the customer. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | * |
||
| 57 | * @JMS\Type("string") |
||
| 58 | * @JMS\SerializedName("companyname") |
||
| 59 | */ |
||
| 60 | protected $companyName; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The VAT identification number of the customer. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | * |
||
| 67 | * @JMS\Type("string") |
||
| 68 | * @JMS\SerializedName("vat_id") |
||
| 69 | */ |
||
| 70 | protected $vatId; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Tax ID of the customer. |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | * |
||
| 77 | * @JMS\Type("string") |
||
| 78 | * @JMS\SerializedName("tax_id") |
||
| 79 | */ |
||
| 80 | protected $taxId; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The academic title of the customer. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | * |
||
| 87 | * @JMS\Type("string") |
||
| 88 | * @JMS\SerializedName("title") |
||
| 89 | */ |
||
| 90 | protected $title; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The salutation of the customer. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | * |
||
| 97 | * @JMS\Type("string") |
||
| 98 | * @JMS\SerializedName("salutation") |
||
| 99 | */ |
||
| 100 | protected $salutation; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The first name of the customer. |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | * |
||
| 107 | * @JMS\Type("string") |
||
| 108 | * @JMS\SerializedName("firstname") |
||
| 109 | */ |
||
| 110 | protected $firstName; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The last name of the customer. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | * |
||
| 117 | * @JMS\Type("string") |
||
| 118 | * @JMS\SerializedName("lastname") |
||
| 119 | */ |
||
| 120 | protected $lastName; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The address of the customer. |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | * |
||
| 127 | * @JMS\Type("string") |
||
| 128 | * @JMS\SerializedName("address") |
||
| 129 | */ |
||
| 130 | protected $address; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The second row of an address of the customer. |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | * |
||
| 137 | * @JMS\Type("string") |
||
| 138 | * @JMS\SerializedName("address_2") |
||
| 139 | */ |
||
| 140 | protected $address2; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The zip code of the customer. |
||
| 144 | * |
||
| 145 | * @var string |
||
| 146 | * |
||
| 147 | * @JMS\Type("string") |
||
| 148 | * @JMS\SerializedName("zipcode") |
||
| 149 | */ |
||
| 150 | protected $zipCode; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * The city of the customer. |
||
| 154 | * |
||
| 155 | * @var string |
||
| 156 | * |
||
| 157 | * @JMS\Type("string") |
||
| 158 | * @JMS\SerializedName("city") |
||
| 159 | */ |
||
| 160 | protected $city; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * The country code of the customer. |
||
| 164 | * |
||
| 165 | * @var string |
||
| 166 | * |
||
| 167 | * @JMS\Type("string") |
||
| 168 | * @JMS\SerializedName("country_code") |
||
| 169 | */ |
||
| 170 | protected $countryCode; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * The language code of the customer. |
||
| 174 | * |
||
| 175 | * @var string |
||
| 176 | * |
||
| 177 | * @JMS\Type("string") |
||
| 178 | * @JMS\SerializedName("language_code") |
||
| 179 | */ |
||
| 180 | protected $languageCode; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * The email address of the customer. |
||
| 184 | * |
||
| 185 | * @var string |
||
| 186 | * |
||
| 187 | * @JMS\Type("string") |
||
| 188 | * @JMS\SerializedName("email") |
||
| 189 | */ |
||
| 190 | protected $email; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * The phone number of the customer. |
||
| 194 | * |
||
| 195 | * @var string |
||
| 196 | * @JMS\Type("string") |
||
| 197 | * @JMS\SerializedName("phone") |
||
| 198 | */ |
||
| 199 | protected $phone; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The payment data URL. |
||
| 203 | * |
||
| 204 | * @var string |
||
| 205 | * |
||
| 206 | * @JMS\Type("string") |
||
| 207 | * @JMS\SerializedName("payment_data_url") |
||
| 208 | */ |
||
| 209 | protected $paymentDataUrl; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The dashboard URL. |
||
| 213 | * |
||
| 214 | * @var string |
||
| 215 | * |
||
| 216 | * @JMS\Type("string") |
||
| 217 | * @JMS\SerializedName("dashboard_url") |
||
| 218 | */ |
||
| 219 | protected $dashboardUrl; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | public function getCustomerId() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param int $customerId |
||
| 231 | * @return Customer |
||
| 232 | */ |
||
| 233 | public function setCustomerId($customerId) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the customer external ID. |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | public function getCustomerExternalId() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set the customer external ID. |
||
| 252 | * |
||
| 253 | * @param int $customerExternalId The customer external ID. |
||
| 254 | * @return Customer |
||
| 255 | */ |
||
| 256 | public function setCustomerExternalId($customerExternalId) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the hash. |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getHash() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set the hash. |
||
| 275 | * |
||
| 276 | * @param string $hash The hash. |
||
| 277 | * @return Customer |
||
| 278 | */ |
||
| 279 | public function setHash($hash) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the customer number. |
||
| 288 | * |
||
| 289 | * @return int The customer number. |
||
| 290 | */ |
||
| 291 | public function getCustomerNumber() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set the customer number. |
||
| 298 | * |
||
| 299 | * @param int $customerNumber The customer number. |
||
| 300 | * @return Customer |
||
| 301 | */ |
||
| 302 | public function setCustomerNumber($customerNumber) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the company name. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getCompanyName() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set the company name. |
||
| 321 | * |
||
| 322 | * @param string $companyName The company name. |
||
| 323 | * @return Customer |
||
| 324 | */ |
||
| 325 | public function setCompanyName($companyName) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get the VAT ID. |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function getVatId() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set the VAT ID. |
||
| 344 | * |
||
| 345 | * @param string $vatId The VAT ID. |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function setVatId($vatId) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get the tax ID. |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getTaxId() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set the tax ID. |
||
| 367 | * |
||
| 368 | * @param string $taxId The tax ID. |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function setTaxId($taxId) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the title. |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getTitle() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Set the title. |
||
| 390 | * |
||
| 391 | * @param string $title The title. |
||
| 392 | * @return Customer |
||
| 393 | */ |
||
| 394 | public function setTitle($title) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the salutation. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getSalutation() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set the salutation. |
||
| 413 | * |
||
| 414 | * @param string $salutation The salutation. |
||
| 415 | * @return Customer |
||
| 416 | */ |
||
| 417 | public function setSalutation($salutation) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the first name. |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getFirstName() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set the first name. |
||
| 436 | * |
||
| 437 | * @param string $firstName The first name. |
||
| 438 | * @return Customer |
||
| 439 | */ |
||
| 440 | public function setFirstName($firstName) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the last name. |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function getLastName() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Set the last name. |
||
| 459 | * |
||
| 460 | * @param string $lastName The last name. |
||
| 461 | * @return Customer |
||
| 462 | */ |
||
| 463 | public function setLastName($lastName) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get the address. |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function getAddress() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Set the address. |
||
| 482 | * |
||
| 483 | * @param string $address The address. |
||
| 484 | * @return Customer |
||
| 485 | */ |
||
| 486 | public function setAddress($address) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get the address2. |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function getAddress2() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set the address2. |
||
| 505 | * |
||
| 506 | * @param string $address2 The address2. |
||
| 507 | * @return Customer |
||
| 508 | */ |
||
| 509 | public function setAddress2($address2) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get the zip code. |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public function getZipCode() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Set the zip code. |
||
| 528 | * |
||
| 529 | * @param string $zipCode The zip code. |
||
| 530 | * @return Customer |
||
| 531 | */ |
||
| 532 | public function setZipCode($zipCode) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Get the city. |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function getCity() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Set the city. |
||
| 551 | * |
||
| 552 | * @param string $city The city. |
||
| 553 | * @return Customer |
||
| 554 | */ |
||
| 555 | public function setCity($city) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get the country code. |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function getCountryCode() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Set the country code. |
||
| 574 | * |
||
| 575 | * @param string $countryCode The country code. |
||
| 576 | * @return Customer |
||
| 577 | */ |
||
| 578 | public function setCountryCode($countryCode) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Get the language code. |
||
| 587 | * |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function getLanguageCode() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Set the language code. |
||
| 597 | * |
||
| 598 | * @param string $languageCode The language code. |
||
| 599 | * @return $this |
||
| 600 | */ |
||
| 601 | public function setLanguageCode($languageCode) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Get the email. |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function getEmail() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Set the email. |
||
| 620 | * |
||
| 621 | * @param string $email The email. |
||
| 622 | * @return Customer |
||
| 623 | */ |
||
| 624 | public function setEmail($email) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get the phone number of the customer. |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function getPhone() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Set the phone number of the customer. |
||
| 643 | * |
||
| 644 | * @param string $phone The phone number to set. |
||
| 645 | * @return $this |
||
| 646 | */ |
||
| 647 | public function setPhone($phone) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get the payment data URL. |
||
| 656 | * |
||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | public function getPaymentDataUrl() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Set the payment data URL. |
||
| 666 | * |
||
| 667 | * @param string $paymentDataUrl The payment data URL. |
||
| 668 | * @return Customer |
||
| 669 | */ |
||
| 670 | public function setPaymentDataUrl($paymentDataUrl) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Get the dashboard URL. |
||
| 679 | * |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | public function getDashboardUrl() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Set the dashboard URL. |
||
| 689 | * |
||
| 690 | * @param string $dashboardUrl The dashboard URL. |
||
| 691 | * @return Customer |
||
| 692 | */ |
||
| 693 | public function setDashboardUrl($dashboardUrl) |
||
| 699 | } |
||
| 700 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.