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 |
||
| 40 | class Organization extends BaseEntity implements |
||
|
|
|||
| 41 | OrganizationInterface, |
||
| 42 | DraftableEntityInterface, |
||
| 43 | ResourceInterface |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * Event name of post construct event. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | const postConstruct = 'postRepositoryConstruct'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * externalId. Allows external applications to reference their primary key. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | * @ODM\Field(type="string") |
||
| 57 | * @ODM\Index |
||
| 58 | */ |
||
| 59 | protected $externalId; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The actual name of the organization. |
||
| 63 | * |
||
| 64 | * @var \Organizations\Entity\OrganizationName |
||
| 65 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 66 | */ |
||
| 67 | protected $organizationName; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Only for sorting/searching purposes |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | * @ODM\Field(type="string") |
||
| 74 | */ |
||
| 75 | protected $_organizationName; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Assigned permissions. |
||
| 79 | * |
||
| 80 | * @var PermissionsInterface |
||
| 81 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 82 | */ |
||
| 83 | protected $permissions; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * primary logo of an organization |
||
| 87 | * |
||
| 88 | * @var \Organizations\Entity\OrganizationImage |
||
| 89 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 90 | */ |
||
| 91 | protected $image; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * |
||
| 95 | * |
||
| 96 | * @ODM\EmbedOne(targetDocument="\Core\Entity\ImageSet") |
||
| 97 | * @var Images |
||
| 98 | */ |
||
| 99 | protected $images; |
||
| 100 | /** |
||
| 101 | * Flag indicating draft state of this job. |
||
| 102 | * |
||
| 103 | * @var bool |
||
| 104 | * @ODM\Boolean |
||
| 105 | */ |
||
| 106 | protected $isDraft = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Organization contact data. |
||
| 110 | * |
||
| 111 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") |
||
| 112 | */ |
||
| 113 | protected $contact; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The organizations' description. |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | * @ODM\Field(type="string") |
||
| 120 | */ |
||
| 121 | protected $description; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The parent of this organization. |
||
| 125 | * |
||
| 126 | * @see setParent() |
||
| 127 | * @var OrganizationInterface | null |
||
| 128 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 129 | * @since 0.18 |
||
| 130 | */ |
||
| 131 | protected $parent; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The hiring organizations of this organization. |
||
| 135 | * |
||
| 136 | * @var Collection |
||
| 137 | * @ODM\ReferenceMany( |
||
| 138 | * targetDocument="Organizations\Entity\Organization", |
||
| 139 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 140 | * ) |
||
| 141 | * @since 0.18 |
||
| 142 | */ |
||
| 143 | protected $hiringOrganizations; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * The associated employees (users) |
||
| 147 | * |
||
| 148 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 149 | * @var Collection |
||
| 150 | */ |
||
| 151 | protected $employees; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Jobs of this organization. |
||
| 155 | * |
||
| 156 | * @var Collection |
||
| 157 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 158 | * @since 0.18 |
||
| 159 | */ |
||
| 160 | protected $jobs; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * the owner of a Organization |
||
| 164 | * |
||
| 165 | * @var UserInterface $user |
||
| 166 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 167 | * @ODM\Index |
||
| 168 | */ |
||
| 169 | protected $user; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Default values of an organizations job template |
||
| 173 | * |
||
| 174 | * @var TemplateInterface; |
||
| 175 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template") |
||
| 176 | */ |
||
| 177 | protected $template; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Default values Workflow |
||
| 181 | * |
||
| 182 | * @var WorkflowSettingsInterface $workflowSettings ; |
||
| 183 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\WorkflowSettings") |
||
| 184 | */ |
||
| 185 | protected $workflowSettings; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the string identifier of the Resource |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getResourceId() |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Gets the organization name |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getName() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Sets the parent of an organization |
||
| 214 | * |
||
| 215 | * @param OrganizationInterface $parent |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function setParent(OrganizationInterface $parent) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @deprecated |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getSearchableProperties() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Gets the parent of an organization |
||
| 237 | * |
||
| 238 | * @param bool $returnSelf returns itself, if this organization does not have a parent? |
||
| 239 | * |
||
| 240 | * @return null|OrganizationInterface |
||
| 241 | */ |
||
| 242 | public function getParent($returnSelf = false) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Gets the Draft flag |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function isDraft() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Gets linked organizations |
||
| 259 | * |
||
| 260 | * @return Collection |
||
| 261 | */ |
||
| 262 | public function getHiringOrganizations() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sets the draft flag |
||
| 269 | * |
||
| 270 | * @param bool $flag |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function setIsDraft($flag) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function isHiringOrganization() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Returns true, if the user belongs to the organization. |
||
| 291 | * |
||
| 292 | * @param UserInterface $user |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function isAssociated(UserInterface $user) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Sets the external id. |
||
| 303 | * |
||
| 304 | * @param $externalId |
||
| 305 | * |
||
| 306 | * @return self |
||
| 307 | */ |
||
| 308 | public function setExternalId($externalId) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Checks, if a User is the owner of an organization |
||
| 317 | * |
||
| 318 | * @param UserInterface $user |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function isOwner(UserInterface $user) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Gets the internal id. |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function getExternalId() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns true, if a User is an employee of the organization |
||
| 339 | * |
||
| 340 | * @param UserInterface $user |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function isEmployee(UserInterface $user) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @todo review this |
||
| 351 | * |
||
| 352 | * @param HydratorInterface $hydrator |
||
| 353 | * |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function setHydrator(HydratorInterface $hydrator) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 363 | * |
||
| 364 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 365 | * organization. |
||
| 366 | * |
||
| 367 | * @ODM\PreUpdate |
||
| 368 | * @ODM\PrePersist |
||
| 369 | * @since 0.18 |
||
| 370 | */ |
||
| 371 | public function updatePermissions() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * * @todo review this |
||
| 396 | * @return EntityHydrator |
||
| 397 | */ |
||
| 398 | public function getHydrator() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Sets the name of an organization |
||
| 405 | * |
||
| 406 | * @param OrganizationName $organizationName |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Gets the organization name entity of an organisation |
||
| 424 | * |
||
| 425 | * @return OrganizationName |
||
| 426 | */ |
||
| 427 | public function getOrganizationName() |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * Gets the Permissions of an organization |
||
| 435 | * |
||
| 436 | * @return PermissionsInterface |
||
| 437 | */ |
||
| 438 | public function getPermissions() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Sets the Permissions of an Organization |
||
| 453 | * |
||
| 454 | * @param PermissionsInterface $permissions |
||
| 455 | * |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | View Code Duplication | public function setPermissions(PermissionsInterface $permissions) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Gets the Permissions Resource ID |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | public function getPermissionsResourceId() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param null $type |
||
| 481 | * |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | public function getPermissionsUserIds($type = null) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Sets the logo of an organization |
||
| 527 | * |
||
| 528 | * @param OrganizationImage $image |
||
| 529 | * |
||
| 530 | * @return self |
||
| 531 | */ |
||
| 532 | public function setImage(OrganizationImage $image = null) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Gets the Logo of an organization |
||
| 541 | * |
||
| 542 | * @return OrganizationImage |
||
| 543 | */ |
||
| 544 | public function getImage() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param ImageSet $images |
||
| 551 | * |
||
| 552 | * @return self |
||
| 553 | */ |
||
| 554 | public function setImages(ImageSet $images) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @return \Organizations\Entity\Images |
||
| 563 | */ |
||
| 564 | public function getImages() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * |
||
| 575 | * |
||
| 576 | * @return self |
||
| 577 | */ |
||
| 578 | public function removeImages() |
||
| 584 | |||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * Sets the Contact Data of an organization |
||
| 589 | * |
||
| 590 | * @param EntityInterface $contact |
||
| 591 | * |
||
| 592 | * @return $this |
||
| 593 | */ |
||
| 594 | public function setContact(EntityInterface $contact = null) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Gets the contact Data of an organization |
||
| 606 | * |
||
| 607 | * @return OrganizationContact |
||
| 608 | */ |
||
| 609 | public function getContact() |
||
| 617 | |||
| 618 | |||
| 619 | /** |
||
| 620 | * Gets the default description of an organization. |
||
| 621 | * |
||
| 622 | * This description is used as the default of the company_description |
||
| 623 | * used in a job template |
||
| 624 | * |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function getDescription() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Set the default description af an organization |
||
| 634 | * |
||
| 635 | * @param string $description |
||
| 636 | * |
||
| 637 | * @return $this |
||
| 638 | */ |
||
| 639 | public function setDescription($description) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Sets the the list of employees |
||
| 648 | * |
||
| 649 | * @param Collection $employees |
||
| 650 | * |
||
| 651 | * @return $this |
||
| 652 | */ |
||
| 653 | public function setEmployees(Collection $employees) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Gets the list of employees |
||
| 665 | * |
||
| 666 | * @return ArrayCollection|Collection |
||
| 667 | */ |
||
| 668 | public function getEmployees() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Gets an employee by User or ID. |
||
| 684 | * |
||
| 685 | * @param UserInterface|string $userOrId |
||
| 686 | * |
||
| 687 | * @return mixed|null |
||
| 688 | */ |
||
| 689 | public function getEmployee($userOrId) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Gets a list of Employees by a user role |
||
| 705 | * |
||
| 706 | * @param string $role |
||
| 707 | * |
||
| 708 | * @return ArrayCollection |
||
| 709 | */ |
||
| 710 | public function getEmployeesByRole($role) |
||
| 723 | |||
| 724 | |||
| 725 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Gets the owner of the organization |
||
| 738 | * |
||
| 739 | * @return UserInterface |
||
| 740 | */ |
||
| 741 | public function getUser() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Gets the Jobs of an organization |
||
| 748 | * |
||
| 749 | * @return Collection |
||
| 750 | */ |
||
| 751 | public function getJobs() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Gets default values of an organizations job template |
||
| 758 | * |
||
| 759 | * @return TemplateInterface |
||
| 760 | */ |
||
| 761 | public function getTemplate() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Sets default values of an organizations job template |
||
| 772 | * |
||
| 773 | * @return self |
||
| 774 | */ |
||
| 775 | public function setTemplate(TemplateInterface $template) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Gets Workflow Settings |
||
| 784 | * |
||
| 785 | * @return WorkflowSettings|WorkflowSettingsInterface |
||
| 786 | */ |
||
| 787 | public function getWorkflowSettings() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Sets Workflow Settings |
||
| 798 | * |
||
| 799 | * @param $workflowSettings |
||
| 800 | * |
||
| 801 | * @return self |
||
| 802 | */ |
||
| 803 | public function setWorkflowSettings($workflowSettings) |
||
| 809 | } |
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.