Complex classes like LeadData 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 LeadData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class LeadData |
||
| 8 | { |
||
| 9 | private $firstname; |
||
| 10 | private $lastname; |
||
| 11 | private $phone; |
||
| 12 | private $cellphone; |
||
| 13 | private $email; |
||
| 14 | private $contact_type_id; |
||
| 15 | private $business_type_id; |
||
| 16 | private $notes; |
||
| 17 | private $origin_id; |
||
| 18 | private $suborigin_id; |
||
| 19 | private $assigned_user; |
||
| 20 | private $car_brand; |
||
| 21 | private $car_modelo; |
||
| 22 | private $city; |
||
| 23 | private $province; |
||
| 24 | private $country; |
||
| 25 | private $vendor_name; |
||
| 26 | private $vendor_email; |
||
| 27 | private $vendor_phone; |
||
| 28 | private $provider_service; |
||
| 29 | private $provider_url; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * LeadData constructor. |
||
| 33 | * |
||
| 34 | * @param array $data |
||
| 35 | */ |
||
| 36 | 27 | public function __construct($data = []) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Object to array |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | 25 | public function toArray() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Validate |
||
| 81 | * |
||
| 82 | * @throws InvalidArgumentException |
||
| 83 | */ |
||
| 84 | 47 | private function validate() |
|
| 106 | |||
| 107 | // Validations |
||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | 47 | public function validateFirstnameOrLastname() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | 46 | public function validatePhoneCellphoneOrEmail() |
|
| 129 | |||
| 130 | 40 | public function validateContactTypeId() |
|
| 136 | |||
| 137 | 28 | public function validateBusinessTypeId() |
|
| 143 | |||
| 144 | 16 | public function validateSuboriginId() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Set |
||
| 153 | * |
||
| 154 | * @param $key |
||
| 155 | * @param string $value |
||
| 156 | */ |
||
| 157 | 26 | private function set($key, $value = "") |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | 48 | public function getFirstname() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param $firstname |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | 17 | public function setFirstname($firstname) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | 26 | public function getLastname() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param $lastname |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | 13 | public function setLastname($lastname) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | 47 | public function getPhone() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param $phone |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 11 | public function setPhone($phone) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | 35 | public function getCellphone() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param $cellphone |
||
| 232 | * |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 7 | public function setCellphone($cellphone) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @return mixed |
||
| 243 | */ |
||
| 244 | 23 | public function getEmail() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param $email |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | 7 | public function setEmail($email) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | 41 | public function getContactTypeId() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param $contact_type_id |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | 19 | public function setContactTypeId($contact_type_id) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @return mixed |
||
| 281 | */ |
||
| 282 | 29 | public function getBusinessTypeId() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param $business_type_id |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | 13 | public function setBusinessTypeId($business_type_id) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return mixed |
||
| 300 | */ |
||
| 301 | 5 | public function getNotes() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param $notes |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | 1 | public function setNotes($notes) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return mixed |
||
| 319 | */ |
||
| 320 | 5 | public function getOriginId() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param $origin_id |
||
| 327 | * |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | 1 | public function setOriginId($origin_id) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | 17 | public function getSuboriginId() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param $suborigin_id |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | 7 | public function setSuboriginId($suborigin_id) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | 5 | public function getAssignedUser() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param $assigned_user |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | 1 | public function setAssignedUser($assigned_user) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @return mixed |
||
| 376 | */ |
||
| 377 | 5 | public function getCarBrand() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param $car_brand |
||
| 384 | * |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | 1 | public function setCarBrand($car_brand) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @return mixed |
||
| 395 | */ |
||
| 396 | 5 | public function getCarModelo() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param $car_modelo |
||
| 403 | * |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | 1 | public function setCarModelo($car_modelo) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @return mixed |
||
| 414 | */ |
||
| 415 | 5 | public function getCity() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @param $city |
||
| 422 | * |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | 1 | public function setCity($city) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | 5 | public function getProvince() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @param $province |
||
| 441 | * |
||
| 442 | * @return $this |
||
| 443 | */ |
||
| 444 | 1 | public function setProvince($province) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @return mixed |
||
| 452 | */ |
||
| 453 | 5 | public function getCountry() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @param $country |
||
| 460 | * |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | 1 | public function setCountry($country) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * @return mixed |
||
| 471 | */ |
||
| 472 | 5 | public function getVendorName() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * @param $vendor_name |
||
| 479 | * |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | 1 | public function setVendorName($vendor_name) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @return mixed |
||
| 490 | */ |
||
| 491 | 5 | public function getVendorEmail() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @param $vendor_email |
||
| 498 | * |
||
| 499 | * @return $this |
||
| 500 | */ |
||
| 501 | 1 | public function setVendorEmail($vendor_email) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * @return mixed |
||
| 509 | */ |
||
| 510 | 5 | public function getVendorPhone() |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @param $vendor_phone |
||
| 517 | * |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | 1 | public function setVendorPhone($vendor_phone) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @return mixed |
||
| 528 | */ |
||
| 529 | 5 | public function getProviderService() |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param $provider_service |
||
| 536 | * |
||
| 537 | * @return $this |
||
| 538 | */ |
||
| 539 | 1 | public function setProviderService($provider_service) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * @return mixed |
||
| 547 | */ |
||
| 548 | 5 | public function getProviderUrl() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * @param $provider_url |
||
| 555 | * |
||
| 556 | * @return $this |
||
| 557 | */ |
||
| 558 | 1 | public function setProviderUrl($provider_url) |
|
| 563 | } |
||
| 564 |