Complex classes like AbstractRoute 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 AbstractRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class AbstractRoute implements RouteInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Default segment pattern, this patter can be applied to controller names, actions and etc. |
||
| 30 | */ |
||
| 31 | const DEFAULT_SEGMENT = '[^\/]+'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $name = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Path prefix (base path). |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $prefix = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Default set of values to fill route matches and target pattern (if specified as pattern). |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $defaults = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * If true route will be matched with URI host in addition to path. BasePath will be ignored. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | private $withHost = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string[] |
||
| 61 | */ |
||
| 62 | private $methods = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Compiled route options, pattern and etc. Internal data. |
||
| 66 | * |
||
| 67 | * @invisible |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private $compiled = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Route matches, populated after match() method executed. Internal. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $matches = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Route pattern includes simplified regular expressing later compiled to real regexp. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $pattern = ''; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $middlewares = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Route endpoint container context. |
||
| 93 | * |
||
| 94 | * @invisible |
||
| 95 | * @var ContainerInterface|null |
||
| 96 | */ |
||
| 97 | protected $container = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $name |
||
| 101 | * @param array $defaults |
||
| 102 | */ |
||
| 103 | public function __construct(string $name, array $defaults) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | * |
||
| 112 | * @return $this|AbstractRoute |
||
| 113 | */ |
||
| 114 | public function withContainer(ContainerInterface $container): RouteInterface |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | * |
||
| 125 | * @return $this|AbstractRoute |
||
| 126 | */ |
||
| 127 | public function withName(string $name): RouteInterface |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function getName(): string |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | * |
||
| 146 | * @return $this|AbstractRoute |
||
| 147 | */ |
||
| 148 | public function withPrefix(string $prefix): RouteInterface |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function getPrefix(): string |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | * |
||
| 167 | * @return $this|AbstractRoute |
||
| 168 | */ |
||
| 169 | public function withMethod(string $method): AbstractRoute |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public function getMethods(): array |
||
| 184 | |||
| 185 | /** |
||
| 186 | * If true (default) route will be matched against path + URI host. Returns new route instance. |
||
| 187 | * |
||
| 188 | * @param bool $withHost |
||
| 189 | * |
||
| 190 | * @return $this|AbstractRoute |
||
| 191 | */ |
||
| 192 | public function withHost(bool $withHost = true): AbstractRoute |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | * |
||
| 203 | * @return $this|AbstractRoute |
||
| 204 | */ |
||
| 205 | public function withDefaults(array $defaults): RouteInterface |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get default route values. |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function getDefaults(): array |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Associated middleware with route. New instance of route will be returned. |
||
| 225 | * |
||
| 226 | * Example: |
||
| 227 | * $route->withMiddleware(new CacheMiddleware(100)); |
||
| 228 | * $route->withMiddleware(ProxyMiddleware::class); |
||
| 229 | * $route->withMiddleware([ProxyMiddleware::class, OtherMiddleware::class]); |
||
| 230 | * |
||
| 231 | * @param callable|MiddlewareInterface|array $middleware |
||
| 232 | * |
||
| 233 | * @return $this|AbstractRoute |
||
| 234 | */ |
||
| 235 | public function withMiddleware($middleware): AbstractRoute |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function match(Request $request) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public function __invoke(Request $request, Response $response): Response |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function uri($parameters = []): UriInterface |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Route matches. |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getMatches(): array |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $name |
||
| 325 | * @param mixed $default |
||
| 326 | * |
||
| 327 | * @return mixed |
||
| 328 | */ |
||
| 329 | public function getMatch(string $name, $default = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param array $matches |
||
| 340 | * |
||
| 341 | * @return self|AbstractRoute |
||
| 342 | */ |
||
| 343 | protected function withMatches(array $matches): AbstractRoute |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Fetch uri segments and query parameters. |
||
| 353 | * |
||
| 354 | * @param \Traversable|array $parameters |
||
| 355 | * @param array|null $query Query parameters. |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | protected function fetchSegments($parameters, &$query): array |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Create callable route endpoint. |
||
| 390 | * |
||
| 391 | * @return callable |
||
| 392 | */ |
||
| 393 | abstract protected function createEndpoint(); |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | protected function iocContainer(): ContainerInterface |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Compile router pattern into valid regexp. |
||
| 409 | */ |
||
| 410 | private function compile() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Part of uri path which is being matched. |
||
| 437 | * |
||
| 438 | * @param Request $request |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | private function getSubject(Request $request): string |
||
| 458 | } |
||
| 459 |