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 |
||
| 24 | abstract class AbstractRoute implements RouteInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Default segment pattern, this patter can be applied to controller names, actions and etc. |
||
| 28 | */ |
||
| 29 | const DEFAULT_SEGMENT = '[^\.\/]+'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $name = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Path prefix (base path). |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $prefix = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Default set of values to fill route matches and target pattern (if specified as pattern). |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $defaults = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * If true route will be matched with URI host in addition to path. BasePath will be ignored. |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private $withHost = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Route matches, populated after match() method executed. Internal. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $matches = []; |
||
| 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 pattern includes simplified regular expressing later compiled to real regexp. |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $pattern = ''; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $middlewares = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Route endpoint container context. |
||
| 86 | * |
||
| 87 | * @invisible |
||
| 88 | * @var ContainerInterface|null |
||
| 89 | */ |
||
| 90 | protected $container = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $name |
||
| 94 | * @param array $defaults |
||
| 95 | */ |
||
| 96 | public function __construct($name, array $defaults) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function withContainer(ContainerInterface $container) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function withName($name) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function getName() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function withPrefix($prefix) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function getPrefix() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * If true (default) route will be matched against path + URI host. Returns new route instance. |
||
| 153 | * |
||
| 154 | * @param bool $withHost |
||
| 155 | * @return self |
||
| 156 | */ |
||
| 157 | public function withHost($withHost = true) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function withDefaults(array $defaults) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get default route values. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getDefaults() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Associated middleware with route. New instance of route will be returned. |
||
| 188 | * |
||
| 189 | * Example: |
||
| 190 | * $route->withMiddleware(new CacheMiddleware(100)); |
||
| 191 | * $route->withMiddleware(ProxyMiddleware::class); |
||
| 192 | * $route->withMiddleware([ProxyMiddleware::class, OtherMiddleware::class]); |
||
| 193 | * |
||
| 194 | * @param callable|MiddlewareInterface|array $middleware |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | public function withMiddleware($middleware) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function match(Request $request) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function perform(Request $request, Response $response) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function uri($parameters = []) |
||
| 251 | { |
||
| 252 | if (empty($this->compiled)) { |
||
| 253 | $this->compile(); |
||
| 254 | } |
||
| 255 | |||
| 256 | $parameters = array_merge( |
||
| 257 | $this->compiled['options'], |
||
| 258 | $this->defaults, |
||
| 259 | $this->matches, |
||
| 260 | $this->fetchSegments($parameters, $query) |
||
| 261 | ); |
||
| 262 | |||
| 263 | //Uri without empty blocks (pretty stupid implementation) |
||
| 264 | $path = strtr( |
||
| 265 | \Spiral\interpolate($this->compiled['template'], $parameters, '<', '>'), |
||
| 266 | ['[]' => '', '[/]' => '', '[' => '', ']' => '', '//' => '/'] |
||
| 267 | ); |
||
| 268 | |||
| 269 | $uri = new Uri(($this->withHost ? '' : $this->prefix) . rtrim($path, '/')); |
||
| 270 | |||
| 271 | return empty($query) ? $uri : $uri->withQuery(http_build_query($query)); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Fetch uri segments and query parameters. |
||
| 276 | * |
||
| 277 | * @param \Traversable|array $parameters |
||
| 278 | * @param array $query Query parameters. |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function fetchSegments($parameters, &$query) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Route matches. |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | protected function getMatches() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $name |
||
| 320 | * @param mixed $default |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | public function getMatch($name, $default = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Create callable route endpoint. |
||
| 334 | * |
||
| 335 | * @return callable |
||
| 336 | */ |
||
| 337 | abstract protected function createEndpoint(); |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | protected function container() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Compile router pattern into valid regexp. |
||
| 353 | */ |
||
| 354 | private function compile() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param Request $request |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | private function getSubject(Request $request) |
||
| 399 | } |
||
| 400 |