Complex classes like Attributable 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 Attributable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | trait Attributable |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The entity attributes. |
||
| 26 | * |
||
| 27 | * @var \Illuminate\Database\Eloquent\Collection |
||
| 28 | */ |
||
| 29 | protected static $entityAttributes; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The entity attribute value trash. |
||
| 33 | * |
||
| 34 | * @var \Illuminate\Support\Collection |
||
| 35 | */ |
||
| 36 | protected $entityAttributeValueTrash; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The entity attribute relations. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $entityAttributeRelations = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Determine if the entity attribute relations have been booted. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $entityAttributeRelationsBooted = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Boot the attributable trait for a model. |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public static function bootAttributable() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Check if the model needs to be booted and if so, do it. |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | protected function bootIfNotBooted() |
||
| 70 | { |
||
| 71 | parent::bootIfNotBooted(); |
||
| 72 | |||
| 73 | if (! $this->entityAttributeRelationsBooted) { |
||
| 74 | $attributes = $this->getEntityAttributes(); |
||
| 75 | |||
| 76 | // We will manually add a relationship for every attribute registered |
||
| 77 | // of this entity. Once we know the relation method we have to use, |
||
| 78 | // we will just add it to the entityAttributeRelations property. |
||
| 79 | foreach ($attributes as $attribute) { |
||
| 80 | $method = (bool) ($attribute->getAttributes()['is_collection'] ?? null) ? 'hasMany' : 'hasOne'; |
||
| 81 | |||
| 82 | // This will return a closure fully binded to the current entity instance, |
||
| 83 | // which will help us to simulate any relation as if it was made in the |
||
| 84 | // original entity class definition using a function statement. |
||
| 85 | $relation = Closure::bind(function () use ($attribute, $method) { |
||
| 86 | $relation = $this->{$method}(Attribute::getTypeModel($attribute->getAttribute('type')), 'entity_id', $this->getKeyName()); |
||
|
|
|||
| 87 | |||
| 88 | // Since an attribute could be attached to multiple entities, then values could have |
||
| 89 | // same entity ID, but for different entity types, so we need to add type where |
||
| 90 | // clause to fetch only values related to the given entity ID + entity type. |
||
| 91 | $relation->where('entity_type', $this->getMorphClass()); |
||
| 92 | |||
| 93 | // We add a where clause in order to fetch only the elements that are |
||
| 94 | // related to the given attribute. If no condition is set, it will |
||
| 95 | // fetch all the value rows related to the current entity. |
||
| 96 | return $relation->where($attribute->getForeignKey(), $attribute->getKey()); |
||
| 97 | }, $this, get_class($this)); |
||
| 98 | |||
| 99 | $this->setEntityAttributeRelation((string) ($attribute->getAttributes()['slug'] ?? null), $relation); |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->entityAttributeRelationsBooted = true; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set the given relationship on the model. |
||
| 108 | * |
||
| 109 | * @param string $relation |
||
| 110 | * @param mixed $value |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | public function relationsToArray() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function setRelation($key, $value) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function getRelationValue($key) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the entity attributes namespace if exists. |
||
| 176 | * |
||
| 177 | * @return string|null |
||
| 178 | */ |
||
| 179 | public function getEntityAttributesNamespace(): ?string |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the entity attributes. |
||
| 186 | * |
||
| 187 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 188 | */ |
||
| 189 | public function getEntityAttributes(): Collection |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Clear the static attributes cache for this model. |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | public function clearAttributableCache() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the fillable attributes of a given array. |
||
| 232 | * |
||
| 233 | * @param array $attributes |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | protected function fillableFromArray(array $attributes) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set a given attribute on the model. |
||
| 254 | * |
||
| 255 | * @param string $key |
||
| 256 | * @param mixed $value |
||
| 257 | * |
||
| 258 | * @return mixed |
||
| 259 | */ |
||
| 260 | public function setAttribute($key, $value) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get an attribute from the model. |
||
| 267 | * |
||
| 268 | * @param string $key |
||
| 269 | * |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | public function getAttribute($key) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set the entity attribute relation. |
||
| 279 | * |
||
| 280 | * @param string $relation |
||
| 281 | * @param mixed $value |
||
| 282 | * |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function setEntityAttributeRelation(string $relation, $value) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Check if the given key is an entity attribute relation. |
||
| 294 | * |
||
| 295 | * @param string $key |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function isEntityAttributeRelation(string $key): bool |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the entity attribute value trash. |
||
| 306 | * |
||
| 307 | * @return \Illuminate\Support\Collection |
||
| 308 | */ |
||
| 309 | public function getEntityAttributeValueTrash(): BaseCollection |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the entity attribute relations. |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function getEntityAttributeRelations(): array |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Check if the given key is an entity attribute. |
||
| 326 | * |
||
| 327 | * @param string $key |
||
| 328 | * |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | public function isEntityAttribute(string $key) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get the entity attribute. |
||
| 340 | * |
||
| 341 | * @param string $key |
||
| 342 | * |
||
| 343 | * @return mixed |
||
| 344 | */ |
||
| 345 | public function getEntityAttribute(string $key) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get the entity attribute value. |
||
| 356 | * |
||
| 357 | * @param string $key |
||
| 358 | * |
||
| 359 | * @return mixed |
||
| 360 | */ |
||
| 361 | protected function getEntityAttributeValue(string $key) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get the entity attribute relationship. |
||
| 377 | * |
||
| 378 | * @param string $key |
||
| 379 | * |
||
| 380 | * @return mixed |
||
| 381 | */ |
||
| 382 | protected function getEntityAttributeRelation(string $key) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set the entity attribute. |
||
| 395 | * |
||
| 396 | * @param string $key |
||
| 397 | * @param mixed $value |
||
| 398 | * |
||
| 399 | * @return mixed |
||
| 400 | */ |
||
| 401 | public function setEntityAttribute(string $key, $value) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Set the entity attribute value. |
||
| 437 | * |
||
| 438 | * @param \Rinvex\Attributes\Models\Attribute $attribute |
||
| 439 | * @param mixed $value |
||
| 440 | * |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | protected function setEntityAttributeValue(Attribute $attribute, $value) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Determine if the given key is a raw entity attribute. |
||
| 462 | * |
||
| 463 | * @param string $key |
||
| 464 | * |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | protected function isRawEntityAttribute(string $key): bool |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get entity attribute bare name. |
||
| 474 | * |
||
| 475 | * @param string $key |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | protected function getEntityAttributeName(string $key): string |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the attributes attached to this entity. |
||
| 486 | * |
||
| 487 | * @return \Illuminate\Database\Eloquent\Collection|null |
||
| 488 | */ |
||
| 489 | public function attributes(): ?Collection |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Scope query with the given entity attribute. |
||
| 496 | * |
||
| 497 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
| 498 | * @param string $key |
||
| 499 | * @param mixed $value |
||
| 500 | * |
||
| 501 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 502 | */ |
||
| 503 | public function scopeHasAttribute(Builder $builder, string $key, $value): Builder |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Dynamically pipe calls to attribute relations. |
||
| 512 | * |
||
| 513 | * @param string $method |
||
| 514 | * @param array $parameters |
||
| 515 | * |
||
| 516 | * @return mixed |
||
| 517 | */ |
||
| 518 | public function __call($method, $parameters) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Prepare the instance for serialization. |
||
| 533 | * |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | public function __sleep() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Restore the model after serialization. |
||
| 554 | * |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | public function __wakeup() |
||
| 572 | } |
||
| 573 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: