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; |
||
14 | class SlugService |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var \Illuminate\Database\Eloquent\Model; |
||
19 | */ |
||
20 | protected $model; |
||
21 | |||
22 | /** |
||
23 | * Slug the current model. |
||
24 | * |
||
25 | * @param \Illuminate\Database\Eloquent\Model $model |
||
26 | * @param bool $force |
||
27 | * @return bool |
||
28 | */ |
||
29 | public function slug(Model $model, $force = false) |
||
52 | |||
53 | /** |
||
54 | * Get the sluggable configuration for the current model, |
||
55 | * including default values where not specified. |
||
56 | * |
||
57 | * @param array $overrides |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getConfiguration(array $overrides = []) |
||
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 | * @return null|string |
||
77 | */ |
||
78 | public function buildSlug($attribute, array $config, $force = null) |
||
79 | { |
||
80 | $slug = $this->model->getAttribute($attribute); |
||
81 | |||
82 | if ($force || $this->needsSlugging($attribute, $config)) { |
||
83 | $source = $this->getSlugSource($config['source']); |
||
84 | |||
85 | if ($source) { |
||
86 | $slug = $this->generateSlug($source, $config, $attribute); |
||
87 | |||
88 | $slug = $this->validateSlug($slug, $config, $attribute); |
||
89 | |||
90 | if ($config['unique']) { |
||
91 | $slug = $this->makeSlugUnique($slug, $config, $attribute); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return $slug; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Determines whether the model needs slugging. |
||
101 | * |
||
102 | * @param string $attribute |
||
103 | * @param array $config |
||
104 | * @return bool |
||
105 | */ |
||
106 | protected function needsSlugging($attribute, array $config) |
||
118 | |||
119 | /** |
||
120 | * Get the source string for the slug. |
||
121 | * |
||
122 | * @param mixed $from |
||
123 | * @return string |
||
124 | */ |
||
125 | protected function getSlugSource($from) |
||
137 | |||
138 | /** |
||
139 | * Generate a slug from the given source string. |
||
140 | * |
||
141 | * @param string $source |
||
142 | * @param array $config |
||
143 | * @param string $attribute |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function generateSlug($source, array $config, $attribute) |
||
167 | |||
168 | /** |
||
169 | * Return a class that has a `slugify()` method, used to convert |
||
170 | * strings into slugs. |
||
171 | * |
||
172 | * @return Slugify |
||
173 | */ |
||
174 | protected function getSlugEngine($attribute) |
||
191 | |||
192 | /** |
||
193 | * Checks that the given slug is not a reserved word. |
||
194 | * |
||
195 | * @param string $slug |
||
196 | * @param array $config |
||
197 | * @param string $attribute |
||
198 | * @return string |
||
199 | */ |
||
200 | protected function validateSlug($slug, array $config, $attribute) |
||
224 | |||
225 | /** |
||
226 | * Checks if the slug should be unique, and makes it so if needed. |
||
227 | * |
||
228 | * @param string $slug |
||
229 | * @param array $config |
||
230 | * @param string $attribute |
||
231 | * @return string |
||
232 | */ |
||
233 | protected function makeSlugUnique($slug, array $config, $attribute) |
||
267 | |||
268 | /** |
||
269 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
270 | * |
||
271 | * @param string $slug |
||
272 | * @param string $separator |
||
273 | * @param \Illuminate\Support\Collection $list |
||
274 | * @return string |
||
275 | */ |
||
276 | protected function generateSuffix($slug, $separator, Collection $list) |
||
295 | |||
296 | /** |
||
297 | * Get all existing slugs that are similar to the given slug. |
||
298 | * |
||
299 | * @param string $slug |
||
300 | * @param string $attribute |
||
301 | * @param array $config |
||
302 | * @return \Illuminate\Support\Collection |
||
303 | */ |
||
304 | protected function getExistingSlugs($slug, $attribute, array $config) |
||
324 | |||
325 | /** |
||
326 | * Does this model use softDeleting? |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | protected function usesSoftDeleting() |
||
334 | |||
335 | /** |
||
336 | * Generate a unique slug for a given string. |
||
337 | * |
||
338 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
339 | * @param string $attribute |
||
340 | * @param string $fromString |
||
341 | * @return string |
||
342 | */ |
||
343 | public static function createSlug($model, $attribute, $fromString) |
||
361 | |||
362 | /** |
||
363 | * @param \Illuminate\Database\Eloquent\Model $model |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setModel(Model $model) |
||
372 | } |
||
373 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.