Complex classes like BaseGroup 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 BaseGroup, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Limoncello\Core\Routing; |
||
35 | abstract class BaseGroup implements GroupInterface |
||
36 | { |
||
37 | use CallableTrait, UriTrait, HasRequestFactoryTrait, CheckCallableTrait; |
||
38 | |||
39 | use HasMiddlewareTrait { |
||
40 | addMiddleware as private addMiddlewareImpl; |
||
41 | } |
||
42 | |||
43 | use HasConfiguratorsTrait { |
||
44 | addConfigurators as private addConfiguratorsImpl; |
||
45 | } |
||
46 | |||
47 | /** Default value if routes should use request factory from its group */ |
||
48 | const USE_FACTORY_FROM_GROUP_DEFAULT = true; |
||
49 | |||
50 | /** |
||
51 | * @var null|GroupInterface |
||
52 | */ |
||
53 | private $parent; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $uriPrefix = ''; |
||
59 | |||
60 | /** |
||
61 | * @var string|null |
||
62 | */ |
||
63 | private $name = null; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $items = []; |
||
69 | |||
70 | /** |
||
71 | * @var bool |
||
72 | */ |
||
73 | private $trailSlashes = false; |
||
74 | |||
75 | /** |
||
76 | * @return self |
||
|
|||
77 | */ |
||
78 | abstract protected function createGroup(): self; |
||
79 | |||
80 | /** |
||
81 | * @param GroupInterface $parent |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | 17 | public function setParentGroup(GroupInterface $parent) |
|
91 | |||
92 | /** |
||
93 | * @param string $uriPrefix |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | 17 | public function setUriPrefix(string $uriPrefix): self |
|
103 | |||
104 | /** |
||
105 | * @param string $name |
||
106 | * |
||
107 | * @return self |
||
108 | */ |
||
109 | 1 | public function setName(string $name): self |
|
115 | |||
116 | /** |
||
117 | * @return self |
||
118 | */ |
||
119 | 17 | public function clearName(): self |
|
125 | |||
126 | /** |
||
127 | * @param bool $trailSlashes |
||
128 | * |
||
129 | * @return self |
||
130 | */ |
||
131 | 17 | public function setHasTrailSlash(bool $trailSlashes): self |
|
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 18 | public function parentGroup(): ?GroupInterface |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | 18 | public function getUriPrefix(): string |
|
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | 8 | public function getName(): ?string |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | 17 | public function getMiddleware(): array |
|
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 1 | public function addMiddleware(array $middleware): GroupInterface |
|
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | 17 | public function getContainerConfigurators(): array |
|
194 | |||
195 | /** |
||
196 | * @inheritdoc |
||
197 | */ |
||
198 | 1 | public function addContainerConfigurators(array $configurators): GroupInterface |
|
202 | |||
203 | /** |
||
204 | * @inheritdoc |
||
205 | */ |
||
206 | 17 | public function getRequestFactory(): ?callable |
|
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | */ |
||
221 | 18 | public function getRoutes(): iterable |
|
236 | |||
237 | /** |
||
238 | * @inheritdoc |
||
239 | */ |
||
240 | 17 | public function group(string $prefix, Closure $closure, array $parameters = []): GroupInterface |
|
255 | |||
256 | /** |
||
257 | * @inheritdoc |
||
258 | */ |
||
259 | 17 | public function addGroup(Closure $closure, GroupInterface $group): GroupInterface |
|
267 | |||
268 | /** |
||
269 | * @inheritdoc |
||
270 | */ |
||
271 | 18 | public function addRoute(RouteInterface $route): GroupInterface |
|
277 | |||
278 | /** |
||
279 | * @inheritdoc |
||
280 | * |
||
281 | * @throws ReflectionException |
||
282 | */ |
||
283 | 22 | public function method(string $method, string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
299 | |||
300 | /** |
||
301 | * @inheritdoc |
||
302 | * |
||
303 | * @throws ReflectionException |
||
304 | */ |
||
305 | 22 | public function get(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
309 | |||
310 | /** |
||
311 | * @inheritdoc |
||
312 | * |
||
313 | * @throws ReflectionException |
||
314 | */ |
||
315 | 16 | public function post(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
319 | |||
320 | /** |
||
321 | * @inheritdoc |
||
322 | * |
||
323 | * @throws ReflectionException |
||
324 | */ |
||
325 | 1 | public function put(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
329 | |||
330 | /** |
||
331 | * @inheritdoc |
||
332 | * |
||
333 | * @throws ReflectionException |
||
334 | */ |
||
335 | 2 | public function patch(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
339 | |||
340 | /** |
||
341 | * @inheritdoc |
||
342 | * |
||
343 | * @throws ReflectionException |
||
344 | */ |
||
345 | 10 | public function delete(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
349 | |||
350 | /** |
||
351 | * @inheritdoc |
||
352 | */ |
||
353 | 22 | public function hasTrailSlash(): bool |
|
357 | |||
358 | /** |
||
359 | * @param GroupInterface $group |
||
360 | * @param string $method |
||
361 | * @param string $uriPath |
||
362 | * @param callable $handler |
||
363 | * |
||
364 | * @return Route |
||
365 | * |
||
366 | * @throws ReflectionException |
||
367 | */ |
||
368 | 22 | protected function createRoute(GroupInterface $group, string $method, string $uriPath, callable $handler): Route |
|
374 | |||
375 | /** |
||
376 | * @param array $parameters |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | 22 | protected function normalizeRouteParameters(array $parameters): array |
|
394 | |||
395 | /** |
||
396 | * @param array $parameters |
||
397 | * |
||
398 | * @return array |
||
399 | */ |
||
400 | 25 | protected function normalizeGroupParameters(array $parameters): array |
|
412 | |||
413 | /** |
||
414 | * @return null|string |
||
415 | */ |
||
416 | 18 | private function getParentUriPrefix(): ?string |
|
423 | |||
424 | /** |
||
425 | * @return array |
||
426 | */ |
||
427 | 17 | private function getParentMiddleware(): array |
|
434 | |||
435 | /** |
||
436 | * @return array |
||
437 | */ |
||
438 | 17 | private function getParentConfigurators(): array |
|
445 | } |
||
446 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.