Complex classes like UserRelationship 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 UserRelationship, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class UserRelationship implements ElementRelationshipInterface |
||
| 33 | { |
||
| 34 | use RelationshipTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Organization |
||
| 38 | */ |
||
| 39 | private $organization; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param Organization $organization |
||
| 43 | */ |
||
| 44 | public function __construct(Organization $organization) |
||
| 48 | |||
| 49 | |||
| 50 | /************************************************************ |
||
| 51 | * COLLECTION |
||
| 52 | ************************************************************/ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get a collection of associated users |
||
| 56 | * |
||
| 57 | * @return User[]|Collection |
||
| 58 | */ |
||
| 59 | public function getCollection(): Collection |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return Collection |
||
| 75 | */ |
||
| 76 | protected function createCollectionFromRelations() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return Collection |
||
| 106 | */ |
||
| 107 | protected function existingRelationships(): Collection |
||
| 115 | |||
| 116 | /************************************************************ |
||
| 117 | * QUERY |
||
| 118 | ************************************************************/ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritDoc |
||
| 122 | * @return UserQuery |
||
| 123 | */ |
||
| 124 | public function getQuery(): ElementQueryInterface |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return UserAssociationQuery |
||
| 132 | */ |
||
| 133 | private function associationQuery(): UserAssociationQuery |
||
| 143 | |||
| 144 | /************************************************************ |
||
| 145 | * CREATE |
||
| 146 | ************************************************************/ |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param $object |
||
| 150 | * @return UserAssociation |
||
| 151 | */ |
||
| 152 | protected function create($object): UserAssociation |
||
| 162 | |||
| 163 | |||
| 164 | /******************************************* |
||
| 165 | * DELTA |
||
| 166 | *******************************************/ |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritDoc |
||
| 170 | */ |
||
| 171 | protected function delta(): array |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param UserAssociation $new |
||
| 205 | * @param UserAssociation $existing |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | 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 | /******************************************* |
||
| 241 | * COLLECTION UTILS |
||
| 242 | *******************************************/ |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritDoc |
||
| 246 | */ |
||
| 247 | protected function insertCollection(Collection $collection, UserAssociation $association) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritDoc |
||
| 259 | */ |
||
| 260 | protected function updateCollection(Collection $collection, UserAssociation $association) |
||
| 272 | |||
| 273 | |||
| 274 | /******************************************* |
||
| 275 | * UTILS |
||
| 276 | *******************************************/ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param UserAssociation|User|int|array|null $object |
||
| 280 | * @return int|null |
||
| 281 | */ |
||
| 282 | protected function findKey($object = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param $identifier |
||
| 302 | * @return int|string|null |
||
| 303 | */ |
||
| 304 | private function findRelationshipKey($identifier) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param UserAssociation|User|int|array|null $user |
||
| 322 | * @return User|null |
||
| 323 | */ |
||
| 324 | protected function resolveObjectInternal($user) |
||
| 344 | } |
||
| 345 |