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 |
||
| 30 | class OrganizationReference implements |
||
| 31 | OrganizationInterface, |
||
| 32 | OrganizationReferenceInterface |
||
| 33 | { |
||
| 34 | /* |
||
| 35 | * Note: We start property names with an underscore to prevent |
||
| 36 | * name collisions with the OrganizationInterface. |
||
| 37 | */ |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The user id of the parent document. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $_userId; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The organization repository. |
||
| 48 | * |
||
| 49 | * @var \Organizations\Repository\Organization |
||
| 50 | */ |
||
| 51 | protected $_repository; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The organization |
||
| 55 | * |
||
| 56 | * @var Organization |
||
| 57 | */ |
||
| 58 | protected $_organization; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Reference type. |
||
| 62 | * |
||
| 63 | * @internal |
||
| 64 | * Initial value is null, so we can determine in {@link load()} if it has already run once. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $_type; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Creates an instance. |
||
| 72 | * |
||
| 73 | * @param string $userId |
||
| 74 | * @param OrganizationRepository $repository |
||
| 75 | */ |
||
| 76 | public function __construct($userId, OrganizationRepository $repository) |
||
| 81 | |||
| 82 | public function isOwner() |
||
| 88 | |||
| 89 | public function isEmployee() |
||
| 95 | |||
| 96 | public function hasAssociation() |
||
| 102 | |||
| 103 | public function getOrganization() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Loads the organization from the database and sets the reference type. |
||
| 112 | */ |
||
| 113 | protected function load() |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Executes a proxy call to the associated organization entity. |
||
| 146 | * |
||
| 147 | * Does nothing, if no organization is available. |
||
| 148 | * |
||
| 149 | * Call it like: |
||
| 150 | * <pre> |
||
| 151 | * $this->proxy($method[, $arg1[, $arg2[, ...]]]); |
||
| 152 | * </pre> |
||
| 153 | * |
||
| 154 | * @param $method |
||
| 155 | * |
||
| 156 | * @return self|mixed |
||
| 157 | */ |
||
| 158 | protected function proxy($method) |
||
| 170 | |||
| 171 | /* |
||
| 172 | * We need to implement each method of OrganizationInterface because we want to proxy |
||
| 173 | * to a concrete instance of Organization and therefor cannot simply extend. |
||
| 174 | */ |
||
| 175 | |||
| 176 | /**#@+ |
||
| 177 | * Proxies to the concrete Organization instance. |
||
| 178 | * |
||
| 179 | * {@inheritDoc} |
||
| 180 | */ |
||
| 181 | |||
| 182 | |||
| 183 | public function __get($property) |
||
| 187 | |||
| 188 | public function __set($property, $value) |
||
| 192 | |||
| 193 | public function __isset($property) |
||
| 197 | |||
| 198 | public function notEmpty($property, array $args=[]) |
||
| 202 | |||
| 203 | public function hasProperty($property, $mode = self::PROPERTY_STRICT) |
||
| 207 | |||
| 208 | public function setHydrator(HydratorInterface $hydrator) |
||
| 212 | |||
| 213 | public function getHydrator() |
||
| 217 | |||
| 218 | public function setId($id) |
||
| 222 | |||
| 223 | public function getId() |
||
| 227 | |||
| 228 | public function setDateCreated($date) |
||
| 232 | |||
| 233 | public function getDateCreated() |
||
| 237 | |||
| 238 | public function setDateModified($date) |
||
| 242 | |||
| 243 | public function getDateModified() |
||
| 247 | |||
| 248 | public function setParent(OrganizationInterface $parent) |
||
| 252 | |||
| 253 | public function getParent($returnSelf = false) |
||
| 257 | |||
| 258 | public function setContact(EntityInterface $contact = null) |
||
| 262 | |||
| 263 | public function getContact() |
||
| 267 | |||
| 268 | public function isHiringOrganization() |
||
| 272 | |||
| 273 | public function getHiringOrganizations() |
||
| 277 | |||
| 278 | public function setImage(OrganizationImage $image) |
||
| 282 | |||
| 283 | public function getImage() |
||
| 287 | |||
| 288 | public function getImages() |
||
| 292 | |||
| 293 | public function setImages(\Core\Entity\ImageSet $images) |
||
| 297 | |||
| 298 | public function setOrganizationName(OrganizationName $organizationNames) |
||
| 302 | |||
| 303 | public function getOrganizationName() |
||
| 307 | |||
| 308 | public function getDescription() |
||
| 312 | |||
| 313 | public function setDescription($description) |
||
| 317 | |||
| 318 | public function setEmployees(Collection $employees) |
||
| 322 | |||
| 323 | public function getEmployees() |
||
| 327 | |||
| 328 | public function getEmployeesByRole($role) |
||
| 332 | |||
| 333 | public function getEmployee($userOrId) |
||
| 337 | |||
| 338 | public function setExternalId($externalId) |
||
| 342 | |||
| 343 | public function getExternalId() |
||
| 347 | |||
| 348 | public function getUser() |
||
| 352 | |||
| 353 | public function setUser(UserInterface $user) |
||
| 358 | |||
| 359 | public function getJobs() |
||
| 363 | |||
| 364 | public function getPermissions() |
||
| 368 | |||
| 369 | public function setPermissions(PermissionsInterface $permissions) |
||
| 373 | |||
| 374 | public function getPermissionsResourceId() |
||
| 378 | |||
| 379 | public function getPermissionsUserIds($type = null) |
||
| 383 | |||
| 384 | public function getSearchableProperties() |
||
| 388 | |||
| 389 | public function setKeywords(array $keywords) |
||
| 393 | |||
| 394 | public function clearKeywords() |
||
| 398 | |||
| 399 | public function getTemplate() |
||
| 403 | |||
| 404 | public function setTemplate(TemplateInterface $template) |
||
| 408 | |||
| 409 | public function getWorkflowSettings() |
||
| 413 | |||
| 414 | public function setWorkflowSettings($workflowSettings) |
||
| 418 | |||
| 419 | /**#@-*/ |
||
| 420 | } |
||
| 421 |
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.