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. 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 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 | /**  | 
            ||
| 32 | * HTTP verb  | 
            ||
| 33 | *  | 
            ||
| 34 | * @var string  | 
            ||
| 35 | */  | 
            ||
| 36 | protected $verb;  | 
            ||
| 37 | |||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * Installed routes  | 
            ||
| 41 | *  | 
            ||
| 42 | * @var array  | 
            ||
| 43 | */  | 
            ||
| 44 | protected $routes = [];  | 
            ||
| 45 | |||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * Logger  | 
            ||
| 49 | *  | 
            ||
| 50 | * @var Logger  | 
            ||
| 51 | */  | 
            ||
| 52 | protected $logger;  | 
            ||
| 53 | |||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * DI container  | 
            ||
| 57 | *  | 
            ||
| 58 | * @var ContainerInterface  | 
            ||
| 59 | */  | 
            ||
| 60 | protected $container;  | 
            ||
| 61 | |||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Init router  | 
            ||
| 65 | *  | 
            ||
| 66 | * @param LoggerInterface $logger  | 
            ||
| 67 | * @param ContainerInterface $container  | 
            ||
| 68 | * @param array $request  | 
            ||
| 69 | * @return void  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 70 | */  | 
            ||
| 71 | public function __construct(LoggerInterface $logger, ?array $request=null, ?ContainerInterface $container=null)  | 
            ||
| 88 | |||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * Add route to the beginning of the routing table  | 
            ||
| 92 | *  | 
            ||
| 93 | * @param Route $route  | 
            ||
| 94 | * @return Router  | 
            ||
| 95 | */  | 
            ||
| 96 | public function prependRoute(Route $route): Router  | 
            ||
| 102 | |||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * Add route to the end of the routing table  | 
            ||
| 106 | *  | 
            ||
| 107 | * @param Route $route  | 
            ||
| 108 | * @return Router  | 
            ||
| 109 | */  | 
            ||
| 110 | public function appendRoute(Route $route): Router  | 
            ||
| 116 | |||
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * Clear routing table  | 
            ||
| 120 | *  | 
            ||
| 121 | * @return Router  | 
            ||
| 122 | */  | 
            ||
| 123 | public function clearRoutingTable(): Router  | 
            ||
| 128 | |||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Get active routes  | 
            ||
| 132 | *  | 
            ||
| 133 | * @return array  | 
            ||
| 134 | */  | 
            ||
| 135 | public function getRoutes(): array  | 
            ||
| 139 | |||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * Set HTTP verb  | 
            ||
| 143 | *  | 
            ||
| 144 | * @param string $verb  | 
            ||
| 145 | * @return Router  | 
            ||
| 146 | */  | 
            ||
| 147 | public function setVerb(string $verb): Router  | 
            ||
| 152 | |||
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * Get http verb  | 
            ||
| 156 | *  | 
            ||
| 157 | * @return string  | 
            ||
| 158 | */  | 
            ||
| 159 | public function getVerb(): string  | 
            ||
| 163 | |||
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * Set routing path  | 
            ||
| 167 | *  | 
            ||
| 168 | * @param string $path  | 
            ||
| 169 | * @return Router  | 
            ||
| 170 | */  | 
            ||
| 171 | public function setPath(string $path): Router  | 
            ||
| 177 | |||
| 178 | |||
| 179 | /**  | 
            ||
| 180 | * Get path  | 
            ||
| 181 | *  | 
            ||
| 182 | * @return string  | 
            ||
| 183 | */  | 
            ||
| 184 | public function getPath(): string  | 
            ||
| 188 | |||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Build method name  | 
            ||
| 192 | *  | 
            ||
| 193 | * @param string $name  | 
            ||
| 194 | * @return string  | 
            ||
| 195 | */  | 
            ||
| 196 | protected function _buildMethodName(string $name): string  | 
            ||
| 206 | |||
| 207 | |||
| 208 | /**  | 
            ||
| 209 | * Execute router  | 
            ||
| 210 | *  | 
            ||
| 211 | * @return bool  | 
            ||
| 212 | */  | 
            ||
| 213 | public function run(): bool  | 
            ||
| 270 | |||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Sends a exception response to the client  | 
            ||
| 274 | *  | 
            ||
| 275 | * @param \Exception $exception  | 
            ||
| 276 | * @return bool  | 
            ||
| 277 | */  | 
            ||
| 278 | public function sendException(\Exception $exception): bool  | 
            ||
| 307 | |||
| 308 | |||
| 309 | /**  | 
            ||
| 310 | * Check if method got params and combine these with  | 
            ||
| 311 | * $_REQUEST  | 
            ||
| 312 | *  | 
            ||
| 313 | * @param string $class  | 
            ||
| 314 | * @param string $method  | 
            ||
| 315 | * @param array $parsed_params  | 
            ||
| 316 | * @return callable  | 
            ||
| 317 | */  | 
            ||
| 318 | protected function getParams(string $class, string $method, array $parsed_params): array  | 
            ||
| 382 | }  | 
            ||
| 383 | 
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.