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 |
||
| 53 | 1 | ||
| 54 | /** |
||
| 55 | 1 | * Attach one or multiple tags to the model. |
|
| 56 | * |
||
| 57 | 1 | * @param string|array $tags |
|
| 58 | 1 | * |
|
| 59 | 1 | * @return self |
|
| 60 | */ |
||
| 61 | 1 | public function tag($tags): self |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Detach one or multiple tags from the model. |
||
| 77 | * |
||
| 78 | * @param string|array $tags |
||
| 79 | * |
||
| 80 | * @return self |
||
| 81 | 2 | */ |
|
| 82 | public function untag($tags): self |
||
| 94 | |||
| 95 | 23 | /** |
|
| 96 | * Remove all tags from the model and assign the given ones. |
||
| 97 | 23 | * |
|
| 98 | 23 | * @param string|array $tags |
|
| 99 | 23 | * |
|
| 100 | 23 | * @return self |
|
| 101 | */ |
||
| 102 | public function retag($tags): self |
||
| 106 | |||
| 107 | 1 | /** |
|
| 108 | * Remove all tags from the model. |
||
| 109 | 1 | * |
|
| 110 | * @return self |
||
| 111 | 1 | */ |
|
| 112 | 1 | public function detag(): self |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Add one tag to the model. |
||
| 121 | * |
||
| 122 | 13 | * @param string $tagName |
|
| 123 | */ |
||
| 124 | 2 | protected function addOneTag(string $tagName): void |
|
| 134 | |||
| 135 | 2 | /** |
|
| 136 | * Remove one tag from the model |
||
| 137 | * |
||
| 138 | * @param string $tagName |
||
| 139 | */ |
||
| 140 | protected function removeOneTag(string $tagName): void |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get all the tags of the model as a delimited string. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | 2 | */ |
|
| 154 | public function getTagListAttribute(): string |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get all normalized tags of a model as a delimited string. |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getTagListNormalizedAttribute(): string |
||
| 168 | 1 | ||
| 169 | /** |
||
| 170 | * Get all tags of a model as an array. |
||
| 171 | 1 | * |
|
| 172 | 1 | * @return array |
|
| 173 | */ |
||
| 174 | public function getTagArrayAttribute(): array |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get all normalized tags of a model as an array. |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | 1 | */ |
|
| 184 | public function getTagArrayNormalizedAttribute(): array |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Determine if a given tag is attached to the model. |
||
| 191 | 1 | * |
|
| 192 | 1 | * @param Tag|string $tag |
|
| 193 | 1 | * |
|
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function hasTag($tag): bool |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Query scope for models that have all of the given tags. |
||
| 209 | * |
||
| 210 | * @param Builder $query |
||
| 211 | * @param array|string $tags |
||
| 212 | * |
||
| 213 | 2 | * @return Builder |
|
| 214 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
| 215 | * @throws \ErrorException |
||
| 216 | 2 | */ |
|
| 217 | public function scopeWithAllTags(Builder $query, $tags): Builder |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Query scope for models that have any of the given tags. |
||
| 251 | * |
||
| 252 | * @param Builder $query |
||
| 253 | * @param array|string $tags |
||
| 254 | * |
||
| 255 | * @return Builder |
||
| 256 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
| 257 | * @throws \ErrorException |
||
| 258 | */ |
||
| 259 | public function scopeWithAnyTags(Builder $query, $tags): Builder |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Query scope for models that have any tag. |
||
| 286 | * |
||
| 287 | * @param Builder $query |
||
| 288 | * |
||
| 289 | * @return Builder |
||
| 290 | */ |
||
| 291 | public function scopeIsTagged(Builder $query): Builder |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Query scope for models that do not have all of the given tags. |
||
| 299 | * |
||
| 300 | * @param Builder $query |
||
| 301 | * @param string|array $tags |
||
| 302 | * @param bool $includeUntagged |
||
| 303 | * |
||
| 304 | * @return Builder |
||
| 305 | * @throws \ErrorException |
||
| 306 | */ |
||
| 307 | public function scopeWithoutAllTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Query scope for models that do not have any of the given tags. |
||
| 331 | * |
||
| 332 | * @param Builder $query |
||
| 333 | * @param string|array $tags |
||
| 334 | * @param bool $includeUntagged |
||
| 335 | * |
||
| 336 | * @return Builder |
||
| 337 | * @throws \ErrorException |
||
| 338 | */ |
||
| 339 | public function scopeWithoutAnyTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Query scope for models that does not have have any tags. |
||
| 362 | * |
||
| 363 | * @param Builder $query |
||
| 364 | * |
||
| 365 | * @return Builder |
||
| 366 | */ |
||
| 367 | public function scopeIsNotTagged(Builder $query): Builder |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param Builder $query |
||
| 378 | * @param string $joinType |
||
| 379 | * |
||
| 380 | * @return Builder |
||
| 381 | */ |
||
| 382 | private function prepareTableJoin(Builder $query, string $joinType, string $alias): Builder |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get a collection of all the tag models used for the called class. |
||
| 406 | * |
||
| 407 | * @return Collection |
||
| 408 | */ |
||
| 409 | public static function allTagModels(): Collection |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get an array of all tags used for the called class. |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public static function allTags(): array |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get all the tags used for the called class as a delimited string. |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public static function allTagsList(): string |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Rename one the tags for the called class. |
||
| 439 | * |
||
| 440 | * @param string $oldTag |
||
| 441 | * @param string $newTag |
||
| 442 | * |
||
| 443 | * @return int |
||
| 444 | */ |
||
| 445 | public static function renameTag(string $oldTag, string $newTag): int |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get the most popular tags for the called class. |
||
| 452 | * |
||
| 453 | * @param int $limit |
||
| 454 | * @param int $minCount |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | public static function popularTags(int $limit = null, int $minCount = 1): array |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the most popular tags for the called class. |
||
| 468 | * |
||
| 469 | * @param int $limit |
||
| 470 | * @param int $minCount |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | public static function popularTagsNormalized(int $limit = null, int $minCount = 1): array |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns the Related Pivot Key Name with the table alias. |
||
| 484 | * |
||
| 485 | * @param string $alias |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | private function getQualifiedRelatedPivotKeyNameWithAlias(string $alias): string |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns the Foreign Pivot Key Name with the table alias. |
||
| 499 | * |
||
| 500 | * @param string $alias |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | private function getQualifiedForeignPivotKeyNameWithAlias(string $alias): string |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Create a new alias to use on scopes to be able to combine many scopes |
||
| 514 | * |
||
| 515 | * @param string $scope |
||
| 516 | * |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | private function taggableCreateNewAlias(string $scope) |
||
| 526 | } |
||
| 527 |
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
Idableprovides a methodequalsIdthat 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.