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() |
|
| 122 | |||
| 123 | // Validations |
||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | 47 | public function validateFirstnameOrLastname() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | 46 | public function validatePhoneCellphoneOrEmail() |
|
| 145 | |||
| 146 | 40 | public function validateContactTypeId() |
|
| 152 | |||
| 153 | 28 | public function validateBusinessTypeId() |
|
| 159 | |||
| 160 | 16 | public function validateSuboriginId() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set |
||
| 169 | * |
||
| 170 | * @param $key |
||
| 171 | * @param string $value |
||
| 172 | */ |
||
| 173 | 26 | private function set($key, $value = "") |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | 48 | public function getFirstname() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param $firstname |
||
| 191 | * |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | 17 | public function setFirstname($firstname) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 26 | public function getLastname() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param $lastname |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 13 | public function setLastname($lastname) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | 47 | public function getPhone() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param $phone |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | 11 | public function setPhone($phone) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | 35 | public function getCellphone() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param $cellphone |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 7 | public function setCellphone($cellphone) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return mixed |
||
| 259 | */ |
||
| 260 | 23 | public function getEmail() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param $email |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | 7 | public function setEmail($email) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | 41 | public function getContactTypeId() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param $contact_type_id |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | 19 | public function setContactTypeId($contact_type_id) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @return mixed |
||
| 297 | */ |
||
| 298 | 29 | public function getBusinessTypeId() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param $business_type_id |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | 13 | public function setBusinessTypeId($business_type_id) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | 5 | public function getNotes() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param $notes |
||
| 324 | * |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | 1 | public function setNotes($notes) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return mixed |
||
| 335 | */ |
||
| 336 | 5 | public function getOriginId() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param $origin_id |
||
| 343 | * |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | 1 | public function setOriginId($origin_id) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | 17 | public function getSuboriginId() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param $suborigin_id |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | 7 | public function setSuboriginId($suborigin_id) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | 5 | public function getAssignedUser() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @param $assigned_user |
||
| 381 | * |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | 1 | public function setAssignedUser($assigned_user) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @return mixed |
||
| 392 | */ |
||
| 393 | 5 | public function getCarBrand() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param $car_brand |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | 1 | public function setCarBrand($car_brand) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | 5 | public function getCarModelo() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param $car_modelo |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 1 | public function setCarModelo($car_modelo) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | 5 | public function getCity() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @param $city |
||
| 438 | * |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | 1 | public function setCity($city) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @return mixed |
||
| 449 | */ |
||
| 450 | 5 | public function getProvince() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param $province |
||
| 457 | * |
||
| 458 | * @return $this |
||
| 459 | */ |
||
| 460 | 1 | public function setProvince($province) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @return mixed |
||
| 468 | */ |
||
| 469 | 5 | public function getCountry() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @param $country |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | 1 | public function setCountry($country) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @return mixed |
||
| 487 | */ |
||
| 488 | 5 | public function getVendorName() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * @param $vendor_name |
||
| 495 | * |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | 1 | public function setVendorName($vendor_name) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @return mixed |
||
| 506 | */ |
||
| 507 | 5 | public function getVendorEmail() |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param $vendor_email |
||
| 514 | * |
||
| 515 | * @return $this |
||
| 516 | */ |
||
| 517 | 1 | public function setVendorEmail($vendor_email) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @return mixed |
||
| 525 | */ |
||
| 526 | 5 | public function getVendorPhone() |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param $vendor_phone |
||
| 533 | * |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | 1 | public function setVendorPhone($vendor_phone) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * @return mixed |
||
| 544 | */ |
||
| 545 | 5 | public function getProviderService() |
|
| 549 | |||
| 550 | /** |
||
| 551 | * @param $provider_service |
||
| 552 | * |
||
| 553 | * @return $this |
||
| 554 | */ |
||
| 555 | 1 | public function setProviderService($provider_service) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @return mixed |
||
| 563 | */ |
||
| 564 | 5 | public function getProviderUrl() |
|
| 568 | |||
| 569 | /** |
||
| 570 | * @param $provider_url |
||
| 571 | * |
||
| 572 | * @return $this |
||
| 573 | */ |
||
| 574 | 1 | public function setProviderUrl($provider_url) |
|
| 579 | } |
||
| 580 |