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 |
||
| 32 | class OrganizationRelationship implements ElementRelationshipInterface |
||
| 33 | { |
||
| 34 | use RelationshipTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var User |
||
| 38 | */ |
||
| 39 | private $user; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param User $user |
||
| 43 | */ |
||
| 44 | public function __construct(User $user) |
||
| 48 | |||
| 49 | |||
| 50 | /************************************************************ |
||
| 51 | * COLLECTION |
||
| 52 | ************************************************************/ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritDoc |
||
| 56 | * @return Organization[]|Collection |
||
| 57 | */ |
||
| 58 | public function getCollection(): Collection |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return Collection |
||
| 74 | */ |
||
| 75 | protected function createCollectionFromRelations() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritDoc |
||
| 105 | * @return Collection |
||
| 106 | */ |
||
| 107 | protected function existingRelationships(): Collection |
||
| 115 | |||
| 116 | |||
| 117 | /************************************************************ |
||
| 118 | * QUERY |
||
| 119 | ************************************************************/ |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritDoc |
||
| 123 | * @return OrganizationQuery |
||
| 124 | */ |
||
| 125 | public function getQuery(): ElementQueryInterface |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return UserAssociationQuery |
||
| 133 | */ |
||
| 134 | private function associationQuery(): UserAssociationQuery |
||
| 143 | |||
| 144 | |||
| 145 | /************************************************************ |
||
| 146 | * CREATE |
||
| 147 | ************************************************************/ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $object |
||
| 151 | * @return UserAssociation |
||
| 152 | */ |
||
| 153 | protected function create($object): UserAssociation |
||
| 163 | |||
| 164 | |||
| 165 | /******************************************* |
||
| 166 | * DELTA |
||
| 167 | *******************************************/ |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritDoc |
||
| 171 | */ |
||
| 172 | protected function delta(): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param UserAssociation $new |
||
| 206 | * @param UserAssociation $existing |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | private function hasChanged(UserAssociation $new, UserAssociation $existing): bool |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param UserAssociation $from |
||
| 219 | * @param UserAssociation $to |
||
| 220 | * |
||
| 221 | * @return UserAssociation |
||
| 222 | */ |
||
| 223 | private function sync(UserAssociation $to, UserAssociation $from): UserAssociation |
||
| 238 | |||
| 239 | /******************************************* |
||
| 240 | * COLLECTION UTILS |
||
| 241 | *******************************************/ |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Position the relationship based on the sort order |
||
| 245 | * |
||
| 246 | * @inheritDoc |
||
| 247 | */ |
||
| 248 | protected function insertCollection(Collection $collection, UserAssociation $association) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @inheritDoc |
||
| 262 | */ |
||
| 263 | protected function updateCollection(Collection $collection, UserAssociation $association) |
||
| 275 | |||
| 276 | |||
| 277 | /******************************************* |
||
| 278 | * UTILS |
||
| 279 | *******************************************/ |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param UserAssociation|Organization|int|array|null $object |
||
| 283 | * @return int|null |
||
| 284 | */ |
||
| 285 | protected function findKey($object = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $identifier |
||
| 300 | * @return int|string|null |
||
| 301 | */ |
||
| 302 | private function findRelationshipKey($identifier) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param UserAssociation|Organization|int|array $organization |
||
| 320 | * @return Organization|null |
||
| 321 | */ |
||
| 322 | protected function resolveObjectInternal($organization) |
||
| 334 | } |
||
| 335 |