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; |
||
| 16 | trait Taggable |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Boot the trait. |
||
| 21 | * |
||
| 22 | 23 | * Listen for the deleting event of a model, then remove the relation between it and tags |
|
| 23 | */ |
||
| 24 | 23 | protected static function bootTaggable() |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Get a collection of all tags the model has. |
||
| 35 | 23 | * |
|
| 36 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
| 37 | 23 | */ |
|
| 38 | public function tags(): MorphToMany |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Attach one or multiple tags to the model. |
||
| 47 | * |
||
| 48 | * @param string|array $tags |
||
| 49 | * |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | public function tag($tags) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Detach one or multiple tags from the model. |
||
| 66 | * |
||
| 67 | * @param string|array $tags |
||
| 68 | * |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | 1 | public function untag($tags) |
|
| 81 | 2 | ||
| 82 | /** |
||
| 83 | 2 | * Remove all tags from the model and assign the given ones. |
|
| 84 | * |
||
| 85 | 2 | * @param string|array $tags |
|
| 86 | * |
||
| 87 | * @return $this |
||
| 88 | */ |
||
| 89 | public function retag($tags) |
||
| 93 | 23 | ||
| 94 | /** |
||
| 95 | 23 | * Remove all tags from the model. |
|
| 96 | * |
||
| 97 | 23 | * @return $this |
|
| 98 | 23 | */ |
|
| 99 | 23 | public function detag() |
|
| 105 | |||
| 106 | /** |
||
| 107 | 1 | * Add one tag to the model. |
|
| 108 | * |
||
| 109 | 1 | * @param string $tagName |
|
| 110 | */ |
||
| 111 | 1 | protected function addOneTag(string $tagName) |
|
| 120 | |||
| 121 | /** |
||
| 122 | 13 | * Remove one tag from the model |
|
| 123 | * |
||
| 124 | 2 | * @param string $tagName |
|
| 125 | 13 | */ |
|
| 126 | protected function removeOneTag(string $tagName) |
||
| 134 | |||
| 135 | 2 | /** |
|
| 136 | * Get all the tags of the model as a delimited string. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getTagListAttribute(): string |
||
| 144 | |||
| 145 | 11 | /** |
|
| 146 | * Get all normalized tags of a model as a delimited string. |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getTagListNormalizedAttribute(): string |
||
| 154 | |||
| 155 | 2 | /** |
|
| 156 | * Get all tags of a model as an array. |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function getTagArrayAttribute(): array |
||
| 164 | |||
| 165 | /** |
||
| 166 | 1 | * Get all normalized tags of a model as an array. |
|
| 167 | * |
||
| 168 | 1 | * @return array |
|
| 169 | */ |
||
| 170 | public function getTagArrayNormalizedAttribute(): array |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Query scope for models that have all of the given tags. |
||
| 177 | * |
||
| 178 | * @param Builder $query |
||
| 179 | * @param array|string $tags |
||
| 180 | * |
||
| 181 | * @return Builder |
||
| 182 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
| 183 | 1 | * @throws \ErrorException |
|
| 184 | */ |
||
| 185 | 1 | public function scopeWithAllTags(Builder $query, $tags): Builder |
|
| 216 | 2 | ||
| 217 | /** |
||
| 218 | 2 | * Query scope for models that have any of the given tags. |
|
| 219 | * |
||
| 220 | * @param Builder $query |
||
| 221 | * @param array|string $tags |
||
| 222 | * |
||
| 223 | * @return Builder |
||
| 224 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
| 225 | * @throws \ErrorException |
||
| 226 | 1 | */ |
|
| 227 | public function scopeWithAnyTags(Builder $query, $tags): Builder |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Query scope for models that have any tag. |
||
| 254 | * |
||
| 255 | * @param Builder $query |
||
| 256 | * |
||
| 257 | * @return Builder |
||
| 258 | */ |
||
| 259 | public function scopeIsTagged(Builder $query): Builder |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Query scope for models that do not have all of the given tags. |
||
| 267 | * |
||
| 268 | * @param Builder $query |
||
| 269 | * @param string|array $tags |
||
| 270 | * @param bool $includeUntagged |
||
| 271 | * |
||
| 272 | * @return Builder |
||
| 273 | * @throws \ErrorException |
||
| 274 | */ |
||
| 275 | public function scopeWithoutAllTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Query scope for models that do not have any 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 scopeWithoutAnyTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Query scope for models that does not have have any tags. |
||
| 330 | * |
||
| 331 | * @param Builder $query |
||
| 332 | * |
||
| 333 | * @return Builder |
||
| 334 | */ |
||
| 335 | public function scopeIsNotTagged(Builder $query): Builder |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param Builder $query |
||
| 346 | * @param string $joinType |
||
| 347 | * |
||
| 348 | * @return Builder |
||
| 349 | */ |
||
| 350 | private function prepareTableJoin(Builder $query, string $joinType, string $alias): Builder |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get a collection of all the tag models used for the called class. |
||
| 374 | * |
||
| 375 | * @return Collection |
||
| 376 | */ |
||
| 377 | public static function allTagModels(): Collection |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get an array of all tags used for the called class. |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | public static function allTags(): array |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get all the tags used for the called class as a delimited string. |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public static function allTagsList(): string |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Rename one the tags for the called class. |
||
| 407 | * |
||
| 408 | * @param string $oldTag |
||
| 409 | * @param string $newTag |
||
| 410 | * |
||
| 411 | * @return int |
||
| 412 | */ |
||
| 413 | public static function renameTag(string $oldTag, string $newTag): int |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get the most popular tags for the called class. |
||
| 420 | * |
||
| 421 | * @param int $limit |
||
| 422 | * @param int $minCount |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | public static function popularTags(int $limit = null, int $minCount = 1): array |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get the most popular tags for the called class. |
||
| 436 | * |
||
| 437 | * @param int $limit |
||
| 438 | * @param int $minCount |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public static function popularTagsNormalized(int $limit = null, int $minCount = 1): array |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Returns the Related Pivot Key Name with the table alias. |
||
| 452 | * |
||
| 453 | * @param $alias |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | private function getQualifiedRelatedPivotKeyNameWithAlias($alias) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the Foreign Pivot Key Name with the table alias. |
||
| 466 | * |
||
| 467 | * @param $alias |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | private function getQualifiedForeignPivotKeyNameWithAlias($alias) |
||
| 477 | |||
| 478 | } |
||
| 479 |
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.