Complex classes like SlugService 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 SlugService, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Cviebrock\EloquentSluggable\Services; |
||
| 13 | class SlugService |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var \Illuminate\Database\Eloquent\Model |
||
| 18 | * @var \Cviebrock\EloquentSluggable\Sluggable |
||
| 19 | */ |
||
| 20 | protected $model; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Slug the current model. |
||
| 24 | * |
||
| 25 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 26 | * @param bool $force |
||
| 27 | * |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | public function slug(Model $model, bool $force = false): bool |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the sluggable configuration for the current model, |
||
| 57 | * including default values where not specified. |
||
| 58 | * |
||
| 59 | * @param array $overrides |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function getConfiguration(array $overrides = []): array |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Build the slug for the given attribute of the current model. |
||
| 72 | * |
||
| 73 | * @param string $attribute |
||
| 74 | * @param array $config |
||
| 75 | * @param bool $force |
||
| 76 | * |
||
| 77 | * @return null|string |
||
| 78 | */ |
||
| 79 | public function buildSlug(string $attribute, array $config, bool $force = null): ?string |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Determines whether the model needs slugging. |
||
| 98 | * |
||
| 99 | * @param string $attribute |
||
| 100 | * @param array $config |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | protected function needsSlugging(string $attribute, array $config): bool |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get the source string for the slug. |
||
| 125 | * |
||
| 126 | * @param mixed $from |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | protected function getSlugSource($from): string |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Generate a slug from the given source string. |
||
| 150 | * |
||
| 151 | * @param string $source |
||
| 152 | * @param array $config |
||
| 153 | * @param string $attribute |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | * @throws \UnexpectedValueException |
||
| 157 | */ |
||
| 158 | protected function generateSlug(string $source, array $config, string $attribute): string |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Return a class that has a `slugify()` method, used to convert |
||
| 190 | * strings into slugs. |
||
| 191 | * |
||
| 192 | * @param string $attribute |
||
| 193 | * |
||
| 194 | * @param array $config |
||
| 195 | * @return \Cocur\Slugify\Slugify |
||
| 196 | */ |
||
| 197 | protected function getSlugEngine(string $attribute, array $config): Slugify |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Checks that the given slug is not a reserved word. |
||
| 215 | * |
||
| 216 | * @param string $slug |
||
| 217 | * @param array $config |
||
| 218 | * @param string $attribute |
||
| 219 | * |
||
| 220 | * @return string |
||
| 221 | * @throws \UnexpectedValueException |
||
| 222 | */ |
||
| 223 | protected function validateSlug(string $slug, array $config, string $attribute): string |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Checks if the slug should be unique, and makes it so if needed. |
||
| 261 | * |
||
| 262 | * @param string $slug |
||
| 263 | * @param array $config |
||
| 264 | * @param string $attribute |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | * @throws \UnexpectedValueException |
||
| 268 | */ |
||
| 269 | protected function makeSlugUnique(string $slug, array $config, string $attribute): string |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
| 322 | * |
||
| 323 | * @param string $slug |
||
| 324 | * @param string $separator |
||
| 325 | * @param \Illuminate\Support\Collection $list |
||
| 326 | * @param mixed $firstSuffix |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | protected function generateSuffix(string $slug, string $separator, Collection $list, $firstSuffix): string |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get all existing slugs that are similar to the given slug. |
||
| 355 | * |
||
| 356 | * @param string $slug |
||
| 357 | * @param string $attribute |
||
| 358 | * @param array $config |
||
| 359 | * |
||
| 360 | * @return \Illuminate\Support\Collection |
||
| 361 | */ |
||
| 362 | protected function getExistingSlugs(string $slug, string $attribute, array $config): Collection |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Does this model use softDeleting? |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | protected function usesSoftDeleting(): bool |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Generate a unique slug for a given string. |
||
| 398 | * |
||
| 399 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
| 400 | * @param string $attribute |
||
| 401 | * @param string $fromString |
||
| 402 | * @param array|null $config |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | * @throws \InvalidArgumentException |
||
| 406 | * @throws \UnexpectedValueException |
||
| 407 | */ |
||
| 408 | public static function createSlug($model, string $attribute, string $fromString, array $config = null): string |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setModel(Model $model): self |
||
| 446 | } |
||
| 447 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.