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 HasSlug 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 HasSlug, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | trait HasSlug |
||
9 | { |
||
10 | /** @var \Padosoft\Sluggable\SlugOptions */ |
||
11 | protected $slugOptions; |
||
12 | |||
13 | /** |
||
14 | * Boot the trait. |
||
15 | */ |
||
16 | protected static function bootHasSlug() |
||
26 | |||
27 | /** |
||
28 | * Add the slug to the model. |
||
29 | */ |
||
30 | protected function addSlug() |
||
44 | |||
45 | /** |
||
46 | * Retrive a specifice SlugOptions for this model, or return default SlugOptions |
||
47 | */ |
||
48 | protected function getSlugOptionsOrDefault() |
||
56 | |||
57 | /** |
||
58 | * Generate a non unique slug for this record. |
||
59 | * @return string |
||
60 | * @throws InvalidOption |
||
61 | */ |
||
62 | protected function generateNonUniqueSlug(): string |
||
81 | |||
82 | /** |
||
83 | * Determine if a custom slug has been saved. |
||
84 | * @return bool |
||
85 | */ |
||
86 | protected function hasCustomSlugBeenUsed(): bool |
||
94 | |||
95 | /** |
||
96 | * Determine if a custom slug has been saved. |
||
97 | * @return bool |
||
98 | */ |
||
99 | protected function hasSlugBeenUsed(): bool |
||
108 | |||
109 | /** |
||
110 | * Get the string that should be used as base for the slug. |
||
111 | * @return string |
||
112 | * @throws InvalidOption |
||
113 | */ |
||
114 | protected function getSlugSourceString(): string |
||
135 | |||
136 | /** |
||
137 | * Get the correct field(s) from to generate slug |
||
138 | * @param string|array|callable $fieldName |
||
139 | * @return string|array |
||
140 | */ |
||
141 | protected function getSlugFrom($fieldName) |
||
179 | |||
180 | /** |
||
181 | * Make the given slug unique. |
||
182 | * @param string $slug |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function makeSlugUnique(string $slug): string |
||
196 | |||
197 | /** |
||
198 | * Determine if a record exists with the given slug. |
||
199 | * @param string $slug |
||
200 | * @return bool |
||
201 | */ |
||
202 | protected function otherRecordExistsWithSlug(string $slug): bool |
||
208 | |||
209 | /** |
||
210 | * This function will throw an exception when any of the options is missing or invalid. |
||
211 | * @throws InvalidOption |
||
212 | */ |
||
213 | protected function guardAgainstInvalidSlugOptions() |
||
227 | |||
228 | /** |
||
229 | * @param $slugFrom |
||
230 | * @param string $separator |
||
231 | * @return string |
||
232 | */ |
||
233 | protected function getImplodeSourceString($slugFrom, string $separator): string |
||
245 | |||
246 | /** |
||
247 | * |
||
248 | * SCOPE HELPERS |
||
249 | * |
||
250 | */ |
||
251 | |||
252 | /** |
||
253 | * Query scope for finding a model by its slug field. |
||
254 | * |
||
255 | * @param \Illuminate\Database\Eloquent\Builder $scope |
||
256 | * @param string $slug |
||
257 | * @return \Illuminate\Database\Eloquent\Builder |
||
258 | */ |
||
259 | public function scopeWhereSlug(\Illuminate\Database\Eloquent\Builder $scope, $slug) |
||
264 | |||
265 | /** |
||
266 | * Find a model by its slug field. |
||
267 | * |
||
268 | * @param string $slug |
||
269 | * @param array $columns |
||
270 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null |
||
271 | */ |
||
272 | public static function findBySlug($slug, array $columns = ['*']) |
||
276 | |||
277 | /** |
||
278 | * Find a model by its slug field or throw an exception. |
||
279 | * |
||
280 | * @param string $slug |
||
281 | * @param array $columns |
||
282 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection |
||
283 | * |
||
284 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
285 | */ |
||
286 | public static function findBySlugOrFail($slug, array $columns = ['*']) |
||
290 | } |
||
291 |
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.