Complex classes like OrganizationRelationship 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 OrganizationRelationship, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class OrganizationRelationship implements RelationshipInterface |
||
| 31 | { |
||
| 32 | use RelationshipTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var User |
||
| 36 | */ |
||
| 37 | private $user; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param User $user |
||
| 41 | */ |
||
| 42 | public function __construct(User $user) |
||
| 46 | |||
| 47 | |||
| 48 | /************************************************************ |
||
| 49 | * COLLECTION |
||
| 50 | ************************************************************/ |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritDoc |
||
| 54 | * @return Organization[]|Collection |
||
| 55 | */ |
||
| 56 | public function getCollection(): Collection |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return Collection |
||
| 70 | */ |
||
| 71 | protected function createCollectionFromRelations() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritDoc |
||
| 99 | * @return Collection |
||
| 100 | */ |
||
| 101 | protected function existingRelationships(): Collection |
||
| 109 | |||
| 110 | /************************************************************ |
||
| 111 | * QUERY |
||
| 112 | ************************************************************/ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return OrganizationQuery |
||
| 116 | */ |
||
| 117 | private function elementQuery(): OrganizationQuery |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return UserAssociationQuery |
||
| 127 | */ |
||
| 128 | private function associationQuery(): UserAssociationQuery |
||
| 137 | |||
| 138 | |||
| 139 | /************************************************************ |
||
| 140 | * CREATE |
||
| 141 | ************************************************************/ |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $object |
||
| 145 | * @return UserAssociation |
||
| 146 | */ |
||
| 147 | protected function create($object): UserAssociation |
||
| 157 | |||
| 158 | |||
| 159 | /******************************************* |
||
| 160 | * DELTA |
||
| 161 | *******************************************/ |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritDoc |
||
| 165 | */ |
||
| 166 | protected function delta(): array |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param UserAssociation $new |
||
| 200 | * @param UserAssociation $existing |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | private function hasChanged(UserAssociation $new, UserAssociation $existing): bool |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param UserAssociation $from |
||
| 213 | * @param UserAssociation $to |
||
| 214 | * |
||
| 215 | * @return UserAssociation |
||
| 216 | */ |
||
| 217 | private function sync(UserAssociation $to, UserAssociation $from): UserAssociation |
||
| 232 | |||
| 233 | /******************************************* |
||
| 234 | * COLLECTION UTILS |
||
| 235 | *******************************************/ |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Position the relationship based on the sort order |
||
| 239 | * |
||
| 240 | * @inheritDoc |
||
| 241 | */ |
||
| 242 | protected function insertCollection(Collection $collection, UserAssociation $association) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @inheritDoc |
||
| 254 | */ |
||
| 255 | protected function updateCollection(Collection $collection, UserAssociation $association) |
||
| 267 | |||
| 268 | |||
| 269 | /******************************************* |
||
| 270 | * UTILS |
||
| 271 | *******************************************/ |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param UserAssociation|Organization|int|array|null $object |
||
| 275 | * @return int|null |
||
| 276 | */ |
||
| 277 | protected function findKey($object = null) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $identifier |
||
| 292 | * @return int|string|null |
||
| 293 | */ |
||
| 294 | private function findRelationshipKey($identifier) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param UserAssociation|Organization|int|array $organization |
||
| 308 | * @return Organization|null |
||
| 309 | */ |
||
| 310 | protected function resolveObjectInternal($organization) |
||
| 322 | } |
||
| 323 |