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 | * Clear the static attributes cache for this model. |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | public function clearAttributableCache() |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | protected function fillableFromArray(array $attributes) |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function setAttribute($key, $value) |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function getAttribute($key) |
||
231 | |||
232 | /** |
||
233 | * Set the entity attribute relation. |
||
234 | * |
||
235 | * @param string $relation |
||
236 | * @param mixed $value |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function setEntityAttributeRelation(string $relation, $value) |
||
246 | |||
247 | /** |
||
248 | * Check if the given key is an entity attribute relation. |
||
249 | * |
||
250 | * @param string $key |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function isEntityAttributeRelation(string $key): bool |
||
258 | |||
259 | /** |
||
260 | * Get the entity attribute value trash. |
||
261 | * |
||
262 | * @return \Illuminate\Support\Collection |
||
263 | */ |
||
264 | public function getEntityAttributeValueTrash(): BaseCollection |
||
268 | |||
269 | /** |
||
270 | * Get the entity attribute relations. |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function getEntityAttributeRelations(): array |
||
278 | |||
279 | /** |
||
280 | * Check if the given key is an entity attribute. |
||
281 | * |
||
282 | * @param string $key |
||
283 | * |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function isEntityAttribute(string $key) |
||
292 | |||
293 | /** |
||
294 | * Get the entity attribute. |
||
295 | * |
||
296 | * @param string $key |
||
297 | * |
||
298 | * @return mixed |
||
299 | */ |
||
300 | public function getEntityAttribute(string $key) |
||
308 | |||
309 | /** |
||
310 | * Get the entity attribute value. |
||
311 | * |
||
312 | * @param string $key |
||
313 | * |
||
314 | * @return mixed |
||
315 | */ |
||
316 | protected function getEntityAttributeValue(string $key) |
||
329 | |||
330 | /** |
||
331 | * Get the entity attribute relationship. |
||
332 | * |
||
333 | * @param string $key |
||
334 | * |
||
335 | * @return mixed |
||
336 | */ |
||
337 | protected function getEntityAttributeRelation(string $key) |
||
347 | |||
348 | /** |
||
349 | * Set the entity attribute. |
||
350 | * |
||
351 | * @param string $key |
||
352 | * @param mixed $value |
||
353 | * |
||
354 | * @return mixed |
||
355 | */ |
||
356 | public function setEntityAttribute(string $key, $value) |
||
389 | |||
390 | /** |
||
391 | * Set the entity attribute value. |
||
392 | * |
||
393 | * @param \Rinvex\Attributes\Models\Attribute $attribute |
||
394 | * @param mixed $value |
||
395 | * |
||
396 | * @return $this |
||
397 | */ |
||
398 | protected function setEntityAttributeValue(Attribute $attribute, $value) |
||
414 | |||
415 | /** |
||
416 | * Determine if the given key is a raw entity attribute. |
||
417 | * |
||
418 | * @param string $key |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | protected function isRawEntityAttribute(string $key): bool |
||
426 | |||
427 | /** |
||
428 | * Get entity attribute bare name. |
||
429 | * |
||
430 | * @param string $key |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | protected function getEntityAttributeName(string $key): string |
||
438 | |||
439 | /** |
||
440 | * Get the attributes attached to this entity. |
||
441 | * |
||
442 | * @return \Illuminate\Database\Eloquent\Collection|null |
||
443 | */ |
||
444 | public function attributes(): ?Collection |
||
448 | |||
449 | /** |
||
450 | * Scope query with the given entity attribute. |
||
451 | * |
||
452 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
453 | * @param string $key |
||
454 | * @param mixed $value |
||
455 | * |
||
456 | * @return \Illuminate\Database\Eloquent\Builder |
||
457 | */ |
||
458 | public function scopeHasAttribute(Builder $builder, string $key, $value): Builder |
||
464 | |||
465 | /** |
||
466 | * Dynamically pipe calls to attribute relations. |
||
467 | * |
||
468 | * @param string $method |
||
469 | * @param array $parameters |
||
470 | * |
||
471 | * @return mixed |
||
472 | */ |
||
473 | public function __call($method, $parameters) |
||
485 | |||
486 | /** |
||
487 | * Prepare the instance for serialization. |
||
488 | * |
||
489 | * @return array |
||
490 | */ |
||
491 | public function __sleep() |
||
506 | |||
507 | /** |
||
508 | * Restore the model after serialization. |
||
509 | * |
||
510 | * @return void |
||
511 | */ |
||
512 | public function __wakeup() |
||
527 | } |
||
528 |
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.