Complex classes like HasUrl 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 HasUrl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | trait HasUrl |
||
17 | { |
||
18 | use HasSlug; |
||
19 | |||
20 | /** |
||
21 | * The container for all the options necessary for this trait. |
||
22 | * Options can be viewed in the Neurony\Url\Options\UrlOptions file. |
||
23 | * |
||
24 | * @var UrlOptions |
||
25 | */ |
||
26 | protected $urlOptions; |
||
27 | |||
28 | /** |
||
29 | * Flag to manually enable/disable the url generation only for the current request. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected static $generateUrl = true; |
||
34 | |||
35 | /** |
||
36 | * Set the options for the HasUrl trait. |
||
37 | * |
||
38 | * @return UrlOptions |
||
39 | */ |
||
40 | abstract public function getUrlOptions(): UrlOptions; |
||
41 | |||
42 | /** |
||
43 | * Boot the trait. |
||
44 | * |
||
45 | * Check if the "getUrlOptions" method has been implemented on the underlying model class. |
||
46 | * Eager load urls through anonymous global scope. |
||
47 | * Trigger eloquent events to create, update, delete url. |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | public static function bootHasUrl(): void |
||
81 | |||
82 | /** |
||
83 | * Get the model's url. |
||
84 | * |
||
85 | * @return MorphOne |
||
86 | */ |
||
87 | public function url(): MorphOne |
||
91 | |||
92 | /** |
||
93 | * Get the model's direct url string. |
||
94 | * |
||
95 | * @param bool|null $secure |
||
96 | * @return \Illuminate\Contracts\Routing\UrlGenerator|string|null |
||
97 | */ |
||
98 | public function getUrl(?bool $secure = null): ?string |
||
106 | |||
107 | /** |
||
108 | * Get the model's direct uri string. |
||
109 | * |
||
110 | * @return string|null |
||
111 | */ |
||
112 | public function getUri(): ?string |
||
116 | |||
117 | /** |
||
118 | * Disable the url generation manually only for the current request. |
||
119 | * |
||
120 | * @return static |
||
121 | */ |
||
122 | public function doNotGenerateUrl(): self |
||
128 | |||
129 | /** |
||
130 | * Get the options for the HasSlug trait. |
||
131 | * |
||
132 | * @return SlugOptions |
||
133 | * @throws Exception |
||
134 | */ |
||
135 | public function getSlugOptions(): SlugOptions |
||
143 | |||
144 | /** |
||
145 | * @return void |
||
146 | * @throws Exception |
||
147 | */ |
||
148 | public function saveUrl(): void |
||
158 | |||
159 | /** |
||
160 | * Create a new url for the model. |
||
161 | * |
||
162 | * @return void |
||
163 | * @throws Exception |
||
164 | */ |
||
165 | public function createUrl(): void |
||
181 | |||
182 | /** |
||
183 | * Update the existing url for the model. |
||
184 | * |
||
185 | * @return void |
||
186 | * @throws Exception |
||
187 | */ |
||
188 | public function updateUrl(): void |
||
214 | |||
215 | /** |
||
216 | * Delete the url for the just deleted model. |
||
217 | * |
||
218 | * @return void |
||
219 | * @throws UrlException |
||
220 | */ |
||
221 | public function deleteUrl(): void |
||
229 | |||
230 | /** |
||
231 | * Synchronize children urls for the actual model's url. |
||
232 | * Saves all children urls of the model in use with the new parent model's slug. |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | protected function updateUrlsInCascade(): void |
||
251 | |||
252 | /** |
||
253 | * Get the full relative url. |
||
254 | * The full url will also include the prefix and suffix if any was provided. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function buildFullUrl(): string |
||
268 | |||
269 | /** |
||
270 | * Build the url segment. |
||
271 | * This can be either "prefix" or "suffix". |
||
272 | * The accepted parameter $type accepts only "prefix" and "suffix" as it's value. |
||
273 | * Otherwise, the method will return an empty string. |
||
274 | * |
||
275 | * @param string $type |
||
276 | * @return string |
||
277 | */ |
||
278 | protected function buildUrlSegment(string $type): string |
||
296 | |||
297 | /** |
||
298 | * Both instantiate the url options as well as validate their contents. |
||
299 | * |
||
300 | * @return void |
||
301 | * @throws Exception |
||
302 | */ |
||
303 | protected function initUrlOptions(): void |
||
311 | |||
312 | /** |
||
313 | * Check if mandatory slug options have been properly set from the model. |
||
314 | * Check if $fromField and $toField have been set. |
||
315 | * |
||
316 | * @return void |
||
317 | * @throws UrlException |
||
318 | */ |
||
319 | protected function validateUrlOptions(): void |
||
333 | } |
||
334 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.