Complex classes like AssociationManagerTrait 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 AssociationManagerTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | trait AssociationManagerTrait |
||
| 23 | { |
||
| 24 | use MutatedTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var ActiveRecord[]|null |
||
| 28 | */ |
||
| 29 | protected $associations; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param null $object |
||
| 33 | * @return int|null |
||
| 34 | */ |
||
| 35 | abstract protected function findKey($object = null); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param array $criteria |
||
| 39 | * @return QueryInterface |
||
| 40 | */ |
||
| 41 | abstract public function query(array $criteria = []): QueryInterface; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param $object |
||
| 45 | * @return ActiveRecord |
||
| 46 | */ |
||
| 47 | abstract public function create($object): ActiveRecord; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * @return ActiveRecord[][] |
||
| 52 | */ |
||
| 53 | abstract protected function associationDelta(): array; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | abstract protected function handleAssociationError(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 62 | * @return ActiveRecord |
||
| 63 | */ |
||
| 64 | public function findOrCreate($object): ActiveRecord |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 75 | * @return ActiveRecord |
||
| 76 | * @throws Exception |
||
| 77 | */ |
||
| 78 | public function findOrFail($object): ActiveRecord |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return ActiveRecord[] |
||
| 89 | */ |
||
| 90 | public function findAll(): array |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param ActiveRecord|ElementInterface|int|string|null $object |
||
| 101 | * @return ActiveRecord|null |
||
| 102 | */ |
||
| 103 | public function findOne($object = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function exists($object): bool |
||
| 120 | |||
| 121 | |||
| 122 | /************************************************************ |
||
| 123 | * SET |
||
| 124 | ************************************************************/ |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param QueryInterface|ElementInterface[] $objects |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function setMany($objects) |
||
| 149 | |||
| 150 | |||
| 151 | /************************************************************ |
||
| 152 | * ADD |
||
| 153 | ************************************************************/ |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param QueryInterface|ElementInterface[] $objects |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function addMany($objects) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Associate a user to an organization |
||
| 183 | * |
||
| 184 | * @param ActiveRecord|ElementInterface|int|array $object |
||
| 185 | * @param array $attributes |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function addOne($object, array $attributes = []) |
||
| 204 | |||
| 205 | |||
| 206 | /************************************************************ |
||
| 207 | * REMOVE |
||
| 208 | ************************************************************/ |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Dissociate an array of user associations from an organization |
||
| 212 | * |
||
| 213 | * @param QueryInterface|ElementInterface[] $objects |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function removeMany($objects) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Dissociate a user from an organization |
||
| 240 | * |
||
| 241 | * @param ActiveRecord|ElementInterface|int|array |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function removeOne($object) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Reset associations |
||
| 255 | */ |
||
| 256 | public function reset() |
||
| 262 | |||
| 263 | |||
| 264 | /******************************************* |
||
| 265 | * SAVE |
||
| 266 | *******************************************/ |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function save(): bool |
||
| 304 | |||
| 305 | /******************************************* |
||
| 306 | * ASSOCIATE |
||
| 307 | *******************************************/ |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param $object |
||
| 311 | * @param array $attributes |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function associateOne($object, array $attributes = []): bool |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param QueryInterface|ElementInterface[] $objects |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | public function associateMany($objects): bool |
||
| 353 | |||
| 354 | |||
| 355 | /******************************************* |
||
| 356 | * DISSOCIATE |
||
| 357 | *******************************************/ |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @noinspection PhpDocMissingThrowsInspection |
||
| 361 | * |
||
| 362 | * @param ActiveRecord|ElementInterface|int|array $object |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public function dissociateOne($object): bool |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param QueryInterface|ElementInterface[] $objects |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function dissociateMany($objects): bool |
||
| 400 | |||
| 401 | |||
| 402 | /******************************************* |
||
| 403 | * CACHE |
||
| 404 | *******************************************/ |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param array $associations |
||
| 408 | * @return static |
||
| 409 | */ |
||
| 410 | protected function setCache(array $associations): self |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param $association |
||
| 420 | * @return AssociationManagerTrait |
||
| 421 | */ |
||
| 422 | protected function addToCache($association): self |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param int $key |
||
| 432 | * @return AssociationManagerTrait |
||
| 433 | */ |
||
| 434 | protected function removeFromCache(int $key): self |
||
| 441 | } |
||
| 442 |
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: