Complex classes like Taggable 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 Taggable, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Cviebrock\EloquentTaggable; |
||
19 | trait Taggable |
||
20 | { |
||
21 | /** |
||
22 | 23 | * Property to control sequence on alias |
|
23 | * |
||
24 | 23 | * @var int |
|
25 | 23 | */ |
|
26 | private $taggableAliasSequence = 0; |
||
27 | |||
28 | /** |
||
29 | * Boot the trait. |
||
30 | * |
||
31 | * Listen for the deleting event of a model, then remove the relation between it and tags |
||
32 | */ |
||
33 | protected static function bootTaggable(): void |
||
41 | 23 | ||
42 | /** |
||
43 | 23 | * Get a collection of all tags the model has. |
|
44 | * |
||
45 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
46 | */ |
||
47 | public function tags(): MorphToMany |
||
54 | |||
55 | 1 | /** |
|
56 | * Attach one or multiple tags to the model. |
||
57 | 1 | * |
|
58 | 1 | * @param string|array $tags |
|
59 | 1 | * |
|
60 | * @return self |
||
61 | 1 | */ |
|
62 | public function tag($tags): self |
||
75 | |||
76 | /** |
||
77 | * Attach one or more existing tags to a model, |
||
78 | * identified by the tag's IDs. |
||
79 | * |
||
80 | * @param int|int[] $ids |
||
81 | 2 | * |
|
82 | * @return $this |
||
83 | 2 | */ |
|
84 | public function tagById($ids): self |
||
91 | |||
92 | /** |
||
93 | 23 | * Detach one or multiple tags from the model. |
|
94 | * |
||
95 | 23 | * @param string|array $tags |
|
96 | * |
||
97 | 23 | * @return self |
|
98 | 23 | */ |
|
99 | 23 | public function untag($tags): self |
|
111 | 1 | ||
112 | 1 | /** |
|
113 | 1 | * Detach one or more existing tags to a model, |
|
114 | 1 | * identified by the tag's IDs. |
|
115 | * |
||
116 | * @param int|int[] $ids |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function untagById($ids): self |
||
127 | |||
128 | /** |
||
129 | * Remove all tags from the model and assign the given ones. |
||
130 | * |
||
131 | * @param string|array $tags |
||
132 | * |
||
133 | 2 | * @return self |
|
134 | */ |
||
135 | 2 | public function retag($tags): self |
|
139 | |||
140 | /** |
||
141 | * Remove all tags from the model and assign the given ones by ID. |
||
142 | * |
||
143 | 11 | * @param int|int[] $ids |
|
144 | * |
||
145 | 11 | * @return self |
|
146 | */ |
||
147 | public function retagById($ids): self |
||
151 | |||
152 | /** |
||
153 | 2 | * Remove all tags from the model. |
|
154 | * |
||
155 | 2 | * @return self |
|
156 | */ |
||
157 | public function detag(): self |
||
163 | |||
164 | /** |
||
165 | * Add one tag to the model. |
||
166 | 1 | * |
|
167 | * @param string $tagName |
||
168 | 1 | */ |
|
169 | protected function addOneTag(string $tagName): void |
||
179 | |||
180 | /** |
||
181 | * Remove one tag from the model |
||
182 | * |
||
183 | 1 | * @param string $tagName |
|
184 | */ |
||
185 | 1 | protected function removeOneTag(string $tagName): void |
|
193 | 1 | ||
194 | /** |
||
195 | * Get all the tags of the model as a delimited string. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getTagListAttribute(): string |
||
203 | 1 | ||
204 | /** |
||
205 | 1 | * Get all normalized tags of a model as a delimited string. |
|
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getTagListNormalizedAttribute(): string |
||
213 | 2 | ||
214 | /** |
||
215 | * Get all tags of a model as an array. |
||
216 | 2 | * |
|
217 | * @return array |
||
218 | 2 | */ |
|
219 | public function getTagArrayAttribute(): array |
||
223 | |||
224 | /** |
||
225 | * Get all normalized tags of a model as an array. |
||
226 | 1 | * |
|
227 | * @return array |
||
228 | 1 | */ |
|
229 | public function getTagArrayNormalizedAttribute(): array |
||
233 | |||
234 | /** |
||
235 | * Determine if a given tag is attached to the model. |
||
236 | * |
||
237 | * @param Tag|string $tag |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function hasTag($tag): bool |
||
251 | |||
252 | /** |
||
253 | * Query scope for models that have all of the given tags. |
||
254 | * |
||
255 | * @param Builder $query |
||
256 | * @param array|string $tags |
||
257 | * |
||
258 | * @return Builder |
||
259 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
260 | * @throws \ErrorException |
||
261 | */ |
||
262 | public function scopeWithAllTags(Builder $query, $tags): Builder |
||
293 | |||
294 | /** |
||
295 | * Query scope for models that have any of the given tags. |
||
296 | * |
||
297 | * @param Builder $query |
||
298 | * @param array|string $tags |
||
299 | * |
||
300 | * @return Builder |
||
301 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
302 | * @throws \ErrorException |
||
303 | */ |
||
304 | public function scopeWithAnyTags(Builder $query, $tags): Builder |
||
328 | |||
329 | /** |
||
330 | * Query scope for models that have any tag. |
||
331 | * |
||
332 | * @param Builder $query |
||
333 | * |
||
334 | * @return Builder |
||
335 | */ |
||
336 | public function scopeIsTagged(Builder $query): Builder |
||
341 | |||
342 | /** |
||
343 | * Query scope for models that do not have all of the given tags. |
||
344 | * |
||
345 | * @param Builder $query |
||
346 | * @param string|array $tags |
||
347 | * @param bool $includeUntagged |
||
348 | * |
||
349 | * @return Builder |
||
350 | * @throws \ErrorException |
||
351 | */ |
||
352 | public function scopeWithoutAllTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
373 | |||
374 | /** |
||
375 | * Query scope for models that do not have any of the given tags. |
||
376 | * |
||
377 | * @param Builder $query |
||
378 | * @param string|array $tags |
||
379 | * @param bool $includeUntagged |
||
380 | * |
||
381 | * @return Builder |
||
382 | * @throws \ErrorException |
||
383 | */ |
||
384 | public function scopeWithoutAnyTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
404 | |||
405 | /** |
||
406 | * Query scope for models that does not have have any tags. |
||
407 | * |
||
408 | * @param Builder $query |
||
409 | * |
||
410 | * @return Builder |
||
411 | */ |
||
412 | public function scopeIsNotTagged(Builder $query): Builder |
||
420 | |||
421 | /** |
||
422 | * @param Builder $query |
||
423 | * @param string $joinType |
||
424 | * |
||
425 | * @return Builder |
||
426 | */ |
||
427 | private function prepareTableJoin(Builder $query, string $joinType, string $alias): Builder |
||
448 | |||
449 | /** |
||
450 | * Get a collection of all the tag models used for the called class. |
||
451 | * |
||
452 | * @return Collection |
||
453 | */ |
||
454 | public static function allTagModels(): Collection |
||
458 | |||
459 | /** |
||
460 | * Get an array of all tags used for the called class. |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | public static function allTags(): array |
||
471 | |||
472 | /** |
||
473 | * Get all the tags used for the called class as a delimited string. |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | public static function allTagsList(): string |
||
481 | |||
482 | /** |
||
483 | * Rename one the tags for the called class. |
||
484 | * |
||
485 | * @param string $oldTag |
||
486 | * @param string $newTag |
||
487 | * |
||
488 | * @return int |
||
489 | */ |
||
490 | public static function renameTag(string $oldTag, string $newTag): int |
||
494 | |||
495 | /** |
||
496 | * Get the most popular tags for the called class. |
||
497 | * |
||
498 | * @param int $limit |
||
499 | * @param int $minCount |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | public static function popularTags(int $limit = null, int $minCount = 1): array |
||
510 | |||
511 | /** |
||
512 | * Get the most popular tags for the called class. |
||
513 | * |
||
514 | * @param int $limit |
||
515 | * @param int $minCount |
||
516 | * |
||
517 | * @return array |
||
518 | */ |
||
519 | public static function popularTagsNormalized(int $limit = null, int $minCount = 1): array |
||
526 | |||
527 | /** |
||
528 | * Returns the Related Pivot Key Name with the table alias. |
||
529 | * |
||
530 | * @param string $alias |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | private function getQualifiedRelatedPivotKeyNameWithAlias(string $alias): string |
||
541 | |||
542 | /** |
||
543 | * Returns the Foreign Pivot Key Name with the table alias. |
||
544 | * |
||
545 | * @param string $alias |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | private function getQualifiedForeignPivotKeyNameWithAlias(string $alias): string |
||
556 | |||
557 | /** |
||
558 | * Create a new alias to use on scopes to be able to combine many scopes |
||
559 | * |
||
560 | * @param string $scope |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | private function taggableCreateNewAlias(string $scope): string |
||
571 | } |
||
572 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.