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) |
||
119 | |||
120 | /** |
||
121 | * Get the source string for the slug. |
||
122 | * |
||
123 | * @param mixed $from |
||
124 | * @return string |
||
125 | */ |
||
126 | protected function getSlugSource($from) |
||
138 | |||
139 | /** |
||
140 | * Generate a slug from the given source string. |
||
141 | * |
||
142 | * @param string $source |
||
143 | * @param array $config |
||
144 | * @param string $attribute |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function generateSlug($source, array $config, $attribute) |
||
148 | { |
||
149 | $separator = $config['separator']; |
||
150 | $method = $config['method']; |
||
151 | $maxLength = $config['maxLength']; |
||
152 | |||
153 | if ($method === null) { |
||
154 | $slugEngine = $this->getSlugEngine($attribute); |
||
155 | $slug = $slugEngine->slugify($source, $separator); |
||
156 | } elseif (is_callable($method)) { |
||
157 | $slug = call_user_func($method, $source, $separator); |
||
158 | } else { |
||
159 | throw new \UnexpectedValueException('Sluggable "method" for ' . get_class($this->model) . ':' . $attribute . ' is not callable nor null.'); |
||
160 | } |
||
161 | |||
162 | if (is_string($slug) && $maxLength) { |
||
163 | $slug = mb_substr($slug, 0, $maxLength); |
||
164 | } |
||
165 | |||
166 | return $slug; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return a class that has a `slugify()` method, used to convert |
||
171 | * strings into slugs. |
||
172 | * |
||
173 | * @param string $attribute |
||
174 | * @return Slugify |
||
175 | */ |
||
176 | protected function getSlugEngine($attribute) |
||
193 | |||
194 | /** |
||
195 | * Checks that the given slug is not a reserved word. |
||
196 | * |
||
197 | * @param string $slug |
||
198 | * @param array $config |
||
199 | * @param string $attribute |
||
200 | * @return string |
||
201 | */ |
||
202 | protected function validateSlug($slug, array $config, $attribute) |
||
226 | |||
227 | /** |
||
228 | * Checks if the slug should be unique, and makes it so if needed. |
||
229 | * |
||
230 | * @param string $slug |
||
231 | * @param array $config |
||
232 | * @param string $attribute |
||
233 | * @return string |
||
234 | */ |
||
235 | protected function makeSlugUnique($slug, array $config, $attribute) |
||
236 | { |
||
237 | $separator = $config['separator']; |
||
238 | |||
239 | // find all models where the slug is like the current one |
||
240 | $list = $this->getExistingSlugs($slug, $attribute, $config); |
||
241 | |||
242 | // if ... |
||
243 | // a) the list is empty |
||
244 | // b) our slug isn't in the list |
||
245 | // c) our slug is in the list and it's for our model |
||
246 | // ... we are okay |
||
247 | if ( |
||
248 | $list->count() === 0 || |
||
249 | $list->contains($slug) === false || |
||
250 | ( |
||
251 | $list->has($this->model->getKey()) && |
||
252 | $list->get($this->model->getKey()) === $slug |
||
253 | ) |
||
254 | ) { |
||
255 | return $slug; |
||
256 | } |
||
257 | |||
258 | $method = $config['uniqueSuffix']; |
||
259 | if ($method === null) { |
||
260 | $suffix = $this->generateSuffix($slug, $separator, $list); |
||
261 | } elseif (is_callable($method)) { |
||
262 | $suffix = call_user_func($method, $slug, $separator, $list); |
||
263 | } else { |
||
264 | throw new \UnexpectedValueException('Sluggable "reserved" for ' . get_class($this->model) . ':' . $attribute . ' is not null, an array, or a closure that returns null/array.'); |
||
265 | } |
||
266 | |||
267 | return $slug . $separator . $suffix; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Generate a unique suffix for the given slug (and list of existing, "similar" slugs. |
||
272 | * |
||
273 | * @param string $slug |
||
274 | * @param string $separator |
||
275 | * @param \Illuminate\Support\Collection $list |
||
276 | * @return string |
||
277 | */ |
||
278 | protected function generateSuffix($slug, $separator, Collection $list) |
||
297 | |||
298 | /** |
||
299 | * Get all existing slugs that are similar to the given slug. |
||
300 | * |
||
301 | * @param string $slug |
||
302 | * @param string $attribute |
||
303 | * @param array $config |
||
304 | * @return \Illuminate\Support\Collection |
||
305 | */ |
||
306 | protected function getExistingSlugs($slug, $attribute, array $config) |
||
331 | |||
332 | /** |
||
333 | * Does this model use softDeleting? |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | protected function usesSoftDeleting() |
||
341 | |||
342 | /** |
||
343 | * Generate a unique slug for a given string. |
||
344 | * |
||
345 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
346 | * @param string $attribute |
||
347 | * @param string $fromString |
||
348 | * @param array $config |
||
349 | * @return string |
||
350 | */ |
||
351 | public static function createSlug($model, $attribute, $fromString, array $config = null) |
||
374 | |||
375 | /** |
||
376 | * @param \Illuminate\Database\Eloquent\Model $model |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function setModel(Model $model) |
||
385 | } |
||
386 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.