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 $relations; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param null $object |
||
| 37 | * @return int|null |
||
| 38 | */ |
||
| 39 | abstract protected function findKey($object = null); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Create a new association/relationship record |
||
| 43 | * |
||
| 44 | * @param $object |
||
| 45 | * @return ActiveRecord |
||
| 46 | */ |
||
| 47 | abstract protected function create($object): ActiveRecord; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Resolve the object that will be related |
||
| 51 | * |
||
| 52 | * @param $object |
||
| 53 | * @return mixed |
||
| 54 | */ |
||
| 55 | abstract protected function resolveObjectInternal($object); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * A collection of existing relationships, indexed by id. We'll compare these with |
||
| 59 | * and new relations to determine if we need to add/remove relations |
||
| 60 | * |
||
| 61 | * @return Collection |
||
| 62 | */ |
||
| 63 | abstract protected function existingRelationships(): Collection; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * An array of relationships to save and an array of relationships to delete. This will |
||
| 67 | * want to be used as such: `list($save, $delete) = $this->delta();` |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | abstract protected function delta(): array; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 75 | * @return ActiveRecord |
||
| 76 | */ |
||
| 77 | public function findOrCreate($object): ActiveRecord |
||
| 78 | { |
||
| 79 | if (null === ($association = $this->findOne($object))) { |
||
| 80 | $association = $this->create($object); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $association; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 88 | * @return ActiveRecord |
||
| 89 | * @throws Exception |
||
| 90 | */ |
||
| 91 | public function findOrFail($object): ActiveRecord |
||
| 92 | { |
||
| 93 | if (null === ($association = $this->findOne($object))) { |
||
| 94 | throw new Exception("Association could not be found."); |
||
| 95 | } |
||
| 96 | |||
| 97 | return $association; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param ActiveRecord|ElementInterface|int|string|null $object |
||
| 102 | * @return ActiveRecord|null |
||
| 103 | */ |
||
| 104 | public function findOne($object = null) |
||
| 112 | |||
| 113 | |||
| 114 | /************************************************************ |
||
| 115 | * COLLECTIONS |
||
| 116 | ************************************************************/ |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | public function getRelationships(): Collection |
||
| 122 | { |
||
| 123 | if (null === $this->relations) { |
||
| 124 | $this->relations = $this->existingRelationships(); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $this->relations; |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | /************************************************************ |
||
| 132 | * ADD / REMOVE |
||
| 133 | ************************************************************/ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add one or many object relations (but do not save) |
||
| 137 | * |
||
| 138 | * @param $objects |
||
| 139 | * @param array $attributes |
||
| 140 | * @return RelationshipInterface |
||
| 141 | */ |
||
| 142 | public function add($objects, array $attributes = []): RelationshipInterface |
||
| 143 | { |
||
| 144 | foreach ($this->objectArray($objects) as $object) { |
||
| 145 | $this->addOne($object, $attributes); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param $object |
||
| 153 | * @param array $attributes |
||
| 154 | * @return RelationshipInterface |
||
| 155 | */ |
||
| 156 | private function addOne($object, array $attributes = []): RelationshipInterface |
||
| 157 | { |
||
| 158 | $isNew = false; |
||
| 159 | |||
| 160 | // Check if it's already linked |
||
| 161 | if (null === ($association = $this->findOne($object))) { |
||
| 162 | $association = $this->create($object); |
||
| 163 | $isNew = true; |
||
| 164 | } |
||
| 165 | |||
| 166 | // Modify? |
||
| 167 | if (!empty($attributes)) { |
||
| 168 | Craft::configure( |
||
| 169 | $association, |
||
| 170 | $attributes |
||
| 171 | ); |
||
| 172 | |||
| 173 | $this->mutated = true; |
||
| 174 | |||
| 175 | if (!$isNew) { |
||
| 176 | $this->updateCollection($this->relations, $association); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($isNew) { |
||
| 181 | $this->addToRelations($association); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param $objects |
||
| 189 | * @return RelationshipInterface |
||
| 190 | */ |
||
| 191 | public function remove($objects): RelationshipInterface |
||
| 192 | { |
||
| 193 | foreach ($this->objectArray($objects) as $object) { |
||
| 194 | if (null !== ($key = $this->findKey($object))) { |
||
| 195 | $this->removeFromRelations($key); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | return $this; |
||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | /******************************************* |
||
| 204 | * SAVE |
||
| 205 | *******************************************/ |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function save(): bool |
||
| 211 | { |
||
| 212 | // No changes? |
||
| 213 | if (!$this->isMutated()) { |
||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | $success = true; |
||
| 218 | |||
| 219 | list($save, $delete) = $this->delta(); |
||
| 220 | |||
| 221 | foreach ($delete as $relationship) { |
||
| 222 | if (!$relationship->delete()) { |
||
| 223 | $success = false; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | foreach ($save as $relationship) { |
||
| 228 | if (!$relationship->save()) { |
||
| 229 | $success = false; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | $this->reset(); |
||
| 234 | |||
| 235 | return $success; |
||
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | /************************************************************ |
||
| 240 | * UTILITIES |
||
| 241 | ************************************************************/ |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Reset associations |
||
| 245 | * @return RelationshipInterface |
||
| 246 | */ |
||
| 247 | public function reset(): RelationshipInterface |
||
| 248 | { |
||
| 249 | $this->relations = null; |
||
| 250 | $this->mutated = false; |
||
| 251 | return $this; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Reset associations |
||
| 256 | * @return RelationshipInterface |
||
| 257 | */ |
||
| 258 | public function clear(): RelationshipInterface |
||
| 259 | { |
||
| 260 | $this->newRelations([]); |
||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param ActiveRecord|ElementInterface|int|string $object |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function exists($object): bool |
||
| 269 | { |
||
| 270 | return null !== $this->findKey($object); |
||
| 271 | } |
||
| 272 | |||
| 273 | |||
| 274 | /******************************************* |
||
| 275 | * COLLECTION UTILS |
||
| 276 | *******************************************/ |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $associations |
||
| 280 | * @param bool $mutated |
||
| 281 | * @return static |
||
| 282 | */ |
||
| 283 | protected function newRelations(array $associations, bool $mutated = true): self |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $association |
||
| 293 | * @return RelationshipTrait |
||
|
|
|||
| 294 | */ |
||
| 295 | protected function addToRelations($association): self |
||
| 296 | { |
||
| 297 | $this->insertCollection($this->getRelationships(), $association); |
||
| 298 | $this->mutated = true; |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param int $key |
||
| 305 | * @return RelationshipTrait |
||
| 306 | */ |
||
| 307 | protected function removeFromRelations(int $key): self |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $associations |
||
| 317 | * @return Collection |
||
| 318 | */ |
||
| 319 | protected function createRelations(array $associations = []): Collection |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Update a relation that's already association. |
||
| 331 | * |
||
| 332 | * @param Collection $collection |
||
| 333 | * @param $association |
||
| 334 | */ |
||
| 335 | protected function updateCollection(Collection $collection, $association) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Insert a relation that's not already associated |
||
| 341 | * |
||
| 342 | * @param Collection $collection |
||
| 343 | * @param $association |
||
| 344 | */ |
||
| 345 | protected function insertCollection(Collection $collection, $association) |
||
| 349 | |||
| 350 | |||
| 351 | /******************************************* |
||
| 352 | * RESOLVERS |
||
| 353 | *******************************************/ |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @inheritDoc |
||
| 357 | */ |
||
| 358 | protected function resolveObject($object = null) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Ensure we're working with an array of objects, not configs, etc |
||
| 375 | * |
||
| 376 | * @param array|QueryInterface|Collection|ElementInterface|UserAssociation $objects |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | protected function objectArray($objects): array |
||
| 392 | } |
||
| 393 |
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.