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 |
||
24 | trait Attributable |
||
25 | { |
||
26 | /** |
||
27 | * The entity attributes. |
||
28 | * |
||
29 | * @var \Illuminate\Database\Eloquent\Collection |
||
30 | */ |
||
31 | protected static $entityAttributes; |
||
32 | |||
33 | /** |
||
34 | * The entity attribute value trash. |
||
35 | * |
||
36 | * @var \Illuminate\Support\Collection |
||
37 | */ |
||
38 | protected $entityAttributeValueTrash; |
||
39 | |||
40 | /** |
||
41 | * The entity attribute relations. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $entityAttributeRelations = []; |
||
46 | |||
47 | /** |
||
48 | * Determine if the entity attribute relations have been booted. |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $entityAttributeRelationsBooted = false; |
||
53 | |||
54 | /** |
||
55 | * Boot the attributable trait for a model. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public static function bootAttributable() |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | protected function bootIfNotBooted() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function relationsToArray() |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function setRelation($key, $value) |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function getRelationValue($key) |
||
142 | |||
143 | /** |
||
144 | * Get the entity attributes namespace if exists. |
||
145 | * |
||
146 | * @return string|null |
||
147 | */ |
||
148 | public function getEntityAttributesNamespace(): ?string |
||
152 | |||
153 | /** |
||
154 | * Get the entity attributes. |
||
155 | * |
||
156 | * @return \Illuminate\Database\Eloquent\Collection |
||
157 | */ |
||
158 | public function getEntityAttributes(): Collection |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | protected function fillableFromArray(array $attributes) |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function setAttribute($key, $value) |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | public function getAttribute($key) |
||
219 | |||
220 | /** |
||
221 | * Set the entity attribute relation. |
||
222 | * |
||
223 | * @param string $relation |
||
224 | * @param mixed $value |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function setEntityAttributeRelation(string $relation, $value) |
||
234 | |||
235 | /** |
||
236 | * Check if the given key is an entity attribute relation. |
||
237 | * |
||
238 | * @param string $key |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function isEntityAttributeRelation(string $key): bool |
||
246 | |||
247 | /** |
||
248 | * Get the entity attribute value trash. |
||
249 | * |
||
250 | * @return \Illuminate\Support\Collection |
||
251 | */ |
||
252 | public function getEntityAttributeValueTrash(): BaseCollection |
||
256 | |||
257 | /** |
||
258 | * Get the entity attribute relations. |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | public function getEntityAttributeRelations(): array |
||
266 | |||
267 | /** |
||
268 | * Check if the given key is an entity attribute. |
||
269 | * |
||
270 | * @param string $key |
||
271 | * |
||
272 | * @return mixed |
||
273 | */ |
||
274 | public function isEntityAttribute(string $key) |
||
280 | |||
281 | /** |
||
282 | * Get the entity attribute. |
||
283 | * |
||
284 | * @param string $key |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | public function getEntityAttribute(string $key) |
||
296 | |||
297 | /** |
||
298 | * Get the entity attribute value. |
||
299 | * |
||
300 | * @param string $key |
||
301 | * |
||
302 | * @return mixed |
||
303 | */ |
||
304 | protected function getEntityAttributeValue(string $key) |
||
317 | |||
318 | /** |
||
319 | * Get the entity attribute relationship. |
||
320 | * |
||
321 | * @param string $key |
||
322 | * |
||
323 | * @return mixed |
||
324 | */ |
||
325 | protected function getEntityAttributeRelation(string $key) |
||
335 | |||
336 | /** |
||
337 | * Set the entity attribute. |
||
338 | * |
||
339 | * @param string $key |
||
340 | * @param mixed $value |
||
341 | * |
||
342 | * @return mixed |
||
343 | */ |
||
344 | public function setEntityAttribute(string $key, $value) |
||
377 | |||
378 | /** |
||
379 | * Set the entity attribute value. |
||
380 | * |
||
381 | * @param \Rinvex\Attributes\Models\Attribute $attribute |
||
382 | * @param mixed $value |
||
383 | * |
||
384 | * @return $this |
||
385 | */ |
||
386 | protected function setEntityAttributeValue(Attribute $attribute, $value) |
||
402 | |||
403 | /** |
||
404 | * Determine if the given key is a raw entity attribute. |
||
405 | * |
||
406 | * @param string $key |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | protected function isRawEntityAttribute(string $key): bool |
||
414 | |||
415 | /** |
||
416 | * Get entity attribute bare name. |
||
417 | * |
||
418 | * @param string $key |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | protected function getEntityAttributeName(string $key): string |
||
426 | |||
427 | /** |
||
428 | * Get the attributes attached to this entity. |
||
429 | * |
||
430 | * @return \Illuminate\Database\Eloquent\Collection|null |
||
431 | */ |
||
432 | public function attributes(): ?Collection |
||
436 | |||
437 | /** |
||
438 | * Scope query with the given entity attribute. |
||
439 | * |
||
440 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
441 | * @param string $key |
||
442 | * @param mixed $value |
||
443 | * |
||
444 | * @return \Illuminate\Database\Eloquent\Builder |
||
445 | */ |
||
446 | public function scopeHasAttribute(Builder $builder, string $key, $value): Builder |
||
452 | |||
453 | /** |
||
454 | * Dynamically pipe calls to attribute relations. |
||
455 | * |
||
456 | * @param string $method |
||
457 | * @param array $parameters |
||
458 | * |
||
459 | * @return mixed |
||
460 | */ |
||
461 | public function __call($method, $parameters) |
||
473 | |||
474 | /** |
||
475 | * Prepare the instance for serialization. |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | public function __sleep() |
||
494 | |||
495 | /** |
||
496 | * Restore the model after serialization. |
||
497 | * |
||
498 | * @return void |
||
499 | */ |
||
500 | public function __wakeup() |
||
515 | } |
||
516 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.