Complex classes like Investment 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 Investment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Investment extends \WebCMS\Entity\Entity |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @ORM\Column(type="string", length=255) |
||
| 21 | */ |
||
| 22 | private $phone; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @ORM\Column(type="string", length=255) |
||
| 26 | */ |
||
| 27 | private $email; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 31 | */ |
||
| 32 | private $birthdateNumber; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 36 | */ |
||
| 37 | private $company; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 41 | */ |
||
| 42 | private $registrationNumber; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 46 | */ |
||
| 47 | private $bankAccount; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @ORM\Column(type="decimal", scale=2) |
||
| 51 | */ |
||
| 52 | private $investment; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @ORM\Column(type="smallint") |
||
| 56 | */ |
||
| 57 | private $investmentLength; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\Column(type="date") |
||
| 61 | */ |
||
| 62 | private $investmentDate; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @ORM\OneToOne(targetEntity="Address") |
||
| 66 | */ |
||
| 67 | private $address; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @ORM\OneToOne(targetEntity="Address") |
||
| 71 | */ |
||
| 72 | private $postalAddress; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var datetime $created |
||
| 76 | * |
||
| 77 | * @gedmo\Timestampable(on="create") |
||
| 78 | * @ORM\Column(type="datetime") |
||
| 79 | */ |
||
| 80 | private $created; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @ORM\Column(type="text", length=255, nullable=true) |
||
| 84 | */ |
||
| 85 | private $hash; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @ORM\Column(type="text", length=255, nullable=true) |
||
| 89 | */ |
||
| 90 | private $pin; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @ORM\Column(type="boolean") |
||
| 94 | */ |
||
| 95 | private $contractSend; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @ORM\Column(type="boolean") |
||
| 99 | */ |
||
| 100 | private $contractClosed; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @ORM\Column(type="boolean") |
||
| 104 | */ |
||
| 105 | private $contractPaid; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @ORM\Column(type="boolean") |
||
| 109 | */ |
||
| 110 | private $clientContacted; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @ORM\ManyToOne(targetEntity="Businessman") |
||
| 114 | */ |
||
| 115 | private $businessman; |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the value of phone. |
||
| 120 | * |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | public function getPhone() |
||
| 124 | { |
||
| 125 | return $this->phone; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Sets the value of phone. |
||
| 130 | * |
||
| 131 | * @param mixed $phone the phone |
||
| 132 | * |
||
| 133 | * @return self |
||
| 134 | */ |
||
| 135 | public function setPhone($phone) |
||
| 136 | { |
||
| 137 | $this->phone = $phone; |
||
| 138 | |||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Gets the value of email. |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function getEmail() |
||
| 148 | { |
||
| 149 | return $this->email; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Sets the value of email. |
||
| 154 | * |
||
| 155 | * @param mixed $email the email |
||
| 156 | * |
||
| 157 | * @return self |
||
| 158 | */ |
||
| 159 | public function setEmail($email) |
||
| 160 | { |
||
| 161 | $this->email = $email; |
||
| 162 | |||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Gets the value of company. |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function getCompany() |
||
| 172 | { |
||
| 173 | return $this->company; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Sets the value of company. |
||
| 178 | * |
||
| 179 | * @param mixed $company the company |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | */ |
||
| 183 | public function setCompany($company) |
||
| 184 | { |
||
| 185 | $this->company = $company; |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the value of registrationNumber. |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function getRegistrationNumber() |
||
| 196 | { |
||
| 197 | return $this->registrationNumber; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets the value of registrationNumber. |
||
| 202 | * |
||
| 203 | * @param mixed $registrationNumber the registration number |
||
| 204 | * |
||
| 205 | * @return self |
||
| 206 | */ |
||
| 207 | public function setRegistrationNumber($registrationNumber) |
||
| 208 | { |
||
| 209 | $this->registrationNumber = $registrationNumber; |
||
| 210 | |||
| 211 | return $this; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the value of investment. |
||
| 216 | * |
||
| 217 | * @return mixed |
||
| 218 | */ |
||
| 219 | public function getInvestment() |
||
| 220 | { |
||
| 221 | return $this->investment; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Sets the value of investment. |
||
| 226 | * |
||
| 227 | * @param mixed $investment the investment |
||
| 228 | * |
||
| 229 | * @return self |
||
| 230 | */ |
||
| 231 | public function setInvestment($investment) |
||
| 232 | { |
||
| 233 | $this->investment = $investment; |
||
| 234 | |||
| 235 | return $this; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Gets the value of investmentLength. |
||
| 240 | * |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | public function getInvestmentLength() |
||
| 244 | { |
||
| 245 | return $this->investmentLength; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Sets the value of investmentLength. |
||
| 250 | * |
||
| 251 | * @param mixed $investmentLength the investment length |
||
| 252 | * |
||
| 253 | * @return self |
||
| 254 | */ |
||
| 255 | public function setInvestmentLength($investmentLength) |
||
| 256 | { |
||
| 257 | $this->investmentLength = $investmentLength; |
||
| 258 | |||
| 259 | return $this; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * |
||
| 264 | * |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | public function getRealInvestmentLength() { |
||
| 268 | $from = strtotime($this->investmentDate->format('Y-m-d')); |
||
| 269 | if ($this->investmentLength == 5) { |
||
| 270 | $to = strtotime('2020-11-30'); |
||
| 271 | } else { |
||
| 272 | $to = strtotime('2017-10-30'); |
||
| 273 | } |
||
| 274 | |||
| 275 | return ($to - $from) / 60 / 60 / 24 / 365; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Gets the value of birthdateNumber. |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | public function getBirthdateNumber() |
||
| 284 | { |
||
| 285 | return $this->birthdateNumber; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Sets the value of birthdateNumber. |
||
| 290 | * |
||
| 291 | * @param mixed $birthdateNumber the birthdate number |
||
| 292 | * |
||
| 293 | * @return self |
||
| 294 | */ |
||
| 295 | public function setBirthdateNumber($birthdateNumber) |
||
| 296 | { |
||
| 297 | $this->birthdateNumber = $birthdateNumber; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Gets the value of address. |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | public function getAddress() |
||
| 308 | { |
||
| 309 | return $this->address; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Sets the value of address. |
||
| 314 | * |
||
| 315 | * @param mixed $address the address |
||
| 316 | * |
||
| 317 | * @return self |
||
| 318 | */ |
||
| 319 | public function setAddress($address) |
||
| 320 | { |
||
| 321 | $this->address = $address; |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Gets the value of postalAddress. |
||
| 328 | * |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | public function getPostalAddress() |
||
| 332 | { |
||
| 333 | return $this->postalAddress; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sets the value of postalAddress. |
||
| 338 | * |
||
| 339 | * @param mixed $postalAddress the postal address |
||
| 340 | * |
||
| 341 | * @return self |
||
| 342 | */ |
||
| 343 | public function setPostalAddress($postalAddress) |
||
| 344 | { |
||
| 345 | $this->postalAddress = $postalAddress; |
||
| 346 | |||
| 347 | return $this; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Gets the value of created. |
||
| 352 | * |
||
| 353 | * @return datetime $created |
||
| 354 | */ |
||
| 355 | public function getCreated() |
||
| 356 | { |
||
| 357 | return $this->created; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Sets the value of created. |
||
| 362 | * |
||
| 363 | * @param datetime $created $created the created |
||
| 364 | * |
||
| 365 | * @return self |
||
| 366 | */ |
||
| 367 | public function setCreated($created) |
||
| 368 | { |
||
| 369 | $this->created = $created; |
||
| 370 | |||
| 371 | return $this; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Gets the value of hash. |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public function getHash() |
||
| 380 | { |
||
| 381 | return $this->hash = md5($this->id . $this->created->format('Y-m-d H:i:s')); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Gets the value of hash. |
||
| 386 | * |
||
| 387 | * @return mixed |
||
| 388 | */ |
||
| 389 | public function getContractHash() |
||
| 390 | { |
||
| 391 | return $this->hash = md5('contract' . $this->id . $this->created->format('Y-m-d H:i:s')); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Gets the value of bankAccount. |
||
| 396 | * |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | public function getBankAccount() |
||
| 400 | { |
||
| 401 | return $this->bankAccount; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Sets the value of bankAccount. |
||
| 406 | * |
||
| 407 | * @param mixed $bankAccount the bank account |
||
| 408 | * |
||
| 409 | * @return self |
||
| 410 | */ |
||
| 411 | public function setBankAccount($bankAccount) |
||
| 412 | { |
||
| 413 | $this->bankAccount = $bankAccount; |
||
| 414 | |||
| 415 | return $this; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Gets the value of pin. |
||
| 420 | * |
||
| 421 | * @return mixed |
||
| 422 | */ |
||
| 423 | public function getPin() |
||
| 424 | { |
||
| 425 | return $this->pin; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Sets the value of pin. |
||
| 430 | * |
||
| 431 | * @param mixed $pin the pin |
||
| 432 | * |
||
| 433 | * @return self |
||
| 434 | */ |
||
| 435 | public function setPin($pin) |
||
| 436 | { |
||
| 437 | $this->pin = $pin; |
||
| 438 | |||
| 439 | return $this; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Gets the value of contractSend. |
||
| 444 | * |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | public function getContractSend() |
||
| 448 | { |
||
| 449 | return $this->contractSend; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Sets the value of contractSend. |
||
| 454 | * |
||
| 455 | * @param mixed $contractSend the contract send |
||
| 456 | * |
||
| 457 | * @return self |
||
| 458 | */ |
||
| 459 | public function setContractSend($contractSend) |
||
| 460 | { |
||
| 461 | $this->contractSend = $contractSend; |
||
| 462 | |||
| 463 | return $this; |
||
| 464 | } |
||
| 465 | |||
| 466 | public function getContractClosed() |
||
| 467 | { |
||
| 468 | return $this->contractClosed; |
||
| 469 | } |
||
| 470 | |||
| 471 | public function setContractClosed($contractClosed) |
||
| 472 | { |
||
| 473 | $this->contractClosed = $contractClosed; |
||
| 474 | return $this; |
||
| 475 | } |
||
| 476 | |||
| 477 | public function getContractPaid() |
||
| 481 | |||
| 482 | public function setContractPaid($contractPaid) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Gets the value of clientContacted. |
||
| 490 | * |
||
| 491 | * @return mixed |
||
| 492 | */ |
||
| 493 | public function getClientContacted() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Sets the value of clientContacted. |
||
| 500 | * |
||
| 501 | * @param mixed $clientContacted the client contacted |
||
| 502 | * |
||
| 503 | * @return self |
||
| 504 | */ |
||
| 505 | public function setClientContacted($clientContacted) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Gets the value of investmentDate. |
||
| 513 | * |
||
| 514 | * @return mixed |
||
| 515 | */ |
||
| 516 | public function getInvestmentDate() |
||
| 517 | { |
||
| 518 | return $this->investmentDate; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Sets the value of investmentDate. |
||
| 523 | * |
||
| 524 | * @param mixed $investmentDate the investment date |
||
| 525 | * |
||
| 526 | * @return self |
||
| 527 | */ |
||
| 528 | public function setInvestmentDate($investmentDate) |
||
| 529 | { |
||
| 530 | $this->investmentDate = $investmentDate; |
||
| 531 | |||
| 532 | return $this; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Gets the value of businessman. |
||
| 537 | * |
||
| 538 | * @return mixed |
||
| 539 | */ |
||
| 540 | public function getBusinessman() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Sets the value of businessman. |
||
| 547 | * |
||
| 548 | * @param mixed $businessman the businessman |
||
| 549 | * |
||
| 550 | * @return self |
||
| 551 | */ |
||
| 552 | public function setBusinessman($businessman) |
||
| 557 | } |
||
| 558 |