Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | */ |
||
| 19 | protected $model; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Slug the current model. |
||
| 23 | * |
||
| 24 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 25 | * @param bool $force |
||
| 26 | * @return bool |
||
| 27 | */ |
||
| 28 | public function slug(Model $model, $force = false) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the sluggable configuration for the current model, |
||
| 54 | * including default values where not specified. |
||
| 55 | * |
||
| 56 | * @param array $overrides |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getConfiguration(array $overrides = []) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Build the slug for the given attribute of the current model. |
||
| 71 | * |
||
| 72 | * @param string $attribute |
||
| 73 | * @param array $config |
||
| 74 | * @param bool $force |
||
| 75 | * @return null|string |
||
| 76 | */ |
||
| 77 | public function buildSlug($attribute, array $config, $force = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Determines whether the model needs slugging. |
||
| 100 | * |
||
| 101 | * @param string $attribute |
||
| 102 | * @param array $config |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | protected function needsSlugging($attribute, array $config) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the source string for the slug. |
||
| 120 | * |
||
| 121 | * @param mixed $from |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | protected function getSlugSource($from) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Generate a slug from the given source string. |
||
| 139 | * |
||
| 140 | * @param string $source |
||
| 141 | * @param array $config |
||
| 142 | * @param string $attribute |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | protected function generateSlug($source, array $config, $attribute) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return a class that has a `slugify()` method, used to convert |
||
| 169 | * strings into slugs. |
||
| 170 | * |
||
| 171 | * @return Slugify |
||
| 172 | */ |
||
| 173 | protected function getSlugEngine($attribute) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Checks that the given slug is not a reserved word. |
||
| 193 | * |
||
| 194 | * @param string $slug |
||
| 195 | * @param array $config |
||
| 196 | * @param string $attribute |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | protected function validateSlug($slug, array $config, $attribute) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Checks if the slug should be unique, and makes it so if needed. |
||
| 226 | * |
||
| 227 | * @param string $slug |
||
| 228 | * @param array $config |
||
| 229 | * @param string $attribute |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function makeSlugUnique($slug, array $config, $attribute) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
| 269 | * |
||
| 270 | * @param string $slug |
||
| 271 | * @param string $separator |
||
| 272 | * @param \Illuminate\Support\Collection $list |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | protected function generateSuffix($slug, $separator, Collection $list) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get all existing slugs that are similar to the given slug. |
||
| 297 | * |
||
| 298 | * @param string $slug |
||
| 299 | * @param string $attribute |
||
| 300 | * @param array $config |
||
| 301 | * @return \Illuminate\Support\Collection |
||
| 302 | */ |
||
| 303 | protected function getExistingSlugs($slug, $attribute, array $config) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Does this model use softDeleting? |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | protected function usesSoftDeleting() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Generate a unique slug for a given string. |
||
| 342 | * |
||
| 343 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
| 344 | * @param string $attribute |
||
| 345 | * @param string $fromString |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public static function createSlug($model, $attribute, $fromString) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function setModel(Model $model) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Determine the version of Laravel (or the Illuminate components) that we are running. |
||
| 380 | * |
||
| 381 | * @return mixed|string |
||
| 382 | */ |
||
| 383 | protected function getApplicationVersion() |
||
| 396 | } |
||
| 397 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.