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