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 |
||
| 34 | class Organization extends BaseEntity implements OrganizationInterface, DraftableEntityInterface |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Event name of post construct event. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const postConstruct = 'postRepositoryConstruct'; |
||
|
|
|||
| 43 | |||
| 44 | /** |
||
| 45 | * externalId. Allows external applications to reference their primary key. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | * @ODM\String |
||
| 49 | * @ODM\Index |
||
| 50 | */ |
||
| 51 | protected $externalId; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The actual name of the organization. |
||
| 55 | * |
||
| 56 | * @var \Organizations\Entity\OrganizationName |
||
| 57 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", simple=true, cascade="persist") |
||
| 58 | */ |
||
| 59 | protected $organizationName; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Assigned permissions. |
||
| 63 | * |
||
| 64 | * @var PermissionsInterface |
||
| 65 | * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions") |
||
| 66 | */ |
||
| 67 | protected $permissions; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * primary logo of an organization |
||
| 71 | * |
||
| 72 | * @var \Organizations\Entity\OrganizationImage |
||
| 73 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", simple=true, nullable="true", cascade={"all"}) |
||
| 74 | */ |
||
| 75 | protected $image; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Flag indicating draft state of this job. |
||
| 79 | * |
||
| 80 | * @var bool |
||
| 81 | * @ODM\Boolean |
||
| 82 | */ |
||
| 83 | protected $isDraft = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Organization contact data. |
||
| 87 | * |
||
| 88 | * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact") */ |
||
| 89 | protected $contact; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The organizations' description. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | * @ODM\String |
||
| 96 | */ |
||
| 97 | protected $description; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The parent of this organization. |
||
| 101 | * |
||
| 102 | * @see setParent() |
||
| 103 | * @var OrganizationInterface | null |
||
| 104 | * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", simple=true, nullable=true) |
||
| 105 | * @since 0.18 |
||
| 106 | */ |
||
| 107 | protected $parent; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The hiring organizations of this organization. |
||
| 111 | * |
||
| 112 | * @var Collection |
||
| 113 | * @ODM\ReferenceMany( |
||
| 114 | * targetDocument="Organizations\Entity\Organization", |
||
| 115 | * repositoryMethod="getHiringOrganizationsCursor" |
||
| 116 | * ) |
||
| 117 | * @since 0.18 |
||
| 118 | */ |
||
| 119 | protected $hiringOrganizations; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The associated employees (users) |
||
| 123 | * |
||
| 124 | * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee") |
||
| 125 | * @var Collection |
||
| 126 | */ |
||
| 127 | protected $employees; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Jobs of this organization. |
||
| 131 | * |
||
| 132 | * @var Collection |
||
| 133 | * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", simple=true, mappedBy="organization") |
||
| 134 | * @since 0.18 |
||
| 135 | */ |
||
| 136 | protected $jobs; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * the owner of a Organization |
||
| 140 | * |
||
| 141 | * @var UserInterface $user |
||
| 142 | * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", simple=true) |
||
| 143 | * @ODM\Index |
||
| 144 | */ |
||
| 145 | protected $user; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Default values of an organizations job template |
||
| 149 | * |
||
| 150 | * @var TemplateInterface; |
||
| 151 | */ |
||
| 152 | protected $template; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param OrganizationInterface $parent |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function setParent(OrganizationInterface $parent) |
||
| 165 | |||
| 166 | public function getParent() |
||
| 170 | |||
| 171 | public function getHiringOrganizations() |
||
| 175 | |||
| 176 | public function isHiringOrganization() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets the external id. |
||
| 183 | * |
||
| 184 | * @todo Has to be in interface! |
||
| 185 | * @param $externalId |
||
| 186 | * |
||
| 187 | * @return self |
||
| 188 | */ |
||
| 189 | public function setExternalId($externalId) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Gets the internal id. |
||
| 197 | * @todo has to be in interface! |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getExternalId() |
||
| 205 | |||
| 206 | public function setHydrator(HydratorInterface $hydrator) |
||
| 210 | |||
| 211 | public function getHydrator() |
||
| 215 | |||
| 216 | public function setOrganizationName(OrganizationName $organizationName) |
||
| 225 | |||
| 226 | public function getOrganizationName() |
||
| 230 | |||
| 231 | |||
| 232 | public function getName() |
||
| 239 | |||
| 240 | public function getSearchableProperties() |
||
| 244 | |||
| 245 | public function setKeywords(array $keywords) |
||
| 248 | |||
| 249 | public function clearKeywords() |
||
| 252 | |||
| 253 | public function getKeywords() |
||
| 256 | |||
| 257 | public function getPermissions() |
||
| 268 | |||
| 269 | View Code Duplication | public function setPermissions(PermissionsInterface $permissions) |
|
| 278 | |||
| 279 | public function getPermissionsResourceId() |
||
| 283 | |||
| 284 | public function getPermissionsUserIds($type = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sets logo. |
||
| 325 | * |
||
| 326 | * @todo has to be in interface |
||
| 327 | * @param OrganizationImage $image |
||
| 328 | * |
||
| 329 | * @return self |
||
| 330 | */ |
||
| 331 | public function setImage(OrganizationImage $image = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * gets image |
||
| 339 | * |
||
| 340 | * @todo has to be in interface |
||
| 341 | * @return OrganizationImage |
||
| 342 | */ |
||
| 343 | public function getImage() |
||
| 347 | |||
| 348 | public function setContact(EntityInterface $contact = null) |
||
| 356 | |||
| 357 | public function getContact() |
||
| 364 | |||
| 365 | public function isDraft() |
||
| 369 | |||
| 370 | public function setIsDraft($flag) |
||
| 375 | |||
| 376 | public function getDescription() |
||
| 380 | |||
| 381 | public function setDescription($description) |
||
| 387 | |||
| 388 | public function setEmployees(Collection $employees) |
||
| 397 | |||
| 398 | public function getEmployees() |
||
| 411 | |||
| 412 | public function getEmployee($userOrId) |
||
| 425 | |||
| 426 | public function isOwner(UserInterface $user) |
||
| 430 | |||
| 431 | public function isEmployee(UserInterface $user) |
||
| 435 | |||
| 436 | public function isAssociated(UserInterface $user) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Updates the organizationsPermissions to allow all employees to view this organization. |
||
| 443 | * |
||
| 444 | * In case of a HiringOrganization Permissions are granted to all employees of the parent |
||
| 445 | * organization. |
||
| 446 | * |
||
| 447 | * @ODM\PreUpdate |
||
| 448 | * @ODM\PrePersist |
||
| 449 | * @since 0.18 |
||
| 450 | */ |
||
| 451 | public function updatePermissions() |
||
| 474 | |||
| 475 | View Code Duplication | public function setUser(UserInterface $user) |
|
| 484 | |||
| 485 | public function getUser() |
||
| 489 | |||
| 490 | public function getJobs() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Gets default values of an organizations job template |
||
| 497 | * |
||
| 498 | * @return TemplateInterface |
||
| 499 | */ |
||
| 500 | public function getTemplate() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Sets default values of an organizations job template |
||
| 510 | * |
||
| 511 | * @return self |
||
| 512 | */ |
||
| 513 | public function setTemplate(TemplateInterface $template) |
||
| 517 | |||
| 518 | |||
| 519 | } |
||
| 520 |