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 | * Compiled route options, pattern and etc. Internal data. |
||
| 61 | * |
||
| 62 | * @invisible |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $compiled = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Route matches, populated after match() method executed. Internal. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $matches = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Route pattern includes simplified regular expressing later compiled to real regexp. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $pattern = ''; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $middlewares = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Route endpoint container context. |
||
| 88 | * |
||
| 89 | * @invisible |
||
| 90 | * @var ContainerInterface|null |
||
| 91 | */ |
||
| 92 | protected $container = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $name |
||
| 96 | * @param array $defaults |
||
| 97 | */ |
||
| 98 | public function __construct(string $name, array $defaults) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | * |
||
| 107 | * @return $this|AbstractRoute |
||
| 108 | */ |
||
| 109 | public function withContainer(ContainerInterface $container): RouteInterface |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | * |
||
| 120 | * @return $this|AbstractRoute |
||
| 121 | */ |
||
| 122 | public function withName(string $name): RouteInterface |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function getName(): string |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | * |
||
| 141 | * @return $this|AbstractRoute |
||
| 142 | */ |
||
| 143 | public function withPrefix(string $prefix): RouteInterface |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function getPrefix(): string |
||
| 158 | |||
| 159 | /** |
||
| 160 | * If true (default) route will be matched against path + URI host. Returns new route instance. |
||
| 161 | * |
||
| 162 | * @param bool $withHost |
||
| 163 | * |
||
| 164 | * @return $this|AbstractRoute |
||
| 165 | */ |
||
| 166 | public function withHost(bool $withHost = true): AbstractRoute |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | * |
||
| 177 | * @return $this|AbstractRoute |
||
| 178 | */ |
||
| 179 | public function withDefaults(array $defaults): RouteInterface |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get default route values. |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getDefaults(): array |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Associated middleware with route. New instance of route will be returned. |
||
| 199 | * |
||
| 200 | * Example: |
||
| 201 | * $route->withMiddleware(new CacheMiddleware(100)); |
||
| 202 | * $route->withMiddleware(ProxyMiddleware::class); |
||
| 203 | * $route->withMiddleware([ProxyMiddleware::class, OtherMiddleware::class]); |
||
| 204 | * |
||
| 205 | * @param callable|MiddlewareInterface|array $middleware |
||
| 206 | * |
||
| 207 | * @return $this|AbstractRoute |
||
| 208 | */ |
||
| 209 | public function withMiddleware($middleware): AbstractRoute |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function match(Request $request) |
||
| 225 | { |
||
| 226 | if (empty($this->compiled)) { |
||
| 227 | $this->compile(); |
||
| 228 | } |
||
| 229 | |||
| 230 | if (preg_match($this->compiled['pattern'], $this->getSubject($request), $matches)) { |
||
| 231 | //To get only named matches |
||
| 232 | $matches = array_intersect_key($matches, $this->compiled['options']); |
||
| 233 | $matches = array_merge($this->compiled['options'], $this->defaults, $matches); |
||
| 234 | |||
| 235 | return $this->withMatches($matches); |
||
| 236 | } |
||
| 237 | |||
| 238 | return null; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function __invoke(Request $request, Response $response): Response |
||
| 245 | { |
||
| 246 | if (empty($this->container)) { |
||
| 247 | throw new RouteException("Unable to perform route endpoint without given container"); |
||
| 248 | } |
||
| 249 | |||
| 250 | $pipeline = new MiddlewarePipeline($this->middlewares, $this->container); |
||
| 251 | |||
| 252 | return $pipeline->target($this->createEndpoint())->run($request, $response); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | */ |
||
| 258 | public function uri($parameters = []): UriInterface |
||
| 259 | { |
||
| 260 | if (empty($this->compiled)) { |
||
| 261 | $this->compile(); |
||
| 262 | } |
||
| 263 | |||
| 264 | $parameters = array_merge( |
||
| 265 | $this->compiled['options'], |
||
| 266 | $this->defaults, |
||
| 267 | $this->matches, |
||
| 268 | $this->fetchSegments($parameters, $query) |
||
| 269 | ); |
||
| 270 | |||
| 271 | //Uri without empty blocks (pretty stupid implementation) |
||
| 272 | $path = strtr( |
||
| 273 | \Spiral\interpolate($this->compiled['template'], $parameters, '<', '>'), |
||
| 274 | ['[]' => '', '[/]' => '', '[' => '', ']' => '', '://' => '://', '//' => '/'] |
||
| 275 | ); |
||
| 276 | |||
| 277 | //Uri with added prefix |
||
| 278 | $uri = new Uri(($this->withHost ? '' : $this->prefix) . trim($path, '/')); |
||
| 279 | |||
| 280 | return empty($query) ? $uri : $uri->withQuery(http_build_query($query)); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Route matches. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getMatches(): array |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $name |
||
| 295 | * @param mixed $default |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | public function getMatch(string $name, $default = null) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param array $matches |
||
| 310 | * |
||
| 311 | * @return self|AbstractRoute |
||
| 312 | */ |
||
| 313 | protected function withMatches(array $matches): AbstractRoute |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Fetch uri segments and query parameters. |
||
| 323 | * |
||
| 324 | * @param \Traversable|array $parameters |
||
| 325 | * @param array|null $query Query parameters. |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | protected function fetchSegments($parameters, &$query): array |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Create callable route endpoint. |
||
| 360 | * |
||
| 361 | * @return callable |
||
| 362 | */ |
||
| 363 | abstract protected function createEndpoint(); |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritdoc} |
||
| 367 | */ |
||
| 368 | protected function iocContainer(): ContainerInterface |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Compile router pattern into valid regexp. |
||
| 379 | */ |
||
| 380 | private function compile() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Part of uri path which is being matched. |
||
| 407 | * |
||
| 408 | * @param Request $request |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | private function getSubject(Request $request): string |
||
| 428 | } |
||
| 429 |