Complex classes like Lead 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 Lead, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Lead extends Model |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var integer |
||
| 16 | */ |
||
| 17 | protected $id; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var integer |
||
| 21 | */ |
||
| 22 | protected $contactId; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var integer |
||
| 31 | */ |
||
| 32 | protected $funnelId; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $source; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $campaign; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $keyword; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var View |
||
| 51 | */ |
||
| 52 | protected $view; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | protected $ownerId; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \DateTime |
||
| 61 | */ |
||
| 62 | protected $createdAt; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Payment[] |
||
| 66 | */ |
||
| 67 | protected $payments = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Custom[] |
||
| 71 | */ |
||
| 72 | protected $customs = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $options = []; |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Lead constructor. |
||
| 82 | * |
||
| 83 | * @param array $leadData |
||
| 84 | */ |
||
| 85 | public function __construct(array $leadData = []) |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function toArray() |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @return bool |
||
| 190 | * @throws LPTrackerSDKException |
||
| 191 | */ |
||
| 192 | public function validate() |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | public function getId() |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function getContactId() |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getName() |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $name |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function setName($name) |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @return int |
||
| 256 | */ |
||
| 257 | public function getFunnelId() |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param int $funnelId |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function setFunnelId($funnelId) |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function getSource() |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $source |
||
| 287 | * |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function setSource($source) |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function getCampaign() |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $campaign |
||
| 309 | */ |
||
| 310 | public function setCampaign($campaign) |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public function getKeyword() |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $keyword |
||
| 327 | * |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function setKeyword($keyword) |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * @return View |
||
| 340 | */ |
||
| 341 | public function getView() |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * @return Payment[] |
||
| 349 | */ |
||
| 350 | public function getPayments() |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param array $payments |
||
| 358 | * |
||
| 359 | * @return $this |
||
| 360 | */ |
||
| 361 | public function setPayments(array $payments) |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param Payment $payment |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | public function addPayment(Payment $payment) |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * @return int |
||
| 384 | */ |
||
| 385 | public function getOwnerId() |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * @param int $ownerId |
||
| 393 | * |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | public function setOwnerId($ownerId) |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * @return Custom[] |
||
| 406 | */ |
||
| 407 | public function getCustoms() |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * @param Custom[] $customs |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function setCustoms(array $customs) |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * @param Custom $custom |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | */ |
||
| 431 | public function addCustom(Custom $custom) |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * @return \DateTime |
||
| 441 | */ |
||
| 442 | public function getCreatedAt() |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * @param \DateTime $createdAt |
||
| 450 | * |
||
| 451 | * @return $this |
||
| 452 | */ |
||
| 453 | public function setCreatedAt($createdAt) |
||
| 459 | } |