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 |
||
7 | trait HasSlug |
||
8 | { |
||
9 | /** @var \Padosoft\Sluggable\SlugOptions */ |
||
10 | protected $slugOptions; |
||
11 | |||
12 | /** |
||
13 | * Boot the trait. |
||
14 | */ |
||
15 | protected static function bootHasSlug() |
||
25 | |||
26 | /** |
||
27 | * Add the slug to the model. |
||
28 | */ |
||
29 | protected function addSlug() |
||
45 | |||
46 | /** |
||
47 | * Retrive a specifice SlugOptions for this model, or return default SlugOptions |
||
48 | */ |
||
49 | protected function getSlugOptionsOrDefault() |
||
57 | |||
58 | /** |
||
59 | * Generate a non unique slug for this record. |
||
60 | * @return string |
||
61 | * @throws InvalidOption |
||
62 | */ |
||
63 | protected function generateNonUniqueSlug(): string |
||
71 | |||
72 | /** |
||
73 | * Determine if a custom slug has been saved. |
||
74 | * @return bool |
||
75 | */ |
||
76 | protected function hasCustomSlugBeenUsed(): bool |
||
85 | |||
86 | /** |
||
87 | * Get the string that should be used as base for the slug. |
||
88 | * @return string |
||
89 | * @throws InvalidOption |
||
90 | */ |
||
91 | protected function getSlugSourceString(): string |
||
112 | |||
113 | /** |
||
114 | * Get the correct field(s) from to generate slug |
||
115 | * @param string|array|callable $fieldName |
||
116 | * @return string|array |
||
117 | */ |
||
118 | protected function getSlugFrom($fieldName) |
||
156 | |||
157 | /** |
||
158 | * Make the given slug unique. |
||
159 | * @param string $slug |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function makeSlugUnique(string $slug): string |
||
173 | |||
174 | /** |
||
175 | * Determine if a record exists with the given slug. |
||
176 | * @param string $slug |
||
177 | * @return bool |
||
178 | */ |
||
179 | protected function otherRecordExistsWithSlug(string $slug): bool |
||
185 | |||
186 | /** |
||
187 | * This function will throw an exception when any of the options is missing or invalid. |
||
188 | * @throws InvalidOption |
||
189 | */ |
||
190 | protected function guardAgainstInvalidSlugOptions() |
||
204 | |||
205 | /** |
||
206 | * @param $slugFrom |
||
207 | * @param string $separator |
||
208 | * @return string |
||
209 | */ |
||
210 | protected function getImplodeSourceString($slugFrom, string $separator) : string |
||
222 | } |
||
223 |