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 declare(strict_types=1); |
||
39 | abstract class BaseGroup implements GroupInterface |
||
40 | { |
||
41 | use CallableTrait, UriTrait, HasRequestFactoryTrait, CheckCallableTrait; |
||
42 | |||
43 | use HasMiddlewareTrait { |
||
44 | addMiddleware as private addMiddlewareImpl; |
||
45 | } |
||
46 | |||
47 | use HasConfiguratorsTrait { |
||
48 | addConfigurators as private addConfiguratorsImpl; |
||
49 | } |
||
50 | |||
51 | /** Default value if routes should use request factory from its group */ |
||
52 | const USE_FACTORY_FROM_GROUP_DEFAULT = true; |
||
53 | |||
54 | /** |
||
55 | * @var null|GroupInterface |
||
56 | */ |
||
57 | private $parent; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | private $uriPrefix = ''; |
||
63 | |||
64 | /** |
||
65 | * @var string|null |
||
66 | */ |
||
67 | private $name = null; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | private $items = []; |
||
73 | |||
74 | /** |
||
75 | * @var bool |
||
76 | */ |
||
77 | private $trailSlashes = false; |
||
78 | |||
79 | /** |
||
80 | * @return self |
||
|
|||
81 | */ |
||
82 | abstract protected function createGroup(): self; |
||
83 | |||
84 | /** |
||
85 | * @param GroupInterface $parent |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | 17 | public function setParentGroup(GroupInterface $parent) |
|
95 | |||
96 | /** |
||
97 | * @param string $uriPrefix |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | 17 | public function setUriPrefix(string $uriPrefix): self |
|
107 | |||
108 | /** |
||
109 | * @param string $name |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | 1 | public function setName(string $name): self |
|
119 | |||
120 | /** |
||
121 | * @return self |
||
122 | */ |
||
123 | 17 | public function clearName(): self |
|
129 | |||
130 | /** |
||
131 | * @param bool $trailSlashes |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | 17 | public function setHasTrailSlash(bool $trailSlashes): self |
|
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 18 | public function parentGroup(): ?GroupInterface |
|
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | */ |
||
153 | 18 | public function getUriPrefix(): string |
|
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | 8 | public function getName(): ?string |
|
170 | |||
171 | /** |
||
172 | * @inheritdoc |
||
173 | */ |
||
174 | 17 | public function getMiddleware(): array |
|
180 | |||
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | 1 | public function addMiddleware(array $middleware): GroupInterface |
|
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 17 | public function getContainerConfigurators(): array |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 1 | public function addContainerConfigurators(array $configurators): GroupInterface |
|
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | 17 | public function getRequestFactory(): ?callable |
|
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | 18 | public function getRoutes(): iterable |
|
240 | |||
241 | /** |
||
242 | * @inheritdoc |
||
243 | */ |
||
244 | 17 | public function group(string $prefix, Closure $closure, array $parameters = []): GroupInterface |
|
259 | |||
260 | /** |
||
261 | * @inheritdoc |
||
262 | */ |
||
263 | 17 | public function addGroup(Closure $closure, GroupInterface $group): GroupInterface |
|
271 | |||
272 | /** |
||
273 | * @inheritdoc |
||
274 | */ |
||
275 | 18 | public function addRoute(RouteInterface $route): GroupInterface |
|
281 | |||
282 | /** |
||
283 | * @inheritdoc |
||
284 | * |
||
285 | * @throws ReflectionException |
||
286 | */ |
||
287 | 22 | public function method(string $method, string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | * |
||
307 | * @throws ReflectionException |
||
308 | */ |
||
309 | 22 | public function get(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
313 | |||
314 | /** |
||
315 | * @inheritdoc |
||
316 | * |
||
317 | * @throws ReflectionException |
||
318 | */ |
||
319 | 16 | public function post(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
323 | |||
324 | /** |
||
325 | * @inheritdoc |
||
326 | * |
||
327 | * @throws ReflectionException |
||
328 | */ |
||
329 | 1 | public function put(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
333 | |||
334 | /** |
||
335 | * @inheritdoc |
||
336 | * |
||
337 | * @throws ReflectionException |
||
338 | */ |
||
339 | 2 | public function patch(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
343 | |||
344 | /** |
||
345 | * @inheritdoc |
||
346 | * |
||
347 | * @throws ReflectionException |
||
348 | */ |
||
349 | 10 | public function delete(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | 22 | public function hasTrailSlash(): bool |
|
361 | |||
362 | /** |
||
363 | * @param GroupInterface $group |
||
364 | * @param string $method |
||
365 | * @param string $uriPath |
||
366 | * @param callable $handler |
||
367 | * |
||
368 | * @return Route |
||
369 | * |
||
370 | * @throws ReflectionException |
||
371 | */ |
||
372 | 22 | protected function createRoute(GroupInterface $group, string $method, string $uriPath, callable $handler): Route |
|
378 | |||
379 | /** |
||
380 | * @param array $parameters |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | 22 | protected function normalizeRouteParameters(array $parameters): array |
|
398 | |||
399 | /** |
||
400 | * @param array $parameters |
||
401 | * |
||
402 | * @return array |
||
403 | */ |
||
404 | 25 | protected function normalizeGroupParameters(array $parameters): array |
|
416 | |||
417 | /** |
||
418 | * @return null|string |
||
419 | */ |
||
420 | 18 | private function getParentUriPrefix(): ?string |
|
427 | |||
428 | /** |
||
429 | * @return array |
||
430 | */ |
||
431 | 17 | private function getParentMiddleware(): array |
|
438 | |||
439 | /** |
||
440 | * @return array |
||
441 | */ |
||
442 | 17 | private function getParentConfigurators(): array |
|
449 | } |
||
450 |
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.