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; |
||
| 32 | abstract class BaseGroup implements GroupInterface |
||
| 33 | { |
||
| 34 | use CallableTrait, UriTrait, HasRequestFactoryTrait, CheckCallableTrait; |
||
| 35 | |||
| 36 | use HasMiddlewareTrait { |
||
| 37 | addMiddleware as private addMiddlewareImpl; |
||
| 38 | } |
||
| 39 | |||
| 40 | use HasConfiguratorsTrait { |
||
| 41 | addConfigurators as private addConfiguratorsImpl; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** Default value if routes should use request factory from its group */ |
||
| 45 | const USE_FACTORY_FROM_GROUP_DEFAULT = true; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var null|GroupInterface |
||
| 49 | */ |
||
| 50 | private $parent; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $uriPrefix = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string|null |
||
| 59 | */ |
||
| 60 | private $name = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $items = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $trailSlashes = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return self |
||
|
|
|||
| 74 | */ |
||
| 75 | 17 | abstract protected function createGroup(): self; |
|
| 76 | |||
| 77 | 17 | /** |
|
| 78 | * @param GroupInterface $parent |
||
| 79 | 17 | * |
|
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function setParentGroup(GroupInterface $parent) |
||
| 88 | |||
| 89 | 17 | /** |
|
| 90 | * @param string $uriPrefix |
||
| 91 | 17 | * |
|
| 92 | * @return self |
||
| 93 | */ |
||
| 94 | public function setUriPrefix(string $uriPrefix): self |
||
| 100 | |||
| 101 | 17 | /** |
|
| 102 | * @param string $name |
||
| 103 | 17 | * |
|
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | public function setName(string $name): self |
||
| 112 | |||
| 113 | 17 | /** |
|
| 114 | * @return self |
||
| 115 | 17 | */ |
|
| 116 | public function clearName(): self |
||
| 122 | |||
| 123 | 18 | /** |
|
| 124 | * @param bool $trailSlashes |
||
| 125 | * |
||
| 126 | * @return self |
||
| 127 | */ |
||
| 128 | public function setHasTrailSlash(bool $trailSlashes): self |
||
| 134 | |||
| 135 | /** |
||
| 136 | 18 | * @inheritdoc |
|
| 137 | */ |
||
| 138 | public function parentGroup(): ?GroupInterface |
||
| 142 | 8 | ||
| 143 | /** |
||
| 144 | 8 | * @inheritdoc |
|
| 145 | */ |
||
| 146 | public function getUriPrefix(): string |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | public function getName(): ?string |
||
| 163 | |||
| 164 | 17 | /** |
|
| 165 | * @inheritdoc |
||
| 166 | */ |
||
| 167 | public function getMiddleware(): array |
||
| 173 | 9 | ||
| 174 | /** |
||
| 175 | * @inheritdoc |
||
| 176 | 15 | */ |
|
| 177 | 15 | public function addMiddleware(array $middleware): GroupInterface |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @inheritdoc |
||
| 184 | */ |
||
| 185 | 18 | public function getContainerConfigurators(): array |
|
| 191 | 18 | ||
| 192 | /** |
||
| 193 | * @inheritdoc |
||
| 194 | */ |
||
| 195 | 17 | public function addContainerConfigurators(array $configurators): GroupInterface |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function getRequestFactory(): ?callable |
||
| 214 | |||
| 215 | 17 | /** |
|
| 216 | * @inheritdoc |
||
| 217 | 17 | */ |
|
| 218 | public function getRoutes(): iterable |
||
| 233 | |||
| 234 | /** |
||
| 235 | 18 | * @inheritdoc |
|
| 236 | */ |
||
| 237 | 18 | public function group(string $prefix, Closure $closure, array $parameters = []): GroupInterface |
|
| 252 | 22 | ||
| 253 | 21 | /** |
|
| 254 | 21 | * @inheritdoc |
|
| 255 | 20 | */ |
|
| 256 | 19 | public function addGroup(Closure $closure, GroupInterface $group): GroupInterface |
|
| 264 | |||
| 265 | 22 | /** |
|
| 266 | * @inheritdoc |
||
| 267 | 22 | */ |
|
| 268 | public function addRoute(RouteInterface $route): GroupInterface |
||
| 274 | |||
| 275 | 16 | /** |
|
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | public function method(string $method, string $uriPath, callable $handler, array $parameters = []): GroupInterface |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @inheritdoc |
||
| 297 | 10 | */ |
|
| 298 | public function get(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @inheritdoc |
||
| 305 | 22 | */ |
|
| 306 | public function post(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @inheritdoc |
||
| 313 | */ |
||
| 314 | public function put(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
||
| 318 | 22 | ||
| 319 | /** |
||
| 320 | 22 | * @inheritdoc |
|
| 321 | */ |
||
| 322 | 21 | public function patch(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @inheritdoc |
||
| 329 | */ |
||
| 330 | 22 | public function delete(string $uriPath, callable $handler, array $parameters = []): GroupInterface |
|
| 334 | 22 | ||
| 335 | 22 | /** |
|
| 336 | 22 | * @inheritdoc |
|
| 337 | 22 | */ |
|
| 338 | public function hasTrailSlash(): bool |
||
| 342 | 22 | ||
| 343 | /** |
||
| 344 | * @param GroupInterface $group |
||
| 345 | 22 | * @param string $method |
|
| 346 | 22 | * @param string $uriPath |
|
| 347 | 22 | * @param callable $handler |
|
| 348 | 22 | * |
|
| 349 | 22 | * @return Route |
|
| 350 | */ |
||
| 351 | protected function createRoute(GroupInterface $group, string $method, string $uriPath, callable $handler): Route |
||
| 357 | |||
| 358 | 25 | /** |
|
| 359 | * @param array $parameters |
||
| 360 | * |
||
| 361 | 25 | * @return array |
|
| 362 | 25 | */ |
|
| 363 | 25 | protected function normalizeRouteParameters(array $parameters): array |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param array $parameters |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | 18 | protected function normalizeGroupParameters(array $parameters): array |
|
| 395 | |||
| 396 | 17 | /** |
|
| 397 | 17 | * @return null|string |
|
| 398 | */ |
||
| 399 | 17 | private function getParentUriPrefix(): ?string |
|
| 406 | |||
| 407 | 17 | /** |
|
| 408 | 17 | * @return array |
|
| 409 | */ |
||
| 410 | 17 | private function getParentMiddleware(): array |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | private function getParentConfigurators(): array |
||
| 428 | } |
||
| 429 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.