| Total Complexity | 48 |
| Total Lines | 414 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Router 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.
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 Router, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Router |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Requested route. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $path; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * HTTP verb. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $verb; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Installed routes. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $routes = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Logger. |
||
| 46 | * |
||
| 47 | * @var LoggerInterface |
||
| 48 | */ |
||
| 49 | protected $logger; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * DI container. |
||
| 53 | * |
||
| 54 | * @var ContainerInterface |
||
| 55 | */ |
||
| 56 | protected $container; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Content type. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $content_type; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Init router. |
||
| 67 | * |
||
| 68 | * @param LoggerInterface $logger |
||
| 69 | * @param ContainerInterface $container |
||
| 70 | * @param array $request |
||
| 71 | */ |
||
| 72 | public function __construct(LoggerInterface $logger, ?array $request = null, ?ContainerInterface $container = null) |
||
| 73 | { |
||
| 74 | $this->logger = $logger; |
||
| 75 | $this->container = $container; |
||
| 76 | |||
| 77 | if (null === $request) { |
||
| 78 | $request = $_SERVER; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (isset($request['CONTENT_TYPE'])) { |
||
| 82 | $this->setContentType($request['CONTENT_TYPE']); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (isset($request['PATH_INFO'])) { |
||
| 86 | $this->setPath($request['PATH_INFO']); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (isset($request['REQUEST_METHOD'])) { |
||
| 90 | $this->setVerb($request['REQUEST_METHOD']); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add route to the beginning of the routing table. |
||
| 96 | * |
||
| 97 | * @param Route $route |
||
| 98 | * |
||
| 99 | * @return Router |
||
| 100 | */ |
||
| 101 | public function prependRoute(Route $route): self |
||
| 102 | { |
||
| 103 | array_unshift($this->routes, $route); |
||
| 104 | $route->setRouter($this); |
||
| 105 | |||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add route to the end of the routing table. |
||
| 111 | * |
||
| 112 | * @param Route $route |
||
| 113 | * |
||
| 114 | * @return Router |
||
| 115 | */ |
||
| 116 | public function appendRoute(Route $route): self |
||
| 117 | { |
||
| 118 | $this->routes[] = $route; |
||
| 119 | $route->setRouter($this); |
||
| 120 | |||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Clear routing table. |
||
| 126 | * |
||
| 127 | * @return Router |
||
| 128 | */ |
||
| 129 | public function clearRoutingTable(): self |
||
| 130 | { |
||
| 131 | $this->routes = []; |
||
| 132 | |||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get active routes. |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function getRoutes(): array |
||
| 142 | { |
||
| 143 | return $this->routes; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set Content type. |
||
| 148 | * |
||
| 149 | * @param string $type |
||
| 150 | * |
||
| 151 | * @return Router |
||
| 152 | */ |
||
| 153 | public function setContentType(string $type): self |
||
| 154 | { |
||
| 155 | $parts = explode(';', $type); |
||
| 156 | $this->content_type = $parts[0]; |
||
| 157 | |||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get content type. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getContentType(): string |
||
| 167 | { |
||
| 168 | return $this->type; |
||
|
|
|||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set HTTP verb. |
||
| 173 | * |
||
| 174 | * @param string $verb |
||
| 175 | * |
||
| 176 | * @return Router |
||
| 177 | */ |
||
| 178 | public function setVerb(string $verb): self |
||
| 179 | { |
||
| 180 | $this->verb = strtolower($verb); |
||
| 181 | |||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get http verb. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getVerb(): string |
||
| 191 | { |
||
| 192 | return $this->verb; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set routing path. |
||
| 197 | * |
||
| 198 | * @param string $path |
||
| 199 | * |
||
| 200 | * @return Router |
||
| 201 | */ |
||
| 202 | public function setPath(string $path): self |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get path. |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getPath(): string |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Execute router. |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function run(): bool |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Sends a exception response to the client. |
||
| 286 | * |
||
| 287 | * @param \Exception $exception |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function sendException(\Exception $exception): bool |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Build method name. |
||
| 323 | * |
||
| 324 | * @param string $name |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | protected function _buildMethodName(string $name): string |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Check if method got params and combine these with |
||
| 341 | * $_REQUEST. |
||
| 342 | * |
||
| 343 | * @param string $class |
||
| 344 | * @param string $method |
||
| 345 | * @param array $parsed_params |
||
| 346 | * |
||
| 347 | * @return callable |
||
| 348 | */ |
||
| 349 | protected function getParams(string $class, string $method, array $parsed_params): array |
||
| 435 | } |
||
| 436 | } |
||
| 437 | } |
||
| 438 |