Complex classes like RelationshipManagerTrait 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 RelationshipManagerTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | trait RelationshipManagerTrait |
||
| 24 | { |
||
| 25 | use MutatedTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Collection|null |
||
| 29 | */ |
||
| 30 | protected $associations; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param null $object |
||
| 34 | * @return int|null |
||
| 35 | */ |
||
| 36 | abstract protected function findKey($object = null); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param array $criteria |
||
| 40 | * @return QueryInterface |
||
| 41 | */ |
||
| 42 | abstract protected function query(array $criteria = []): QueryInterface; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param $object |
||
| 46 | * @return ActiveRecord |
||
| 47 | */ |
||
| 48 | abstract protected function create($object): ActiveRecord; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | * @return ActiveRecord[][] |
||
| 53 | */ |
||
| 54 | abstract protected function associationDelta(): array; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | abstract protected function handleAssociationError(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 63 | * @return ActiveRecord |
||
| 64 | */ |
||
| 65 | public function findOrCreate($object): ActiveRecord |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 76 | * @return ActiveRecord |
||
| 77 | * @throws Exception |
||
| 78 | */ |
||
| 79 | public function findOrFail($object): ActiveRecord |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return Collection |
||
| 90 | */ |
||
| 91 | 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 | * SET |
||
| 125 | ************************************************************/ |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param QueryInterface|ElementInterface[] $objects |
||
| 129 | * @return static |
||
| 130 | */ |
||
| 131 | public function setMany($objects) |
||
| 150 | |||
| 151 | |||
| 152 | /************************************************************ |
||
| 153 | * ADD |
||
| 154 | ************************************************************/ |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param QueryInterface|ElementInterface[] $objects |
||
| 158 | * @return static |
||
| 159 | */ |
||
| 160 | public function addMany($objects) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Associate a user to an organization |
||
| 184 | * |
||
| 185 | * @param ActiveRecord|ElementInterface|int|array $object |
||
| 186 | * @param array $attributes |
||
| 187 | * @return static |
||
| 188 | */ |
||
| 189 | public function addOne($object, array $attributes = []) |
||
| 209 | |||
| 210 | |||
| 211 | /************************************************************ |
||
| 212 | * REMOVE |
||
| 213 | ************************************************************/ |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Dissociate an array of user associations from an organization |
||
| 217 | * |
||
| 218 | * @param QueryInterface|ElementInterface[] $objects |
||
| 219 | * @return static |
||
| 220 | */ |
||
| 221 | public function removeMany($objects) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Dissociate a user from an organization |
||
| 245 | * |
||
| 246 | * @param ActiveRecord|ElementInterface|int|array |
||
| 247 | * @return static |
||
| 248 | */ |
||
| 249 | public function removeOne($object) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Reset associations |
||
| 264 | */ |
||
| 265 | public function reset() |
||
| 271 | |||
| 272 | |||
| 273 | /******************************************* |
||
| 274 | * SAVE |
||
| 275 | *******************************************/ |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public function save(): bool |
||
| 313 | |||
| 314 | /******************************************* |
||
| 315 | * ASSOCIATE |
||
| 316 | *******************************************/ |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param $object |
||
| 320 | * @param array $attributes |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function associateOne($object, array $attributes = []): bool |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param QueryInterface|ElementInterface[] $objects |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function associateMany($objects): bool |
||
| 362 | |||
| 363 | |||
| 364 | /******************************************* |
||
| 365 | * DISSOCIATE |
||
| 366 | *******************************************/ |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @noinspection PhpDocMissingThrowsInspection |
||
| 370 | * |
||
| 371 | * @param ActiveRecord|ElementInterface|int|array $object |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function dissociateOne($object): bool |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param QueryInterface|ElementInterface[] $objects |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function dissociateMany($objects): bool |
||
| 409 | |||
| 410 | |||
| 411 | /******************************************* |
||
| 412 | * CACHE |
||
| 413 | *******************************************/ |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param array $associations |
||
| 417 | * @param bool $mutated |
||
| 418 | * @return static |
||
| 419 | */ |
||
| 420 | protected function setCache(array $associations, bool $mutated = true): self |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param $association |
||
| 430 | * @return RelationshipManagerTrait |
||
| 431 | */ |
||
| 432 | protected function addToCache($association): self |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param int $key |
||
| 446 | * @return RelationshipManagerTrait |
||
| 447 | */ |
||
| 448 | protected function removeFromCache(int $key): self |
||
| 455 | } |
||
| 456 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: