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 |
||
| 39 | class Organization extends BaseEntity implements |
||
|
|
|||
| 40 | OrganizationInterface, |
||
| 41 | DraftableEntityInterface, |
||
| 42 | ResourceInterface |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * Event name of post construct event. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | const postConstruct = 'postRepositoryConstruct'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * externalId. Allows external applications to reference their primary key. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | * @ODM\Field(type="string") |
||
| 56 | * @ODM\Index |
||
| 57 | */ |
||
| 58 | protected $externalId; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The actual name of the organization. |
||
| 62 | * |
||
| 63 | * @var \Organizations\Entity\OrganizationName |
||
| 64 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 65 | */ |
||
| 66 | protected $organizationName; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Only for sorting/searching purposes |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | * @ODM\Field(type="string") |
||
| 73 | */ |
||
| 74 | protected $_organizationName; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Assigned permissions. |
||
| 78 | * |
||
| 79 | * @var PermissionsInterface |
||
| 80 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 81 | */ |
||
| 82 | protected $permissions; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * primary logo of an organization |
||
| 86 | * |
||
| 87 | * @var \Organizations\Entity\OrganizationImage |
||
| 88 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 89 | */ |
||
| 90 | protected $image; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Flag indicating draft state of this job. |
||
| 94 | * |
||
| 95 | * @var bool |
||
| 96 | * @ODM\Boolean |
||
| 97 | */ |
||
| 98 | protected $isDraft = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Organization contact data. |
||
| 102 | * |
||
| 103 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") |
||
| 104 | */ |
||
| 105 | protected $contact; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The organizations' description. |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | * @ODM\Field(type="string") |
||
| 112 | */ |
||
| 113 | protected $description; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The parent of this organization. |
||
| 117 | * |
||
| 118 | * @see setParent() |
||
| 119 | * @var OrganizationInterface | null |
||
| 120 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 121 | * @since 0.18 |
||
| 122 | */ |
||
| 123 | protected $parent; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The hiring organizations of this organization. |
||
| 127 | * |
||
| 128 | * @var Collection |
||
| 129 | * @ODM\ReferenceMany( |
||
| 130 | * targetDocument="Organizations\Entity\Organization", |
||
| 131 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 132 | * ) |
||
| 133 | * @since 0.18 |
||
| 134 | */ |
||
| 135 | protected $hiringOrganizations; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * The associated employees (users) |
||
| 139 | * |
||
| 140 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 141 | * @var Collection |
||
| 142 | */ |
||
| 143 | protected $employees; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Jobs of this organization. |
||
| 147 | * |
||
| 148 | * @var Collection |
||
| 149 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 150 | * @since 0.18 |
||
| 151 | */ |
||
| 152 | protected $jobs; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * the owner of a Organization |
||
| 156 | * |
||
| 157 | * @var UserInterface $user |
||
| 158 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 159 | * @ODM\Index |
||
| 160 | */ |
||
| 161 | protected $user; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Default values of an organizations job template |
||
| 165 | * |
||
| 166 | * @var TemplateInterface; |
||
| 167 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template") |
||
| 168 | */ |
||
| 169 | protected $template; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Default values Workflow |
||
| 173 | * |
||
| 174 | * @var WorkflowSettingsInterface $workflowSettings ; |
||
| 175 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\WorkflowSettings") |
||
| 176 | */ |
||
| 177 | protected $workflowSettings; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the string identifier of the Resource |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getResourceId() |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the organization name |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getName() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Sets the parent of an organization |
||
| 206 | * |
||
| 207 | * @param OrganizationInterface $parent |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setParent(OrganizationInterface $parent) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @deprecated |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public function getSearchableProperties() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets the parent of an organization |
||
| 229 | * |
||
| 230 | * @param bool $returnSelf returns itself, if this organization does not have a parent? |
||
| 231 | * |
||
| 232 | * @return null|OrganizationInterface |
||
| 233 | */ |
||
| 234 | public function getParent($returnSelf = false) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Gets the Draft flag |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function isDraft() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Gets linked organizations |
||
| 251 | * |
||
| 252 | * @return Collection |
||
| 253 | */ |
||
| 254 | public function getHiringOrganizations() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets the draft flag |
||
| 261 | * |
||
| 262 | * @param bool $flag |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function setIsDraft($flag) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function isHiringOrganization() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns true, if the user belongs to the organization. |
||
| 283 | * |
||
| 284 | * @param UserInterface $user |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function isAssociated(UserInterface $user) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets the external id. |
||
| 295 | * |
||
| 296 | * @param $externalId |
||
| 297 | * |
||
| 298 | * @return self |
||
| 299 | */ |
||
| 300 | public function setExternalId($externalId) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Checks, if a User is the owner of an organization |
||
| 309 | * |
||
| 310 | * @param UserInterface $user |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function isOwner(UserInterface $user) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Gets the internal id. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getExternalId() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Returns true, if a User is an employee of the organization |
||
| 331 | * |
||
| 332 | * @param UserInterface $user |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function isEmployee(UserInterface $user) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @todo review this |
||
| 343 | * |
||
| 344 | * @param HydratorInterface $hydrator |
||
| 345 | * |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function setHydrator(HydratorInterface $hydrator) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 355 | * |
||
| 356 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 357 | * organization. |
||
| 358 | * |
||
| 359 | * @ODM\PreUpdate |
||
| 360 | * @ODM\PrePersist |
||
| 361 | * @since 0.18 |
||
| 362 | */ |
||
| 363 | public function updatePermissions() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * * @todo review this |
||
| 388 | * @return EntityHydrator |
||
| 389 | */ |
||
| 390 | public function getHydrator() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Sets the name of an organization |
||
| 397 | * |
||
| 398 | * @param OrganizationName $organizationName |
||
| 399 | * |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Gets the organization name entity of an organisation |
||
| 416 | * |
||
| 417 | * @return OrganizationName |
||
| 418 | */ |
||
| 419 | public function getOrganizationName() |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the Permissions of an organization |
||
| 427 | * |
||
| 428 | * @return PermissionsInterface |
||
| 429 | */ |
||
| 430 | public function getPermissions() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Sets the Permissions of an Organization |
||
| 445 | * |
||
| 446 | * @param PermissionsInterface $permissions |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | View Code Duplication | public function setPermissions(PermissionsInterface $permissions) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Gets the Permissions Resource ID |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | public function getPermissionsResourceId() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param null $type |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function getPermissionsUserIds($type = null) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Sets the logo of an organization |
||
| 519 | * |
||
| 520 | * @param OrganizationImage $image |
||
| 521 | * |
||
| 522 | * @return self |
||
| 523 | */ |
||
| 524 | public function setImage(OrganizationImage $image = null) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Gets the Logo of an organization |
||
| 533 | * |
||
| 534 | * @return OrganizationImage |
||
| 535 | */ |
||
| 536 | public function getImage() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Sets the Contact Data of an organization |
||
| 543 | * |
||
| 544 | * @param EntityInterface $contact |
||
| 545 | * |
||
| 546 | * @return $this |
||
| 547 | */ |
||
| 548 | public function setContact(EntityInterface $contact = null) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Gets the contact Data of an organization |
||
| 560 | * |
||
| 561 | * @return OrganizationContact |
||
| 562 | */ |
||
| 563 | public function getContact() |
||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Gets the default description of an organization. |
||
| 575 | * |
||
| 576 | * This description is used as the default of the company_description |
||
| 577 | * used in a job template |
||
| 578 | * |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | public function getDescription() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Set the default description af an organization |
||
| 588 | * |
||
| 589 | * @param string $description |
||
| 590 | * |
||
| 591 | * @return $this |
||
| 592 | */ |
||
| 593 | public function setDescription($description) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Sets the the list of employees |
||
| 602 | * |
||
| 603 | * @param Collection $employees |
||
| 604 | * |
||
| 605 | * @return $this |
||
| 606 | */ |
||
| 607 | public function setEmployees(Collection $employees) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Gets the list of employees |
||
| 619 | * |
||
| 620 | * @return ArrayCollection|Collection |
||
| 621 | */ |
||
| 622 | public function getEmployees() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Gets an employee by User or ID. |
||
| 638 | * |
||
| 639 | * @param UserInterface|string $userOrId |
||
| 640 | * |
||
| 641 | * @return mixed|null |
||
| 642 | */ |
||
| 643 | public function getEmployee($userOrId) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Gets a list of Employees by a user role |
||
| 659 | * |
||
| 660 | * @param string $role |
||
| 661 | * |
||
| 662 | * @return ArrayCollection |
||
| 663 | */ |
||
| 664 | public function getEmployeesByRole($role) |
||
| 677 | |||
| 678 | |||
| 679 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Gets the owner of the organization |
||
| 692 | * |
||
| 693 | * @return UserInterface |
||
| 694 | */ |
||
| 695 | public function getUser() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Gets the Jobs of an organization |
||
| 702 | * |
||
| 703 | * @return Collection |
||
| 704 | */ |
||
| 705 | public function getJobs() |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Gets default values of an organizations job template |
||
| 712 | * |
||
| 713 | * @return TemplateInterface |
||
| 714 | */ |
||
| 715 | public function getTemplate() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Sets default values of an organizations job template |
||
| 726 | * |
||
| 727 | * @return self |
||
| 728 | */ |
||
| 729 | public function setTemplate(TemplateInterface $template) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Gets Workflow Settings |
||
| 738 | * |
||
| 739 | * @return WorkflowSettings|WorkflowSettingsInterface |
||
| 740 | */ |
||
| 741 | public function getWorkflowSettings() |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Sets Workflow Settings |
||
| 752 | * |
||
| 753 | * @param $workflowSettings |
||
| 754 | * |
||
| 755 | * @return self |
||
| 756 | */ |
||
| 757 | public function setWorkflowSettings($workflowSettings) |
||
| 763 | } |
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.