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 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | View Code Duplication | public function slug(Model $model, bool $force = false): bool |
|
53 | |||
54 | /** |
||
55 | * Get the sluggable configuration for the current model, |
||
56 | * including default values where not specified. |
||
57 | * |
||
58 | * @param array $overrides |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | public function getConfiguration(array $overrides = []): array |
||
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 | * |
||
76 | * @return null|string |
||
77 | */ |
||
78 | public function buildSlug(string $attribute, array $config, bool $force = null) |
||
130 | |||
131 | /** |
||
132 | * Determines whether the model needs slugging. |
||
133 | * |
||
134 | * @param string $attribute |
||
135 | * @param array $config |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | View Code Duplication | protected function needsSlugging(string $attribute, array $config): bool |
|
154 | |||
155 | /** |
||
156 | * Get the source strings for the slug. |
||
157 | * |
||
158 | * @param mixed $from |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | View Code Duplication | protected function getSlugSources($from): array |
|
179 | |||
180 | /** |
||
181 | * Generate a slug from the given source string. |
||
182 | * |
||
183 | * @param string $source |
||
184 | * @param array $config |
||
185 | * @param string $attribute |
||
186 | * |
||
187 | * @return string |
||
188 | * @throws \UnexpectedValueException |
||
189 | */ |
||
190 | View Code Duplication | protected function generateSlug(string $source, array $config, string $attribute): string |
|
219 | |||
220 | /** |
||
221 | * Return a class that has a `slugify()` method, used to convert |
||
222 | * strings into slugs. |
||
223 | * |
||
224 | * @param string $attribute |
||
225 | * |
||
226 | * @return \Cocur\Slugify\Slugify |
||
227 | */ |
||
228 | View Code Duplication | protected function getSlugEngine(string $attribute): Slugify |
|
245 | |||
246 | /** |
||
247 | * Checks that the given slug is not a reserved word. |
||
248 | * |
||
249 | * @param string $slug |
||
250 | * @param array $config |
||
251 | * @param string $attribute |
||
252 | * |
||
253 | * @return string |
||
254 | * @throws \UnexpectedValueException |
||
255 | */ |
||
256 | View Code Duplication | protected function validateSlug(string $slug, array $config, string $attribute): string |
|
289 | |||
290 | /** |
||
291 | * Checks if the slug should be unique, and makes it so if needed. |
||
292 | * |
||
293 | * @param string $slug |
||
294 | * @param array $config |
||
295 | * @param string $attribute |
||
296 | * |
||
297 | * @return string |
||
298 | * @throws \UnexpectedValueException |
||
299 | */ |
||
300 | View Code Duplication | protected function makeSlugUnique(string $slug, array $config, string $attribute): string |
|
348 | |||
349 | /** |
||
350 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
351 | * |
||
352 | * @param string $slug |
||
353 | * @param string $separator |
||
354 | * @param \Illuminate\Support\Collection $list |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | View Code Duplication | protected function generateSuffix(string $slug, string $separator, Collection $list): string |
|
377 | |||
378 | /** |
||
379 | * Get all existing slugs that are similar to the given slug. |
||
380 | * |
||
381 | * @param string $slug |
||
382 | * @param string $attribute |
||
383 | * @param array $config |
||
384 | * |
||
385 | * @return \Illuminate\Support\Collection |
||
386 | */ |
||
387 | View Code Duplication | protected function getExistingSlugs(string $slug, string $attribute, array $config): Collection |
|
412 | |||
413 | /** |
||
414 | * Does this model use softDeleting? |
||
415 | * |
||
416 | * @return bool |
||
417 | */ |
||
418 | protected function usesSoftDeleting(): bool |
||
422 | |||
423 | /** |
||
424 | * Generate a unique slug for a given string. |
||
425 | * |
||
426 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
427 | * @param string $attribute |
||
428 | * @param string $fromString |
||
429 | * @param array|null $config |
||
430 | * |
||
431 | * @return string |
||
432 | * @throws \InvalidArgumentException |
||
433 | * @throws \UnexpectedValueException |
||
434 | */ |
||
435 | View Code Duplication | public static function createSlug($model, string $attribute, string $fromString, array $config = null): string |
|
461 | |||
462 | /** |
||
463 | * @param \Illuminate\Database\Eloquent\Model $model |
||
464 | * |
||
465 | * @return $this |
||
466 | */ |
||
467 | public function setModel(Model $model) |
||
473 | } |
||
474 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.