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; |
||
12 | class SlugService |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var \Illuminate\Database\Eloquent\Model; |
||
17 | */ |
||
18 | protected $model; |
||
19 | |||
20 | /** |
||
21 | * Slug the current model. |
||
22 | * |
||
23 | * @param \Illuminate\Database\Eloquent\Model $model |
||
24 | * @param bool $force |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | public function slug(Model $model, bool $force = false): bool |
||
29 | { |
||
30 | $this->setModel($model); |
||
31 | |||
32 | $attributes = []; |
||
33 | |||
34 | foreach ($this->model->sluggable() as $attribute => $config) { |
||
35 | if (is_numeric($attribute)) { |
||
36 | $attribute = $config; |
||
37 | $config = $this->getConfiguration(); |
||
38 | } else { |
||
39 | $config = $this->getConfiguration($config); |
||
40 | } |
||
41 | |||
42 | $slug = $this->buildSlug($attribute, $config, $force); |
||
43 | |||
44 | $this->model->setAttribute($attribute, $slug); |
||
45 | |||
46 | $attributes[] = $attribute; |
||
47 | } |
||
48 | |||
49 | return $this->model->isDirty($attributes); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Get the sluggable configuration for the current model, |
||
54 | * including default values where not specified. |
||
55 | * |
||
56 | * @param array $overrides |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getConfiguration(array $overrides = []): array |
||
61 | { |
||
62 | $defaultConfig = config('sluggable', []); |
||
63 | |||
64 | return array_merge($defaultConfig, $overrides); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Build the slug for the given attribute of the current model. |
||
69 | * |
||
70 | * @param string $attribute |
||
71 | * @param array $config |
||
72 | * @param bool $force |
||
73 | * |
||
74 | * @return null|string |
||
75 | */ |
||
76 | public function buildSlug(string $attribute, array $config, bool $force = null) |
||
92 | |||
93 | /** |
||
94 | * Determines whether the model needs slugging. |
||
95 | * |
||
96 | * @param string $attribute |
||
97 | * @param array $config |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | protected function needsSlugging(string $attribute, array $config): bool |
||
116 | |||
117 | /** |
||
118 | * Get the source string for the slug. |
||
119 | * |
||
120 | * @param mixed $from |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | protected function getSlugSource($from): string |
||
141 | |||
142 | /** |
||
143 | * Generate a slug from the given source string. |
||
144 | * |
||
145 | * @param string $source |
||
146 | * @param array $config |
||
147 | * @param string $attribute |
||
148 | * |
||
149 | * @return string |
||
150 | * @throws \UnexpectedValueException |
||
151 | */ |
||
152 | protected function generateSlug(string $source, array $config, string $attribute): string |
||
181 | |||
182 | /** |
||
183 | * Return a class that has a `slugify()` method, used to convert |
||
184 | * strings into slugs. |
||
185 | * |
||
186 | * @param string $attribute |
||
187 | * |
||
188 | * @return \Cocur\Slugify\Slugify |
||
189 | */ |
||
190 | protected function getSlugEngine(string $attribute): Slugify |
||
207 | |||
208 | /** |
||
209 | * Checks that the given slug is not a reserved word. |
||
210 | * |
||
211 | * @param string $slug |
||
212 | * @param array $config |
||
213 | * @param string $attribute |
||
214 | * |
||
215 | * @return string |
||
216 | * @throws \UnexpectedValueException |
||
217 | */ |
||
218 | protected function validateSlug(string $slug, array $config, string $attribute): string |
||
251 | |||
252 | /** |
||
253 | * Checks if the slug should be unique, and makes it so if needed. |
||
254 | * |
||
255 | * @param string $slug |
||
256 | * @param array $config |
||
257 | * @param string $attribute |
||
258 | * |
||
259 | * @return string |
||
260 | * @throws \UnexpectedValueException |
||
261 | */ |
||
262 | protected function makeSlugUnique(string $slug, array $config, string $attribute): string |
||
310 | |||
311 | /** |
||
312 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
313 | * |
||
314 | * @param string $slug |
||
315 | * @param string $separator |
||
316 | * @param \Illuminate\Support\Collection $list |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | protected function generateSuffix(string $slug, string $separator, Collection $list): string |
||
339 | |||
340 | /** |
||
341 | * Get all existing slugs that are similar to the given slug. |
||
342 | * |
||
343 | * @param string $slug |
||
344 | * @param string $attribute |
||
345 | * @param array $config |
||
346 | * |
||
347 | * @return \Illuminate\Support\Collection |
||
348 | */ |
||
349 | protected function getExistingSlugs(string $slug, string $attribute, array $config): Collection |
||
374 | |||
375 | /** |
||
376 | * Does this model use softDeleting? |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | protected function usesSoftDeleting(): bool |
||
384 | |||
385 | /** |
||
386 | * Generate a unique slug for a given string. |
||
387 | * |
||
388 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
389 | * @param string $attribute |
||
390 | * @param string $fromString |
||
391 | * @param array|null $config |
||
392 | * |
||
393 | * @return string |
||
394 | * @throws \UnexpectedValueException |
||
395 | */ |
||
396 | public static function createSlug($model, string $attribute, string $fromString, array $config = null): string |
||
418 | |||
419 | /** |
||
420 | * @param \Illuminate\Database\Eloquent\Model $model |
||
421 | * |
||
422 | * @return $this |
||
423 | */ |
||
424 | public function setModel(Model $model) |
||
430 | } |
||
431 |
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.