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 | * @return bool |
||
26 | */ |
||
27 | public function slug(Model $model, $force = false) |
||
50 | |||
51 | /** |
||
52 | * Get the sluggable configuration for the current model, |
||
53 | * including default values where not specified. |
||
54 | * |
||
55 | * @param array $overrides |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getConfiguration(array $overrides = []) |
||
67 | |||
68 | /** |
||
69 | * Build the slug for the given attribute of the current model. |
||
70 | * |
||
71 | * @param string $attribute |
||
72 | * @param array $config |
||
73 | * @param bool $force |
||
74 | * @return null|string |
||
75 | */ |
||
76 | public function buildSlug($attribute, array $config, $force = null) |
||
96 | |||
97 | /** |
||
98 | * Determines whether the model needs slugging. |
||
99 | * |
||
100 | * @param string $attribute |
||
101 | * @param array $config |
||
102 | * @return bool |
||
103 | */ |
||
104 | protected function needsSlugging($attribute, array $config) |
||
116 | |||
117 | /** |
||
118 | * Get the source string for the slug. |
||
119 | * |
||
120 | * @param mixed $from |
||
121 | * @return string |
||
122 | */ |
||
123 | protected function getSlugSource($from) |
||
135 | |||
136 | /** |
||
137 | * Generate a slug from the given source string. |
||
138 | * |
||
139 | * @param string $source |
||
140 | * @param array $config |
||
141 | * @param string $attribute |
||
142 | * @return string |
||
143 | */ |
||
144 | protected function generateSlug($source, array $config, $attribute) |
||
165 | |||
166 | /** |
||
167 | * Return a class that has a `slugify()` method, used to convert |
||
168 | * strings into slugs. |
||
169 | * |
||
170 | * @return Slugify |
||
171 | */ |
||
172 | protected function getSlugEngine($attribute) |
||
189 | |||
190 | /** |
||
191 | * Checks that the given slug is not a reserved word. |
||
192 | * |
||
193 | * @param string $slug |
||
194 | * @param array $config |
||
195 | * @param string $attribute |
||
196 | * @return string |
||
197 | */ |
||
198 | protected function validateSlug($slug, array $config, $attribute) |
||
222 | |||
223 | /** |
||
224 | * Checks if the slug should be unique, and makes it so if needed. |
||
225 | * |
||
226 | * @param string $slug |
||
227 | * @param array $config |
||
228 | * @param string $attribute |
||
229 | * @return string |
||
230 | */ |
||
231 | protected function makeSlugUnique($slug, array $config, $attribute) |
||
265 | |||
266 | /** |
||
267 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
268 | * |
||
269 | * @param string $slug |
||
270 | * @param string $separator |
||
271 | * @param \Illuminate\Support\Collection $list |
||
272 | * @return string |
||
273 | */ |
||
274 | protected function generateSuffix($slug, $separator, Collection $list) |
||
293 | |||
294 | /** |
||
295 | * Get all existing slugs that are similar to the given slug. |
||
296 | * |
||
297 | * @param string $slug |
||
298 | * @param string $attribute |
||
299 | * @param array $config |
||
300 | * @return \Illuminate\Support\Collection |
||
301 | */ |
||
302 | protected function getExistingSlugs($slug, $attribute, array $config) |
||
322 | |||
323 | /** |
||
324 | * Does this model use softDeleting? |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | protected function usesSoftDeleting() |
||
332 | |||
333 | /** |
||
334 | * Generate a unique slug for a given string. |
||
335 | * |
||
336 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
337 | * @param string $attribute |
||
338 | * @param string $fromString |
||
339 | * @return string |
||
340 | */ |
||
341 | public static function createSlug($model, $attribute, $fromString) |
||
359 | |||
360 | /** |
||
361 | * @param \Illuminate\Database\Eloquent\Model $model |
||
362 | * @return $this |
||
363 | */ |
||
364 | public function setModel(Model $model) |
||
370 | } |
||
371 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.