Complex classes like Donation 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 Donation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Donation { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | * |
||
| 17 | * @ORM\Column(name="name", type="string", length=250, nullable=true) |
||
| 18 | */ |
||
| 19 | private $name; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | * |
||
| 24 | * @ORM\Column(name="ort", type="string", length=250, nullable=true) |
||
| 25 | */ |
||
| 26 | private $city; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | * |
||
| 31 | * @ORM\Column(name="email", type="string", length=250, nullable=true) |
||
| 32 | */ |
||
| 33 | private $email; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var boolean |
||
| 37 | * |
||
| 38 | * @ORM\Column(name="info", type="boolean", options={"default":0}, nullable=false) |
||
| 39 | */ |
||
| 40 | private $info = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var boolean |
||
| 44 | * |
||
| 45 | * @ORM\Column(name="bescheinigung", type="boolean", nullable=true) |
||
| 46 | */ |
||
| 47 | private $donationReceipt; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | * |
||
| 52 | * @ORM\Column(name="eintrag", type="string", length=250, options={"default":""}, nullable=false) |
||
| 53 | */ |
||
| 54 | private $publicRecord = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | * |
||
| 59 | * @ORM\Column(name="betrag", type="string", length=250, nullable=true) |
||
| 60 | */ |
||
| 61 | private $amount; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var integer |
||
| 65 | * |
||
| 66 | * @ORM\Column(name="periode", type="smallint", options={"default":0}, nullable=false) |
||
| 67 | */ |
||
| 68 | private $period = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | * |
||
| 73 | * @ORM\Column(name="zahlweise", type="string", length=3, options={"default":"BEZ", "fixed":true}, nullable=false) |
||
| 74 | */ |
||
| 75 | private $paymentType = 'BEZ'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | * |
||
| 80 | * @ORM\Column(name="kommentar", type="text", options={"default":""}, nullable=false) |
||
| 81 | */ |
||
| 82 | private $comment = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | * |
||
| 87 | * @ORM\Column(name="ueb_code", type="string", length=32, options={"default":""}, nullable=false) |
||
| 88 | */ |
||
| 89 | private $transferCode = ''; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | * |
||
| 94 | * @ORM\Column(name="data", type="text", nullable=true) |
||
| 95 | */ |
||
| 96 | private $data; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | * |
||
| 101 | * @ORM\Column(name="source", type="string", length=250, nullable=true) |
||
| 102 | */ |
||
| 103 | private $source; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string |
||
| 107 | * |
||
| 108 | * @ORM\Column(name="remote_addr", type="string", length=250, options={"default":""}, nullable=false) |
||
| 109 | */ |
||
| 110 | private $remoteAddr = ''; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | * |
||
| 115 | * @ORM\Column(name="hash", type="string", length=250, nullable=true) |
||
| 116 | */ |
||
| 117 | private $hash; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var boolean |
||
| 121 | * |
||
| 122 | * @ORM\Column(name="is_public", type="boolean", options={"default":0}, nullable=false) |
||
| 123 | */ |
||
| 124 | private $isPublic = 0; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var \DateTime |
||
| 128 | * |
||
| 129 | * @ORM\Column(name="dt_new", type="datetime", nullable=true) |
||
| 130 | */ |
||
| 131 | private $dtNew; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var \DateTime |
||
| 135 | * |
||
| 136 | * @ORM\Column(name="dt_del", type="datetime", nullable=true) |
||
| 137 | */ |
||
| 138 | private $dtDel; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var \DateTime |
||
| 142 | * |
||
| 143 | * @ORM\Column(name="dt_exp", type="datetime", nullable=true) |
||
| 144 | */ |
||
| 145 | private $dtExp; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var string |
||
| 149 | * |
||
| 150 | * @ORM\Column(name="status", type="string", length=1, options={"default":"N", "fixed":true}, nullable=false) |
||
| 151 | */ |
||
| 152 | private $status = 'N'; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var \DateTime |
||
| 156 | * |
||
| 157 | * @ORM\Column(name="dt_gruen", type="datetime", nullable=true) |
||
| 158 | */ |
||
| 159 | private $dtGruen; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var \DateTime |
||
| 163 | * |
||
| 164 | * @ORM\Column(name="dt_backup", type="datetime", nullable=true) |
||
| 165 | */ |
||
| 166 | private $dtBackup; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var integer |
||
| 170 | * |
||
| 171 | * @ORM\Column(name="id", type="integer") |
||
| 172 | * @ORM\Id |
||
| 173 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
| 174 | */ |
||
| 175 | private $id; |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Set name |
||
| 180 | * |
||
| 181 | * @param string $name |
||
| 182 | * @return Spenden |
||
| 183 | */ |
||
| 184 | public function setName( $name ) { |
||
| 185 | $this->name = $name; |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get name |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getName() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set city |
||
| 201 | * |
||
| 202 | * @param string $city |
||
| 203 | * @return Spenden |
||
| 204 | */ |
||
| 205 | public function setCity( $city ) { |
||
| 206 | $this->city = $city; |
||
| 207 | |||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get city |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getCity() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set email |
||
| 222 | * |
||
| 223 | * @param string $email |
||
| 224 | * @return Spenden |
||
| 225 | */ |
||
| 226 | public function setEmail( $email ) { |
||
| 227 | $this->email = $email; |
||
| 228 | |||
| 229 | return $this; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get email |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function getEmail() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set info |
||
| 243 | * |
||
| 244 | * @param boolean $info |
||
| 245 | * @return Spenden |
||
| 246 | */ |
||
| 247 | public function setInfo( $info ) { |
||
| 248 | $this->info = $info; |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get info |
||
| 255 | * |
||
| 256 | * @return boolean |
||
| 257 | */ |
||
| 258 | public function getInfo() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set donation receipt state |
||
| 264 | * |
||
| 265 | * @param boolean $donationReceipt |
||
| 266 | * @return Spenden |
||
| 267 | */ |
||
| 268 | public function setDonationReceipt( $donationReceipt ) { |
||
| 269 | $this->donationReceipt = $donationReceipt; |
||
| 270 | |||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get donation receipt state |
||
| 276 | * |
||
| 277 | * @return boolean |
||
| 278 | */ |
||
| 279 | public function getDonationReceipt() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set publicly displayed donation record |
||
| 285 | * |
||
| 286 | * @param string $publicRecord |
||
| 287 | * @return Spenden |
||
| 288 | */ |
||
| 289 | public function setPublicRecord( $publicRecord ) { |
||
| 290 | $this->publicRecord = $publicRecord; |
||
| 291 | |||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get publicly displayed donation record |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getPublicRecord() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set amount |
||
| 306 | * |
||
| 307 | * @param string $amount |
||
| 308 | * @return Spenden |
||
| 309 | */ |
||
| 310 | public function setAmount( $amount ) { |
||
| 311 | $this->amount = $amount; |
||
| 312 | |||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get amount |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getAmount() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set period |
||
| 327 | * |
||
| 328 | * @param integer $period |
||
| 329 | * @return Spenden |
||
| 330 | */ |
||
| 331 | public function setPeriod( $period ) { |
||
| 332 | $this->period = $period; |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get period |
||
| 339 | * |
||
| 340 | * @return integer |
||
| 341 | */ |
||
| 342 | public function getPeriod() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set payment type short code |
||
| 348 | * |
||
| 349 | * @param string $paymentType |
||
| 350 | * @return Spenden |
||
| 351 | */ |
||
| 352 | public function setPaymentType( $paymentType ) { |
||
| 353 | $this->paymentType = $paymentType; |
||
| 354 | |||
| 355 | return $this; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get payment type short code |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getPaymentType() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set comment |
||
| 369 | * |
||
| 370 | * @param string $comment |
||
| 371 | * @return Spenden |
||
| 372 | */ |
||
| 373 | public function setComment( $comment ) { |
||
| 374 | $this->comment = $comment; |
||
| 375 | |||
| 376 | return $this; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get comment |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function getComment() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Set bank transfer reference code |
||
| 390 | * |
||
| 391 | * @param string $transferCode |
||
| 392 | * @return Spenden |
||
| 393 | */ |
||
| 394 | public function setTransferCode( $transferCode ) { |
||
| 395 | $this->transferCode = $transferCode; |
||
| 396 | |||
| 397 | return $this; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get bank transfer reference code |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getTransferCode() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Set data |
||
| 411 | * |
||
| 412 | * @param string $data |
||
| 413 | * @return Spenden |
||
| 414 | */ |
||
| 415 | public function setData( $data ) { |
||
| 416 | $this->data = $data; |
||
| 417 | |||
| 418 | return $this; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get data |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getData() { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set source |
||
| 432 | * |
||
| 433 | * @param string $source |
||
| 434 | * @return Spenden |
||
| 435 | */ |
||
| 436 | public function setSource( $source ) { |
||
| 437 | $this->source = $source; |
||
| 438 | |||
| 439 | return $this; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get source |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getSource() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Set remoteAddr |
||
| 453 | * |
||
| 454 | * @param string $remoteAddr |
||
| 455 | * @return Spenden |
||
| 456 | */ |
||
| 457 | public function setRemoteAddr( $remoteAddr ) { |
||
| 458 | $this->remoteAddr = $remoteAddr; |
||
| 459 | |||
| 460 | return $this; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Get remoteAddr |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public function getRemoteAddr() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Set hash |
||
| 474 | * |
||
| 475 | * @param string $hash |
||
| 476 | * @return Spenden |
||
| 477 | */ |
||
| 478 | public function setHash( $hash ) { |
||
| 479 | $this->hash = $hash; |
||
| 480 | |||
| 481 | return $this; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get hash |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getHash() { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Set isPublic |
||
| 495 | * |
||
| 496 | * @param boolean $isPublic |
||
| 497 | * @return Spenden |
||
| 498 | */ |
||
| 499 | public function setIsPublic( $isPublic ) { |
||
| 500 | $this->isPublic = $isPublic; |
||
| 501 | |||
| 502 | return $this; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get isPublic |
||
| 507 | * |
||
| 508 | * @return boolean |
||
| 509 | */ |
||
| 510 | public function getIsPublic() { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Set dtNew |
||
| 516 | * |
||
| 517 | * @param \DateTime $dtNew |
||
| 518 | * @return Spenden |
||
| 519 | */ |
||
| 520 | public function setDtNew( $dtNew ) { |
||
| 521 | $this->dtNew = $dtNew; |
||
| 522 | |||
| 523 | return $this; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get dtNew |
||
| 528 | * |
||
| 529 | * @return \DateTime |
||
| 530 | */ |
||
| 531 | public function getDtNew() { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Set dtDel |
||
| 537 | * |
||
| 538 | * @param \DateTime $dtDel |
||
| 539 | * @return Spenden |
||
| 540 | */ |
||
| 541 | public function setDtDel( $dtDel ) { |
||
| 542 | $this->dtDel = $dtDel; |
||
| 543 | |||
| 544 | return $this; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Get dtDel |
||
| 549 | * |
||
| 550 | * @return \DateTime |
||
| 551 | */ |
||
| 552 | public function getDtDel() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Set dtExp |
||
| 558 | * |
||
| 559 | * @param \DateTime $dtExp |
||
| 560 | * @return Spenden |
||
| 561 | */ |
||
| 562 | public function setDtExp( $dtExp ) { |
||
| 563 | $this->dtExp = $dtExp; |
||
| 564 | |||
| 565 | return $this; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get dtExp |
||
| 570 | * |
||
| 571 | * @return \DateTime |
||
| 572 | */ |
||
| 573 | public function getDtExp() { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Set status |
||
| 579 | * |
||
| 580 | * @param string $status |
||
| 581 | * @return Spenden |
||
| 582 | */ |
||
| 583 | public function setStatus( $status ) { |
||
| 584 | $this->status = $status; |
||
| 585 | |||
| 586 | return $this; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get status |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function getStatus() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Set dtGruen |
||
| 600 | * |
||
| 601 | * @param \DateTime $dtGruen |
||
| 602 | * @return Spenden |
||
| 603 | */ |
||
| 604 | public function setDtGruen( $dtGruen ) { |
||
| 605 | $this->dtGruen = $dtGruen; |
||
| 606 | |||
| 607 | return $this; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get dtGruen |
||
| 612 | * |
||
| 613 | * @return \DateTime |
||
| 614 | */ |
||
| 615 | public function getDtGruen() { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Set dtBackup |
||
| 621 | * |
||
| 622 | * @param \DateTime $dtBackup |
||
| 623 | * @return Spenden |
||
| 624 | */ |
||
| 625 | public function setDtBackup( $dtBackup ) { |
||
| 626 | $this->dtBackup = $dtBackup; |
||
| 627 | |||
| 628 | return $this; |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get dtBackup |
||
| 633 | * |
||
| 634 | * @return \DateTime |
||
| 635 | */ |
||
| 636 | public function getDtBackup() { |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Get id |
||
| 642 | * |
||
| 643 | * @return integer |
||
| 644 | */ |
||
| 645 | public function getId() { |
||
| 648 | |||
| 649 | public function getUExpiry() { |
||
| 650 | $data = $this->decodeData( $this->data ); |
||
| 651 | |||
| 652 | return $data[ 'uexpiry' ]; |
||
| 653 | } |
||
| 654 | |||
| 655 | public function uTokenIsExpired() { |
||
| 659 | |||
| 660 | public function validateToken( $tokenToCheck, $serverSecret ) { |
||
| 661 | $checkToken = preg_replace( '/\$.*$/', '', $tokenToCheck ); |
||
| 662 | |||
| 663 | $checkToken = $checkToken . '$' . |
||
| 664 | sha1( sha1( "$checkToken+$serverSecret" ) . '|' . |
||
| 665 | sha1( "{$this->id}+$serverSecret" ) . '|' . |
||
| 666 | sha1( "{$this->dtNew->format( 'Y-m-d H:i:s' )}+$serverSecret" ) ); |
||
| 667 | return $checkToken === $tokenToCheck; |
||
| 668 | } |
||
| 669 | |||
| 670 | public function getEntryType( $mode = null ) { |
||
| 671 | $data = $this->decodeData( $this->data ); |
||
| 672 | |||
| 673 | if ( $mode === null ) { |
||
| 674 | $mode = $this->publicRecord; |
||
| 675 | if ( !is_int( $mode ) ) { |
||
| 676 | return $this->publicRecord; |
||
| 677 | } |
||
| 678 | } |
||
| 679 | |||
| 680 | if ( $mode == 1 || $mode == 2 ) { |
||
| 681 | $eintrag = $this->name; |
||
| 682 | } else { |
||
| 683 | $eintrag = "anonym"; |
||
| 684 | } |
||
| 685 | |||
| 686 | if ( ( $mode == 1 || $mode == 3 ) && !empty( $data[ "ort" ] ) ) { |
||
| 687 | $eintrag .= ", " . $data[ "ort" ]; |
||
| 688 | } |
||
| 689 | |||
| 690 | return $eintrag; |
||
| 691 | } |
||
| 692 | |||
| 693 | private function decodeData( $data ) { |
||
| 696 | } |
||
| 697 |