Complex classes like RelationshipTrait 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 RelationshipTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | trait RelationshipTrait |
||
| 27 | { |
||
| 28 | use MutatedTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Collection|null |
||
| 32 | */ |
||
| 33 | protected $collection; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param null $object |
||
| 37 | * @return int|null |
||
| 38 | */ |
||
| 39 | abstract protected function findKey($object = null); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param array $criteria |
||
| 43 | * @return QueryInterface |
||
| 44 | */ |
||
| 45 | abstract protected function query(array $criteria = []): QueryInterface; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param $object |
||
| 49 | * @return ActiveRecord |
||
| 50 | */ |
||
| 51 | abstract protected function create($object): ActiveRecord; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return ActiveRecord[][] |
||
| 55 | */ |
||
| 56 | abstract protected function associationDelta(): array; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | abstract protected function handleAssociationError(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 65 | * @return ActiveRecord |
||
| 66 | */ |
||
| 67 | public function findOrCreate($object): ActiveRecord |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 78 | * @return ActiveRecord |
||
| 79 | * @throws Exception |
||
| 80 | */ |
||
| 81 | public function findOrFail($object): ActiveRecord |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return Collection |
||
| 92 | * |
||
| 93 | * @deprecated use `getCollection()` |
||
| 94 | */ |
||
| 95 | public function findAll(): Collection |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param ActiveRecord|ElementInterface|int|string|null $object |
||
| 102 | * @return ActiveRecord|null |
||
| 103 | */ |
||
| 104 | public function findOne($object = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function exists($object): bool |
||
| 121 | |||
| 122 | |||
| 123 | /************************************************************ |
||
| 124 | * COLLECTIONS |
||
| 125 | ************************************************************/ |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritDoc |
||
| 129 | */ |
||
| 130 | public function getRelationships(): Collection |
||
| 140 | |||
| 141 | |||
| 142 | /************************************************************ |
||
| 143 | * SET |
||
| 144 | ************************************************************/ |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param QueryInterface|ElementInterface[] $objects |
||
| 148 | * @param array $attributes |
||
| 149 | * @return RelationshipInterface |
||
| 150 | * |
||
| 151 | * @deprecated use `reset()->add($objects, $attributes)` |
||
| 152 | */ |
||
| 153 | public function setMany($objects, array $attributes = []): RelationshipInterface |
||
| 158 | |||
| 159 | |||
| 160 | /************************************************************ |
||
| 161 | * ADD |
||
| 162 | ************************************************************/ |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param QueryInterface|ElementInterface[] $objects |
||
| 166 | * @param array $attributes |
||
| 167 | * @return RelationshipInterface |
||
| 168 | * |
||
| 169 | * @deprecated use `add($objects, $attributes)` |
||
| 170 | */ |
||
| 171 | public function addMany($objects, array $attributes = []): RelationshipInterface |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Associate a user to an organization |
||
| 178 | * |
||
| 179 | * @param ActiveRecord|ElementInterface|int|array $object |
||
| 180 | * @param array $attributes |
||
| 181 | * @return RelationshipInterface |
||
| 182 | * |
||
| 183 | * @deprecated use `add($objects, $attributes)` |
||
| 184 | */ |
||
| 185 | public function addOne($object, array $attributes = []): RelationshipInterface |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Add one or many object relations (but do not save) |
||
| 192 | * |
||
| 193 | * @param $objects |
||
| 194 | * @param array $attributes |
||
| 195 | * @return RelationshipInterface |
||
| 196 | */ |
||
| 197 | public function add($objects, array $attributes = []): RelationshipInterface |
||
| 217 | |||
| 218 | |||
| 219 | /************************************************************ |
||
| 220 | * REMOVE |
||
| 221 | ************************************************************/ |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Dissociate an array of user associations from an organization |
||
| 225 | * |
||
| 226 | * @param QueryInterface|ElementInterface[] $objects |
||
| 227 | * @return RelationshipInterface |
||
| 228 | * |
||
| 229 | * @deprecated use `remove($objects)` |
||
| 230 | */ |
||
| 231 | public function removeMany($objects): RelationshipInterface |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Dissociate a user from an organization |
||
| 238 | * |
||
| 239 | * @param ActiveRecord|ElementInterface|int|array |
||
| 240 | * @return RelationshipInterface |
||
| 241 | * |
||
| 242 | * @deprecated use `remove($objects)` |
||
| 243 | */ |
||
| 244 | public function removeOne($object): RelationshipInterface |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param $objects |
||
| 251 | * @return RelationshipInterface |
||
| 252 | */ |
||
| 253 | public function remove($objects): RelationshipInterface |
||
| 263 | |||
| 264 | |||
| 265 | /************************************************************ |
||
| 266 | * RESET |
||
| 267 | ************************************************************/ |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Reset associations |
||
| 271 | * @return RelationshipInterface |
||
| 272 | */ |
||
| 273 | public function reset(): RelationshipInterface |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Reset associations |
||
| 282 | * @return RelationshipInterface |
||
| 283 | */ |
||
| 284 | public function clear(): RelationshipInterface |
||
| 289 | |||
| 290 | |||
| 291 | /******************************************* |
||
| 292 | * SAVE |
||
| 293 | *******************************************/ |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function save(): bool |
||
| 331 | |||
| 332 | |||
| 333 | /******************************************* |
||
| 334 | * ASSOCIATE |
||
| 335 | *******************************************/ |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param $object |
||
| 339 | * @param array $attributes |
||
| 340 | * @return bool |
||
| 341 | * |
||
| 342 | * @deprecated use `add($object, $attributes)->save()` |
||
| 343 | */ |
||
| 344 | public function associateOne($object, array $attributes = []): bool |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param QueryInterface|ElementInterface[] $objects |
||
| 352 | * @return bool |
||
| 353 | * |
||
| 354 | * @deprecated use `add($object, $attributes)->save()` |
||
| 355 | */ |
||
| 356 | public function associateMany($objects): bool |
||
| 361 | |||
| 362 | |||
| 363 | /******************************************* |
||
| 364 | * DISSOCIATE |
||
| 365 | *******************************************/ |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @noinspection PhpDocMissingThrowsInspection |
||
| 369 | * |
||
| 370 | * @param ActiveRecord|ElementInterface|int|array $object |
||
| 371 | * @return bool |
||
| 372 | * |
||
| 373 | * @deprecated use `remove($object)->save()` |
||
| 374 | */ |
||
| 375 | public function dissociateOne($object): bool |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param QueryInterface|ElementInterface[] $objects |
||
| 383 | * @return bool |
||
| 384 | * |
||
| 385 | * @deprecated use `remove($objects)->save()` |
||
| 386 | */ |
||
| 387 | public function dissociateMany($objects): bool |
||
| 392 | |||
| 393 | |||
| 394 | /******************************************* |
||
| 395 | * CACHE |
||
| 396 | *******************************************/ |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param array $associations |
||
| 400 | * @param bool $mutated |
||
| 401 | * @return static |
||
| 402 | */ |
||
| 403 | protected function newCollection(array $associations, bool $mutated = true): self |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param $association |
||
| 413 | * @return RelationshipTrait |
||
|
|
|||
| 414 | */ |
||
| 415 | protected function addToCollection($association): self |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param int $key |
||
| 429 | * @return RelationshipTrait |
||
| 430 | */ |
||
| 431 | protected function removeFromCollection(int $key): self |
||
| 438 | |||
| 439 | |||
| 440 | /******************************************* |
||
| 441 | * UTILITIES |
||
| 442 | *******************************************/ |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Ensure we're working with an array of objects, not configs, etc |
||
| 446 | * |
||
| 447 | * @param array|QueryInterface|Collection|ElementInterface|UserAssociation $objects |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | protected function objectArray($objects): array |
||
| 463 | } |
||
| 464 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.