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; |
||
17 | trait Taggable |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Boot the trait. |
||
22 | 23 | * |
|
23 | * Listen for the deleting event of a model, then remove the relation between it and tags |
||
24 | 23 | */ |
|
25 | 23 | protected static function bootTaggable(): void |
|
33 | |||
34 | /** |
||
35 | 23 | * Get a collection of all tags the model has. |
|
36 | * |
||
37 | 23 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
38 | */ |
||
39 | 23 | public function tags(): MorphToMany |
|
45 | |||
46 | /** |
||
47 | * Attach one or multiple tags to the model. |
||
48 | * |
||
49 | * @param string|array $tags |
||
50 | * |
||
51 | * @return self |
||
52 | */ |
||
53 | 1 | public function tag($tags): self |
|
64 | |||
65 | /** |
||
66 | * Detach one or multiple tags from the model. |
||
67 | * |
||
68 | * @param string|array $tags |
||
69 | * |
||
70 | * @return self |
||
71 | 1 | */ |
|
72 | public function untag($tags): self |
||
82 | |||
83 | 2 | /** |
|
84 | * Remove all tags from the model and assign the given ones. |
||
85 | 2 | * |
|
86 | * @param string|array $tags |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | public function retag($tags): self |
||
94 | |||
95 | 23 | /** |
|
96 | * Remove all tags from the model. |
||
97 | 23 | * |
|
98 | 23 | * @return self |
|
99 | 23 | */ |
|
100 | 23 | public function detag(): self |
|
106 | |||
107 | 1 | /** |
|
108 | * Add one tag to the model. |
||
109 | 1 | * |
|
110 | * @param string $tagName |
||
111 | 1 | */ |
|
112 | 1 | protected function addOneTag(string $tagName): void |
|
122 | 13 | ||
123 | /** |
||
124 | 2 | * Remove one tag from the model |
|
125 | 13 | * |
|
126 | * @param string $tagName |
||
127 | */ |
||
128 | protected function removeOneTag(string $tagName): void |
||
136 | |||
137 | /** |
||
138 | * Get all the tags of the model as a delimited string. |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getTagListAttribute(): string |
||
146 | |||
147 | /** |
||
148 | * Get all normalized tags of a model as a delimited string. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getTagListNormalizedAttribute(): string |
||
156 | |||
157 | /** |
||
158 | * Get all tags of a model as an array. |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getTagArrayAttribute(): array |
||
166 | 1 | ||
167 | /** |
||
168 | 1 | * Get all normalized tags of a model as an array. |
|
169 | * |
||
170 | * @return array |
||
171 | 1 | */ |
|
172 | 1 | public function getTagArrayNormalizedAttribute(): array |
|
176 | |||
177 | /** |
||
178 | * Determine if a given tag is attached to the model. |
||
179 | * |
||
180 | * @param Tag|string $tag |
||
181 | * |
||
182 | * @return bool |
||
183 | 1 | */ |
|
184 | public function hasTag($tag): bool |
||
194 | |||
195 | /** |
||
196 | * Query scope for models that have all of the given tags. |
||
197 | * |
||
198 | * @param Builder $query |
||
199 | * @param array|string $tags |
||
200 | * |
||
201 | * @return Builder |
||
202 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
203 | 1 | * @throws \ErrorException |
|
204 | */ |
||
205 | 1 | public function scopeWithAllTags(Builder $query, $tags): Builder |
|
236 | |||
237 | /** |
||
238 | * Query scope for models that have any of the given tags. |
||
239 | * |
||
240 | * @param Builder $query |
||
241 | * @param array|string $tags |
||
242 | * |
||
243 | * @return Builder |
||
244 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
245 | * @throws \ErrorException |
||
246 | */ |
||
247 | public function scopeWithAnyTags(Builder $query, $tags): Builder |
||
271 | |||
272 | /** |
||
273 | * Query scope for models that have any tag. |
||
274 | * |
||
275 | * @param Builder $query |
||
276 | * |
||
277 | * @return Builder |
||
278 | */ |
||
279 | public function scopeIsTagged(Builder $query): Builder |
||
284 | |||
285 | /** |
||
286 | * Query scope for models that do not have all of the given tags. |
||
287 | * |
||
288 | * @param Builder $query |
||
289 | * @param string|array $tags |
||
290 | * @param bool $includeUntagged |
||
291 | * |
||
292 | * @return Builder |
||
293 | * @throws \ErrorException |
||
294 | */ |
||
295 | public function scopeWithoutAllTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
316 | |||
317 | /** |
||
318 | * Query scope for models that do not have any of the given tags. |
||
319 | * |
||
320 | * @param Builder $query |
||
321 | * @param string|array $tags |
||
322 | * @param bool $includeUntagged |
||
323 | * |
||
324 | * @return Builder |
||
325 | * @throws \ErrorException |
||
326 | */ |
||
327 | public function scopeWithoutAnyTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
347 | |||
348 | /** |
||
349 | * Query scope for models that does not have have any tags. |
||
350 | * |
||
351 | * @param Builder $query |
||
352 | * |
||
353 | * @return Builder |
||
354 | */ |
||
355 | public function scopeIsNotTagged(Builder $query): Builder |
||
363 | |||
364 | /** |
||
365 | * @param Builder $query |
||
366 | * @param string $joinType |
||
367 | * |
||
368 | * @return Builder |
||
369 | */ |
||
370 | private function prepareTableJoin(Builder $query, string $joinType, string $alias): Builder |
||
391 | |||
392 | /** |
||
393 | * Get a collection of all the tag models used for the called class. |
||
394 | * |
||
395 | * @return Collection |
||
396 | */ |
||
397 | public static function allTagModels(): Collection |
||
401 | |||
402 | /** |
||
403 | * Get an array of all tags used for the called class. |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | public static function allTags(): array |
||
414 | |||
415 | /** |
||
416 | * Get all the tags used for the called class as a delimited string. |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | public static function allTagsList(): string |
||
424 | |||
425 | /** |
||
426 | * Rename one the tags for the called class. |
||
427 | * |
||
428 | * @param string $oldTag |
||
429 | * @param string $newTag |
||
430 | * |
||
431 | * @return int |
||
432 | */ |
||
433 | public static function renameTag(string $oldTag, string $newTag): int |
||
437 | |||
438 | /** |
||
439 | * Get the most popular tags for the called class. |
||
440 | * |
||
441 | * @param int $limit |
||
442 | * @param int $minCount |
||
443 | * |
||
444 | * @return array |
||
445 | */ |
||
446 | public static function popularTags(int $limit = null, int $minCount = 1): array |
||
453 | |||
454 | /** |
||
455 | * Get the most popular tags for the called class. |
||
456 | * |
||
457 | * @param int $limit |
||
458 | * @param int $minCount |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | public static function popularTagsNormalized(int $limit = null, int $minCount = 1): array |
||
469 | |||
470 | /** |
||
471 | * Returns the Related Pivot Key Name with the table alias. |
||
472 | * |
||
473 | * @param string $alias |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | private function getQualifiedRelatedPivotKeyNameWithAlias(string $alias): string |
||
484 | |||
485 | /** |
||
486 | * Returns the Foreign Pivot Key Name with the table alias. |
||
487 | * |
||
488 | * @param string $alias |
||
489 | * |
||
490 | * @return string |
||
491 | */ |
||
492 | private function getQualifiedForeignPivotKeyNameWithAlias(string $alias): string |
||
499 | |||
500 | } |
||
501 |
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.