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 Organization 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 Organization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class Organization extends BaseEntity implements |
||
|
|
|||
| 42 | OrganizationInterface, |
||
| 43 | DraftableEntityInterface, |
||
| 44 | ResourceInterface |
||
| 45 | { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Always enabled even if there are no active jobs |
||
| 49 | */ |
||
| 50 | const PROFILE_ALWAYS_ENABLE = 'always'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Hide if there are no jobs available |
||
| 54 | */ |
||
| 55 | const PROFILE_ACTIVE_JOBS = 'active-jobs'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Always disabled profile |
||
| 59 | */ |
||
| 60 | const PROFILE_DISABLED = 'disabled'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Event name of post construct event. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | const postConstruct = 'postRepositoryConstruct'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * externalId. Allows external applications to reference their primary key. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | * @ODM\Field(type="string") |
||
| 74 | * @ODM\Index |
||
| 75 | */ |
||
| 76 | protected $externalId; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The actual name of the organization. |
||
| 80 | * |
||
| 81 | * @var \Organizations\Entity\OrganizationName |
||
| 82 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 83 | */ |
||
| 84 | protected $organizationName; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Only for sorting/searching purposes |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | * @ODM\Field(type="string") |
||
| 91 | */ |
||
| 92 | protected $_organizationName; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Assigned permissions. |
||
| 96 | * |
||
| 97 | * @var PermissionsInterface |
||
| 98 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 99 | */ |
||
| 100 | protected $permissions; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * primary logo of an organization |
||
| 104 | * |
||
| 105 | * @var \Organizations\Entity\OrganizationImage |
||
| 106 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 107 | */ |
||
| 108 | protected $image; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * |
||
| 112 | * |
||
| 113 | * @ODM\EmbedOne(targetDocument="\Core\Entity\ImageSet") |
||
| 114 | * @var ImageSet |
||
| 115 | */ |
||
| 116 | protected $images; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Flag indicating draft state of this job. |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | * @ODM\Boolean |
||
| 123 | */ |
||
| 124 | protected $isDraft = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Organization contact data. |
||
| 128 | * |
||
| 129 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") |
||
| 130 | */ |
||
| 131 | protected $contact; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The organizations' description. |
||
| 135 | * |
||
| 136 | * @var string |
||
| 137 | * @ODM\Field(type="string") |
||
| 138 | */ |
||
| 139 | protected $description; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * The parent of this organization. |
||
| 143 | * |
||
| 144 | * @see setParent() |
||
| 145 | * @var OrganizationInterface | null |
||
| 146 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 147 | * @since 0.18 |
||
| 148 | */ |
||
| 149 | protected $parent; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The hiring organizations of this organization. |
||
| 153 | * |
||
| 154 | * @var Collection |
||
| 155 | * @ODM\ReferenceMany( |
||
| 156 | * targetDocument="Organizations\Entity\Organization", |
||
| 157 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 158 | * ) |
||
| 159 | * @since 0.18 |
||
| 160 | */ |
||
| 161 | protected $hiringOrganizations; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The associated employees (users) |
||
| 165 | * |
||
| 166 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 167 | * @var Collection |
||
| 168 | */ |
||
| 169 | protected $employees; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Jobs of this organization. |
||
| 173 | * |
||
| 174 | * @var Collection |
||
| 175 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 176 | * @since 0.18 |
||
| 177 | */ |
||
| 178 | protected $jobs; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * the owner of a Organization |
||
| 182 | * |
||
| 183 | * @var UserInterface $user |
||
| 184 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 185 | * @ODM\Index |
||
| 186 | */ |
||
| 187 | protected $user; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Default values of an organizations job template |
||
| 191 | * |
||
| 192 | * @var TemplateInterface; |
||
| 193 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template") |
||
| 194 | */ |
||
| 195 | protected $template; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Default values Workflow |
||
| 199 | * |
||
| 200 | * @var WorkflowSettingsInterface $workflowSettings ; |
||
| 201 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\WorkflowSettings") |
||
| 202 | */ |
||
| 203 | protected $workflowSettings; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Profile Setting |
||
| 207 | * @var string |
||
| 208 | * @ODM\Field(type="string", nullable=true) |
||
| 209 | */ |
||
| 210 | protected $profileSetting; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getProfileSetting() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $profileSetting |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function setProfileSetting($profileSetting) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the string identifier of the Resource |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function getResourceId() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Gets the organization name |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getName() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Sets the parent of an organization |
||
| 258 | * |
||
| 259 | * @param OrganizationInterface $parent |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function setParent(OrganizationInterface $parent) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @deprecated |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getSearchableProperties() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Gets the parent of an organization |
||
| 281 | * |
||
| 282 | * @param bool $returnSelf returns itself, if this organization does not have a parent? |
||
| 283 | * |
||
| 284 | * @return null|OrganizationInterface |
||
| 285 | */ |
||
| 286 | public function getParent($returnSelf = false) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Gets the Draft flag |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function isDraft() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Gets linked organizations |
||
| 303 | * |
||
| 304 | * @return Collection |
||
| 305 | */ |
||
| 306 | public function getHiringOrganizations() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Sets the draft flag |
||
| 313 | * |
||
| 314 | * @param bool $flag |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setIsDraft($flag) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function isHiringOrganization() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Returns true, if the user belongs to the organization. |
||
| 335 | * |
||
| 336 | * @param UserInterface $user |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function isAssociated(UserInterface $user) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Sets the external id. |
||
| 347 | * |
||
| 348 | * @param $externalId |
||
| 349 | * |
||
| 350 | * @return self |
||
| 351 | */ |
||
| 352 | public function setExternalId($externalId) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Checks, if a User is the owner of an organization |
||
| 361 | * |
||
| 362 | * @param UserInterface $user |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | public function isOwner(UserInterface $user) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Gets the internal id. |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getExternalId() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Returns true, if a User is an employee of the organization |
||
| 383 | * |
||
| 384 | * @param UserInterface $user |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function isEmployee(UserInterface $user) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @todo review this |
||
| 395 | * |
||
| 396 | * @param HydratorInterface $hydrator |
||
| 397 | * |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | public function setHydrator(HydratorInterface $hydrator) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 407 | * |
||
| 408 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 409 | * organization. |
||
| 410 | * |
||
| 411 | * @ODM\PreUpdate |
||
| 412 | * @ODM\PrePersist |
||
| 413 | * @since 0.18 |
||
| 414 | */ |
||
| 415 | public function updatePermissions() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * * @todo review this |
||
| 440 | * @return EntityHydrator |
||
| 441 | */ |
||
| 442 | public function getHydrator() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Sets the name of an organization |
||
| 449 | * |
||
| 450 | * @param OrganizationName $organizationName |
||
| 451 | * |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the organization name entity of an organisation |
||
| 468 | * |
||
| 469 | * @return OrganizationName |
||
| 470 | */ |
||
| 471 | public function getOrganizationName() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Gets the Permissions of an organization |
||
| 478 | * |
||
| 479 | * @return PermissionsInterface |
||
| 480 | */ |
||
| 481 | public function getPermissions() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Sets the Permissions of an Organization |
||
| 496 | * |
||
| 497 | * @param PermissionsInterface $permissions |
||
| 498 | * |
||
| 499 | * @return $this |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function setPermissions(PermissionsInterface $permissions) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Gets the Permissions Resource ID |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function getPermissionsResourceId() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param null $type |
||
| 524 | * |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function getPermissionsUserIds($type = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Sets the logo of an organization |
||
| 570 | * |
||
| 571 | * @param OrganizationImage $image |
||
| 572 | * |
||
| 573 | * @return self |
||
| 574 | * @deprecated since 0.29; use $this->getImages()->set() |
||
| 575 | */ |
||
| 576 | public function setImage(OrganizationImage $image = null) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Gets the Logo of an organization |
||
| 585 | * |
||
| 586 | * @param string|bool $key Key of the image to get. |
||
| 587 | * If true: get Thumbnail |
||
| 588 | * If false: get Original |
||
| 589 | * |
||
| 590 | * @return OrganizationImage |
||
| 591 | * @deprecated since 0.29; use $this->getImages()->get() |
||
| 592 | * @since 0.29 modified to return images from the image set for compatibility reasons |
||
| 593 | */ |
||
| 594 | public function getImage($key = ImageSet::ORIGINAL) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param ImageSet $images |
||
| 605 | * |
||
| 606 | * @return self |
||
| 607 | */ |
||
| 608 | public function setImages(ImageSet $images) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @return ImageSet |
||
| 617 | */ |
||
| 618 | public function getImages() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * |
||
| 629 | * |
||
| 630 | * @return self |
||
| 631 | */ |
||
| 632 | public function removeImages() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Sets the Contact Data of an organization |
||
| 641 | * |
||
| 642 | * @param EntityInterface $contact |
||
| 643 | * |
||
| 644 | * @return $this |
||
| 645 | */ |
||
| 646 | public function setContact(EntityInterface $contact = null) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Gets the contact Data of an organization |
||
| 658 | * |
||
| 659 | * @return OrganizationContact |
||
| 660 | */ |
||
| 661 | public function getContact() |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Gets the default description of an organization. |
||
| 672 | * |
||
| 673 | * This description is used as the default of the company_description |
||
| 674 | * used in a job template |
||
| 675 | * |
||
| 676 | * @return string |
||
| 677 | */ |
||
| 678 | public function getDescription() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Set the default description af an organization |
||
| 685 | * |
||
| 686 | * @param string $description |
||
| 687 | * |
||
| 688 | * @return $this |
||
| 689 | */ |
||
| 690 | public function setDescription($description) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Sets the the list of employees |
||
| 699 | * |
||
| 700 | * @param Collection $employees |
||
| 701 | * |
||
| 702 | * @return $this |
||
| 703 | */ |
||
| 704 | public function setEmployees(Collection $employees) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Gets the list of employees |
||
| 716 | * |
||
| 717 | * @return ArrayCollection|Collection |
||
| 718 | */ |
||
| 719 | public function getEmployees() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Gets an employee by User or ID. |
||
| 735 | * |
||
| 736 | * @param UserInterface|string $userOrId |
||
| 737 | * |
||
| 738 | * @return mixed|null |
||
| 739 | */ |
||
| 740 | public function getEmployee($userOrId) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Gets a list of Employees by a user role |
||
| 756 | * |
||
| 757 | * @param string $role |
||
| 758 | * |
||
| 759 | * @return ArrayCollection |
||
| 760 | */ |
||
| 761 | public function getEmployeesByRole($role) |
||
| 774 | |||
| 775 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Gets the owner of the organization |
||
| 788 | * |
||
| 789 | * @return UserInterface |
||
| 790 | */ |
||
| 791 | public function getUser() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Gets the Jobs of an organization |
||
| 798 | * |
||
| 799 | * @return Collection |
||
| 800 | */ |
||
| 801 | public function getJobs() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Gets default values of an organizations job template |
||
| 808 | * |
||
| 809 | * @return TemplateInterface |
||
| 810 | */ |
||
| 811 | public function getTemplate() |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Sets default values of an organizations job template |
||
| 822 | * |
||
| 823 | * @return self |
||
| 824 | */ |
||
| 825 | public function setTemplate(TemplateInterface $template) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Gets Workflow Settings |
||
| 834 | * |
||
| 835 | * @return WorkflowSettings|WorkflowSettingsInterface |
||
| 836 | */ |
||
| 837 | public function getWorkflowSettings() |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Sets Workflow Settings |
||
| 848 | * |
||
| 849 | * @param $workflowSettings |
||
| 850 | * |
||
| 851 | * @return self |
||
| 852 | */ |
||
| 853 | public function setWorkflowSettings($workflowSettings) |
||
| 859 | } |
||
| 860 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.