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) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function perform(Request $request, Response $response) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function uri($parameters = []) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Route matches. |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function getMatches() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $name |
||
| 283 | * @param mixed $default |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | public function getMatch($name, $default = null) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $matches |
||
| 297 | * @return AbstractRoute |
||
| 298 | */ |
||
| 299 | protected function withMatches(array $matches) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Fetch uri segments and query parameters. |
||
| 309 | * |
||
| 310 | * @param \Traversable|array $parameters |
||
| 311 | * @param array $query Query parameters. |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | protected function fetchSegments($parameters, &$query) |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * Create callable route endpoint. |
||
| 344 | * |
||
| 345 | * @return callable |
||
| 346 | */ |
||
| 347 | abstract protected function createEndpoint(); |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | protected function container() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Compile router pattern into valid regexp. |
||
| 363 | */ |
||
| 364 | private function compile() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param Request $request |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | private function getSubject(Request $request) |
||
| 409 | } |
||
| 410 |