Complex classes like OrganizationReference 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 OrganizationReference, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class OrganizationReference implements |
||
| 32 | OrganizationInterface, |
||
| 33 | OrganizationReferenceInterface |
||
| 34 | { |
||
| 35 | /* |
||
| 36 | * Note: We start property names with an underscore to prevent |
||
| 37 | * name collisions with the OrganizationInterface. |
||
| 38 | */ |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The user id of the parent document. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $_userId; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The organization repository. |
||
| 49 | * |
||
| 50 | * @var \Organizations\Repository\Organization |
||
| 51 | */ |
||
| 52 | protected $_repository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The organization |
||
| 56 | * |
||
| 57 | * @var Organization |
||
| 58 | */ |
||
| 59 | protected $_organization; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Reference type. |
||
| 63 | * |
||
| 64 | * @internal |
||
| 65 | * Initial value is null, so we can determine in {@link load()} if it has already run once. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $_type; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Creates an instance. |
||
| 73 | * |
||
| 74 | * @param string $userId |
||
| 75 | * @param OrganizationRepository $repository |
||
| 76 | */ |
||
| 77 | public function __construct($userId, OrganizationRepository $repository) |
||
| 82 | |||
| 83 | public function isOwner() |
||
| 89 | |||
| 90 | public function isEmployee() |
||
| 96 | |||
| 97 | public function hasAssociation() |
||
| 103 | |||
| 104 | public function getOrganization() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Loads the organization from the database and sets the reference type. |
||
| 113 | */ |
||
| 114 | protected function load() |
||
| 115 | { |
||
| 116 | if (null !== $this->_type) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | // Is the user the owner of the referenced organization? |
||
| 121 | $org = $this->_repository->findByUser($this->_userId); |
||
| 122 | |||
| 123 | if ($org) { |
||
| 124 | $this->_type = self::TYPE_OWNER; |
||
| 125 | $this->_organization = $org; |
||
| 126 | |||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | // Is the user employed by the referenced organization? |
||
| 131 | $org = $this->_repository->findByEmployee($this->_userId); |
||
| 132 | |||
| 133 | if ($org) { |
||
| 134 | $this->_type = self::TYPE_EMPLOYEE; |
||
| 135 | $this->_organization = $org; |
||
| 136 | |||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | // It seems the user is not associated with an organization. |
||
| 141 | $this->_type = self::TYPE_NONE; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Executes a proxy call to the associated organization entity. |
||
| 147 | * |
||
| 148 | * Does nothing, if no organization is available. |
||
| 149 | * |
||
| 150 | * Call it like: |
||
| 151 | * <pre> |
||
| 152 | * $this->proxy($method[, $arg1[, $arg2[, ...]]]); |
||
| 153 | * </pre> |
||
| 154 | * |
||
| 155 | * @param $method |
||
| 156 | * |
||
| 157 | * @return self|mixed |
||
| 158 | */ |
||
| 159 | protected function proxy($method) |
||
| 171 | |||
| 172 | /* |
||
| 173 | * We need to implement each method of OrganizationInterface because we want to proxy |
||
| 174 | * to a concrete instance of Organization and therefor cannot simply extend. |
||
| 175 | */ |
||
| 176 | |||
| 177 | /**#@+ |
||
| 178 | * Proxies to the concrete Organization instance. |
||
| 179 | * |
||
| 180 | * {@inheritDoc} |
||
| 181 | */ |
||
| 182 | |||
| 183 | |||
| 184 | public function __get($property) |
||
| 188 | |||
| 189 | public function __set($property, $value) |
||
| 193 | |||
| 194 | public function __isset($property) |
||
| 198 | |||
| 199 | public function notEmpty($property, array $args=[]) |
||
| 203 | |||
| 204 | public function hasProperty($property, $mode = self::PROPERTY_STRICT) |
||
| 208 | |||
| 209 | public function setHydrator(HydratorInterface $hydrator) |
||
| 213 | |||
| 214 | public function getHydrator() |
||
| 218 | |||
| 219 | public function setId($id) |
||
| 223 | |||
| 224 | public function getId() |
||
| 228 | |||
| 229 | public function setDateCreated(DateTime $date) |
||
| 233 | |||
| 234 | public function getDateCreated() |
||
| 238 | |||
| 239 | public function setDateModified($date) |
||
| 243 | |||
| 244 | public function getDateModified() |
||
| 248 | |||
| 249 | public function setParent(OrganizationInterface $parent) |
||
| 253 | |||
| 254 | public function getParent() |
||
| 258 | |||
| 259 | public function setContact(EntityInterface $contact = null) |
||
| 263 | |||
| 264 | public function getContact() |
||
| 268 | |||
| 269 | public function isHiringOrganization() |
||
| 273 | |||
| 274 | public function getHiringOrganizations() |
||
| 278 | |||
| 279 | public function setImage(OrganizationImage $image) |
||
| 283 | |||
| 284 | public function getImage() |
||
| 288 | |||
| 289 | public function setOrganizationName(OrganizationName $organizationNames) |
||
| 293 | |||
| 294 | public function getOrganizationName() |
||
| 298 | |||
| 299 | public function getDescription() |
||
| 303 | |||
| 304 | public function setDescription($description) |
||
| 308 | |||
| 309 | public function setEmployees(Collection $employees) |
||
| 313 | |||
| 314 | public function getEmployees() |
||
| 318 | |||
| 319 | public function getEmployeesByRole($role) |
||
| 323 | |||
| 324 | public function getEmployee($userOrId) |
||
| 328 | |||
| 329 | public function setExternalId($externalId) |
||
| 333 | |||
| 334 | public function getExternalId() |
||
| 338 | |||
| 339 | public function getUser() |
||
| 343 | |||
| 344 | public function setUser(UserInterface $user) |
||
| 349 | |||
| 350 | public function getJobs() |
||
| 354 | |||
| 355 | public function getPermissions() |
||
| 359 | |||
| 360 | public function setPermissions(PermissionsInterface $permissions) |
||
| 364 | |||
| 365 | public function getPermissionsResourceId() |
||
| 369 | |||
| 370 | public function getPermissionsUserIds($type = null) |
||
| 374 | |||
| 375 | public function getSearchableProperties() |
||
| 379 | |||
| 380 | public function setKeywords(array $keywords) |
||
| 384 | |||
| 385 | public function clearKeywords() |
||
| 389 | |||
| 390 | public function getKeywords() |
||
| 394 | |||
| 395 | public function getTemplate() |
||
| 399 | |||
| 400 | public function setTemplate(TemplateInterface $template) |
||
| 404 | |||
| 405 | |||
| 406 | /**#@-*/ |
||
| 407 | } |
||
| 408 |
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.