Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Session 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 Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Session |
||
| 28 | { |
||
| 29 | const VISIBLE = 1; |
||
| 30 | const READ_ONLY = 2; |
||
| 31 | const INVISIBLE = 3; |
||
| 32 | const AVAILABLE = 4; |
||
| 33 | |||
| 34 | const STUDENT = 0; |
||
| 35 | const DRH = 1; |
||
| 36 | const COACH = 2; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var integer |
||
| 40 | * |
||
| 41 | * @ORM\Column(name="id", type="integer", nullable=false, unique=false) |
||
| 42 | * @ORM\Id |
||
| 43 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 44 | */ |
||
| 45 | private $id; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | * |
||
| 50 | * @ORM\Column(name="name", type="string", length=150, nullable=false, unique=false) |
||
| 51 | */ |
||
| 52 | private $name; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | * |
||
| 57 | * @ORM\Column(name="description", type="text", nullable=true, unique=false) |
||
| 58 | */ |
||
| 59 | private $description; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | * |
||
| 64 | * @ORM\Column(name="show_description", type="boolean", nullable=true) |
||
| 65 | */ |
||
| 66 | private $showDescription; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var integer |
||
| 70 | * |
||
| 71 | * @ORM\Column(name="duration", type="integer", nullable=true) |
||
| 72 | */ |
||
| 73 | private $duration; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var integer |
||
| 77 | * |
||
| 78 | * @ORM\Column(name="nbr_courses", type="smallint", nullable=true, unique=false) |
||
| 79 | */ |
||
| 80 | private $nbrCourses; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var integer |
||
| 84 | * |
||
| 85 | * @ORM\Column(name="nbr_users", type="integer", nullable=true, unique=false) |
||
| 86 | */ |
||
| 87 | private $nbrUsers; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var integer |
||
| 91 | * |
||
| 92 | * @ORM\Column(name="nbr_classes", type="integer", nullable=true, unique=false) |
||
| 93 | */ |
||
| 94 | private $nbrClasses; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var integer |
||
| 98 | * |
||
| 99 | * @ORM\Column(name="session_admin_id", type="integer", nullable=true, unique=false) |
||
| 100 | */ |
||
| 101 | private $sessionAdminId; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var integer |
||
| 105 | * |
||
| 106 | * @ORM\Column(name="visibility", type="integer", nullable=false, unique=false) |
||
| 107 | */ |
||
| 108 | private $visibility; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var integer |
||
| 112 | * |
||
| 113 | * @ORM\Column(name="promotion_id", type="integer", nullable=true, unique=false) |
||
| 114 | */ |
||
| 115 | private $promotionId; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var \DateTime |
||
| 119 | * |
||
| 120 | * @ORM\Column(name="display_start_date", type="datetime", nullable=true, unique=false) |
||
| 121 | */ |
||
| 122 | private $displayStartDate; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var \DateTime |
||
| 126 | * |
||
| 127 | * @ORM\Column(name="display_end_date", type="datetime", nullable=true, unique=false) |
||
| 128 | */ |
||
| 129 | private $displayEndDate; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var \DateTime |
||
| 133 | * |
||
| 134 | * @ORM\Column(name="access_start_date", type="datetime", nullable=true, unique=false) |
||
| 135 | */ |
||
| 136 | private $accessStartDate; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var \DateTime |
||
| 140 | * |
||
| 141 | * @ORM\Column(name="access_end_date", type="datetime", nullable=true, unique=false) |
||
| 142 | */ |
||
| 143 | private $accessEndDate; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var \DateTime |
||
| 147 | * |
||
| 148 | * @ORM\Column(name="coach_access_start_date", type="datetime", nullable=true, unique=false) |
||
| 149 | */ |
||
| 150 | private $coachAccessStartDate; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var \DateTime |
||
| 154 | * |
||
| 155 | * @ORM\Column(name="coach_access_end_date", type="datetime", nullable=true, unique=false) |
||
| 156 | */ |
||
| 157 | private $coachAccessEndDate; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CItemProperty", mappedBy="session") |
||
| 161 | **/ |
||
| 162 | //private $items; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="sessionAsGeneralCoach") |
||
| 166 | * @ORM\JoinColumn(name="id_coach", referencedColumnName="id") |
||
| 167 | **/ |
||
| 168 | private $generalCoach; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\SessionCategory", inversedBy="session") |
||
| 172 | * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id") |
||
| 173 | **/ |
||
| 174 | private $category; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var ArrayCollection |
||
| 178 | * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true) |
||
| 179 | **/ |
||
| 180 | protected $courses; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var ArrayCollection |
||
| 184 | * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true) |
||
| 185 | **/ |
||
| 186 | protected $users; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var ArrayCollection |
||
| 190 | * @ORM\OneToMany(targetEntity="SessionRelCourseRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true) |
||
| 191 | **/ |
||
| 192 | protected $userCourseSubscriptions; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var Course |
||
| 196 | **/ |
||
| 197 | protected $currentCourse; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var boolean |
||
| 201 | * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false}) |
||
| 202 | */ |
||
| 203 | private $sendSubscriptionNotification; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Constructor |
||
| 207 | */ |
||
| 208 | public function __construct() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return int |
||
| 233 | */ |
||
| 234 | public function getDuration() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param int $duration |
||
| 241 | */ |
||
| 242 | public function setDuration($duration) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getShowDescription() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $showDescription |
||
| 257 | */ |
||
| 258 | public function setShowDescription($showDescription) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function __toString() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get id |
||
| 273 | * |
||
| 274 | * @return integer |
||
| 275 | */ |
||
| 276 | public function getId() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param int $id |
||
| 283 | */ |
||
| 284 | public function setId($id) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return ArrayCollection |
||
| 291 | */ |
||
| 292 | public function getUsers() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param $users |
||
| 299 | */ |
||
| 300 | public function setUsers($users) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param SessionRelUser $user |
||
| 311 | */ |
||
| 312 | public function addUser(SessionRelUser $user) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param int $status |
||
| 323 | * @param User $user |
||
| 324 | */ |
||
| 325 | public function addUserInSession($status, User $user) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param SessionRelUser $subscription |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | View Code Duplication | public function hasUser(SessionRelUser $subscription) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @return ArrayCollection |
||
| 360 | */ |
||
| 361 | public function getCourses() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param $courses |
||
| 368 | */ |
||
| 369 | public function setCourses($courses) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param SessionRelCourse $course |
||
| 380 | */ |
||
| 381 | public function addCourses(SessionRelCourse $course) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param Course $course |
||
| 389 | * |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | public function hasCourse(Course $course) |
||
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * Remove $course |
||
| 409 | * |
||
| 410 | * @param SessionRelCourse $course |
||
| 411 | */ |
||
| 412 | public function removeCourses($course) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param User $user |
||
| 423 | * @param Course $course |
||
| 424 | * @param int $status if not set it will check if the user is registered |
||
| 425 | * with any status |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function hasUserInCourse(User $user, Course $course, $status = null) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param User $user |
||
| 438 | * @param Course $course |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function hasStudentInCourse(User $user, Course $course) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param User $user |
||
| 449 | * @param Course $course |
||
| 450 | * |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | public function hasCoachInCourseWithStatus(User $user, Course $course) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param User $user |
||
| 460 | * @param Course $course |
||
| 461 | * @param string $status |
||
| 462 | * |
||
| 463 | * @return \Doctrine\Common\Collections\Collection|static |
||
| 464 | */ |
||
| 465 | public function getUserInCourse(User $user, Course $course, $status = null) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set name |
||
| 484 | * |
||
| 485 | * @param string $name |
||
| 486 | * @return Session |
||
| 487 | */ |
||
| 488 | public function setName($name) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get name |
||
| 497 | * |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | public function getName() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Set description |
||
| 507 | * |
||
| 508 | * @param string $description |
||
| 509 | * @return Groups |
||
| 510 | */ |
||
| 511 | public function setDescription($description) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get description |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function getDescription() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Set nbrCourses |
||
| 530 | * |
||
| 531 | * @param integer $nbrCourses |
||
| 532 | * @return Session |
||
| 533 | */ |
||
| 534 | public function setNbrCourses($nbrCourses) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Get nbrCourses |
||
| 543 | * |
||
| 544 | * @return integer |
||
| 545 | */ |
||
| 546 | public function getNbrCourses() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set nbrUsers |
||
| 553 | * |
||
| 554 | * @param integer $nbrUsers |
||
| 555 | * @return Session |
||
| 556 | */ |
||
| 557 | public function setNbrUsers($nbrUsers) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get nbrUsers |
||
| 566 | * |
||
| 567 | * @return integer |
||
| 568 | */ |
||
| 569 | public function getNbrUsers() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set nbrClasses |
||
| 576 | * |
||
| 577 | * @param integer $nbrClasses |
||
| 578 | * @return Session |
||
| 579 | */ |
||
| 580 | public function setNbrClasses($nbrClasses) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get nbrClasses |
||
| 589 | * |
||
| 590 | * @return integer |
||
| 591 | */ |
||
| 592 | public function getNbrClasses() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Set sessionAdminId |
||
| 599 | * |
||
| 600 | * @param integer $sessionAdminId |
||
| 601 | * @return Session |
||
| 602 | */ |
||
| 603 | public function setSessionAdminId($sessionAdminId) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get sessionAdminId |
||
| 612 | * |
||
| 613 | * @return integer |
||
| 614 | */ |
||
| 615 | public function getSessionAdminId() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Set visibility |
||
| 622 | * |
||
| 623 | * @param integer $visibility |
||
| 624 | * @return Session |
||
| 625 | */ |
||
| 626 | public function setVisibility($visibility) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Get visibility |
||
| 635 | * |
||
| 636 | * @return integer |
||
| 637 | */ |
||
| 638 | public function getVisibility() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Set promotionId |
||
| 645 | * |
||
| 646 | * @param integer $promotionId |
||
| 647 | * @return Session |
||
| 648 | */ |
||
| 649 | public function setPromotionId($promotionId) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get promotionId |
||
| 658 | * |
||
| 659 | * @return integer |
||
| 660 | */ |
||
| 661 | public function getPromotionId() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Set displayStartDate |
||
| 668 | * |
||
| 669 | * @param \DateTime $displayStartDate |
||
| 670 | * @return Session |
||
| 671 | */ |
||
| 672 | public function setDisplayStartDate($displayStartDate) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Get displayStartDate |
||
| 681 | * |
||
| 682 | * @return \DateTime |
||
| 683 | */ |
||
| 684 | public function getDisplayStartDate() |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Set displayEndDate |
||
| 691 | * |
||
| 692 | * @param \DateTime $displayEndDate |
||
| 693 | * @return Session |
||
| 694 | */ |
||
| 695 | public function setDisplayEndDate($displayEndDate) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Get displayEndDate |
||
| 704 | * |
||
| 705 | * @return \DateTime |
||
| 706 | */ |
||
| 707 | public function getDisplayEndDate() |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Set accessStartDate |
||
| 714 | * |
||
| 715 | * @param \DateTime $accessStartDate |
||
| 716 | * @return Session |
||
| 717 | */ |
||
| 718 | public function setAccessStartDate($accessStartDate) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Get accessStartDate |
||
| 727 | * |
||
| 728 | * @return \DateTime |
||
| 729 | */ |
||
| 730 | public function getAccessStartDate() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Set accessEndDate |
||
| 737 | * |
||
| 738 | * @param \DateTime $accessEndDate |
||
| 739 | * @return Session |
||
| 740 | */ |
||
| 741 | public function setAccessEndDate($accessEndDate) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Get accessEndDate |
||
| 750 | * |
||
| 751 | * @return \DateTime |
||
| 752 | */ |
||
| 753 | public function getAccessEndDate() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Set coachAccessStartDate |
||
| 760 | * |
||
| 761 | * @param \DateTime $coachAccessStartDate |
||
| 762 | * @return Session |
||
| 763 | */ |
||
| 764 | public function setCoachAccessStartDate($coachAccessStartDate) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Get coachAccessStartDate |
||
| 773 | * |
||
| 774 | * @return \DateTime |
||
| 775 | */ |
||
| 776 | public function getCoachAccessStartDate() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Set coachAccessEndDate |
||
| 783 | * |
||
| 784 | * @param \DateTime $coachAccessEndDate |
||
| 785 | * @return Session |
||
| 786 | */ |
||
| 787 | public function setCoachAccessEndDate($coachAccessEndDate) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Get coachAccessEndDate |
||
| 796 | * |
||
| 797 | * @return \DateTime |
||
| 798 | */ |
||
| 799 | public function getCoachAccessEndDate() |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Get id |
||
| 806 | * |
||
| 807 | * @return integer |
||
| 808 | */ |
||
| 809 | public function getGeneralCoach() |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @param $coach |
||
| 816 | */ |
||
| 817 | public function setGeneralCoach($coach) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * @return mixed |
||
| 824 | */ |
||
| 825 | public function getCategory() |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param $category |
||
| 832 | * @return $this |
||
| 833 | */ |
||
| 834 | public function setCategory($category) |
||
| 840 | |||
| 841 | /** |
||
| 842 | * @return array |
||
| 843 | */ |
||
| 844 | public static function getStatusList() |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Check if session is visible |
||
| 856 | * @return bool |
||
| 857 | */ |
||
| 858 | public function isActive() |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @param Course $course |
||
| 872 | */ |
||
| 873 | public function addCourse(Course $course) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * @return ArrayCollection |
||
| 882 | */ |
||
| 883 | public function getUserCourseSubscriptions() |
||
| 887 | |||
| 888 | /** |
||
| 889 | * @param ArrayCollection $userCourseSubscriptions |
||
| 890 | */ |
||
| 891 | public function setUserCourseSubscriptions($userCourseSubscriptions) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @param SessionRelCourseRelUser $subscription |
||
| 902 | */ |
||
| 903 | public function addUserCourseSubscription(SessionRelCourseRelUser $subscription) |
||
| 910 | |||
| 911 | /** |
||
| 912 | * @param int $status |
||
| 913 | * @param User $user |
||
| 914 | * @param Course $course |
||
| 915 | */ |
||
| 916 | public function addUserInCourse($status, User $user, Course $course) |
||
| 925 | |||
| 926 | /** |
||
| 927 | * @param SessionRelCourseRelUser $subscription |
||
| 928 | * @return bool |
||
| 929 | */ |
||
| 930 | View Code Duplication | public function hasUserCourseSubscription(SessionRelCourseRelUser $subscription) |
|
| 947 | |||
| 948 | /** |
||
| 949 | * @return Course |
||
| 950 | */ |
||
| 951 | public function getCurrentCourse() |
||
| 955 | |||
| 956 | /** |
||
| 957 | * @param Course $course |
||
| 958 | * @return $this |
||
| 959 | */ |
||
| 960 | public function setCurrentCourse(Course $course) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Set $sendSubscriptionNotification |
||
| 971 | * @param boolean $sendNotification |
||
| 972 | * @return \Chamilo\CoreBundle\Entity\Session |
||
| 973 | */ |
||
| 974 | public function setSendSubscriptionNotification($sendNotification) |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Get $sendSubscriptionNotification |
||
| 983 | * @return boolean |
||
| 984 | */ |
||
| 985 | public function getSendSubscriptionNotification() |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Get user from course by status |
||
| 992 | * @param \Chamilo\CoreBundle\Entity\Course $course |
||
| 993 | * @param string $status |
||
| 994 | * @return \Doctrine\Common\Collections\Collection|static |
||
| 995 | */ |
||
| 996 | public function getUserCourseSubscriptionsByStatus(Course $course, $status) |
||
| 1008 | |||
| 1009 | public function getBuyCoursePluginPrice() |
||
| 1028 | |||
| 1029 | } |
||
| 1030 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.