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  | 
            ||
| 53 | final class Router  | 
            ||
| 54 | { | 
            ||
| 55 | /**  | 
            ||
| 56 | * FastRoute, null if usingCache is set  | 
            ||
| 57 | * @var RouteCollector  | 
            ||
| 58 | */  | 
            ||
| 59 | private $routeCollector = null;  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * FastRoute cache file path.  | 
            ||
| 63 | * @var string  | 
            ||
| 64 | */  | 
            ||
| 65 | private $cacheFilePath;  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * List of middlewares called using the middleware() method.  | 
            ||
| 69 | * @var array  | 
            ||
| 70 | */  | 
            ||
| 71 | private $currentMiddlewares = [];  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * List of group prefixes called using the group() method.  | 
            ||
| 75 | * @var string  | 
            ||
| 76 | */  | 
            ||
| 77 | private $currentGroupPrefix;  | 
            ||
| 78 | |||
| 79 | /**  | 
            ||
| 80 | * Initialize the route configurations.  | 
            ||
| 81 | */  | 
            ||
| 82 | public function __construct()  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * Encapsulate all the routes that are added from $func(Router) with this middleware.  | 
            ||
| 106 | *  | 
            ||
| 107 | * If the return value of the middleware is false, throws a RouteMiddlewareFailedException.  | 
            ||
| 108 | *  | 
            ||
| 109 | * @param string $library_name The library to call through \Cervo\Core::getLibrary( string )  | 
            ||
| 110 | * @param string $method_name The method to call through the library  | 
            ||
| 111 | * @param callable $func  | 
            ||
| 112 | */  | 
            ||
| 113 | public function middleware(string $library_name, string $method_name, callable $func) : void  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Adds a prefix in front of all the encapsulated routes.  | 
            ||
| 128 | *  | 
            ||
| 129 | * @param string $prefix The prefix of the group.  | 
            ||
| 130 | * @param callable $func  | 
            ||
| 131 | */  | 
            ||
| 132 | public function group(string $prefix, callable $func) : void  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * Dispatch the request to the router.  | 
            ||
| 144 | *  | 
            ||
| 145 | * @return Route  | 
            ||
| 146 | * @throws MethodNotAllowedException if the request method is not supported, but others are for this route.  | 
            ||
| 147 | * @throws RouteNotFoundException if the requested route did not match any routes.  | 
            ||
| 148 | */  | 
            ||
| 149 | public function dispatch() : Route  | 
            ||
| 179 | |||
| 180 | /**  | 
            ||
| 181 | * Add a new route.  | 
            ||
| 182 | *  | 
            ||
| 183 | * @param string|string[] $http_method The HTTP method, example: GET, POST, PATCH, PUT, DELETE, CLI, etc. Can be an array of values.  | 
            ||
| 184 | * @param string $route The route  | 
            ||
| 185 | * @param string $method_path The Method Path  | 
            ||
| 186 | * @param array $parameters The parameters to pass  | 
            ||
| 187 | */  | 
            ||
| 188 | public function addRoute($http_method, string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Add a new route with GET as HTTP method.  | 
            ||
| 205 | *  | 
            ||
| 206 | * @param string $route The route  | 
            ||
| 207 | * @param string $method_path The Method Path  | 
            ||
| 208 | * @param array $parameters The parameters to pass  | 
            ||
| 209 | */  | 
            ||
| 210 | public function get(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 214 | |||
| 215 | /**  | 
            ||
| 216 | * Add a new route with POST as HTTP method.  | 
            ||
| 217 | *  | 
            ||
| 218 | * @param string $route The route  | 
            ||
| 219 | * @param string $method_path The Method Path  | 
            ||
| 220 | * @param array $parameters The parameters to pass  | 
            ||
| 221 | */  | 
            ||
| 222 | public function post(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 226 | |||
| 227 | /**  | 
            ||
| 228 | * Add a new route with PUT as HTTP method.  | 
            ||
| 229 | *  | 
            ||
| 230 | * @param string $route The route  | 
            ||
| 231 | * @param string $method_path The Method Path  | 
            ||
| 232 | * @param array $parameters The parameters to pass  | 
            ||
| 233 | */  | 
            ||
| 234 | public function put(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * Add a new route with PATCH as HTTP method.  | 
            ||
| 241 | *  | 
            ||
| 242 | * @param string $route The route  | 
            ||
| 243 | * @param string $method_path The Method Path  | 
            ||
| 244 | * @param array $parameters The parameters to pass  | 
            ||
| 245 | */  | 
            ||
| 246 | public function patch(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * Add a new route with DELETE as HTTP method.  | 
            ||
| 253 | *  | 
            ||
| 254 | * @param string $route The route  | 
            ||
| 255 | * @param string $method_path The Method Path  | 
            ||
| 256 | * @param array $parameters The parameters to pass  | 
            ||
| 257 | */  | 
            ||
| 258 | public function delete(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 262 | |||
| 263 | /**  | 
            ||
| 264 | * Add a new route with HEAD as HTTP method.  | 
            ||
| 265 | *  | 
            ||
| 266 | * @param string $route The route  | 
            ||
| 267 | * @param string $method_path The Method Path  | 
            ||
| 268 | * @param array $parameters The parameters to pass  | 
            ||
| 269 | */  | 
            ||
| 270 | public function head(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 274 | |||
| 275 | /**  | 
            ||
| 276 | * Add a new route with CLI as method.  | 
            ||
| 277 | *  | 
            ||
| 278 | * @param string $route The route  | 
            ||
| 279 | * @param string $method_path The Method Path  | 
            ||
| 280 | * @param array $parameters The parameters to pass  | 
            ||
| 281 | */  | 
            ||
| 282 | public function cli(string $route, string $method_path, array $parameters = []) : void  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Force the generation of the cache file. Delete the current cache file if it exists.  | 
            ||
| 289 | */  | 
            ||
| 290 | public function forceGenerateCache() : bool  | 
            ||
| 302 | |||
| 303 | /**  | 
            ||
| 304 | * @return Dispatcher\GroupCountBased  | 
            ||
| 305 | * @throws InvalidRouterCacheException if the router cache exists and is invalid.  | 
            ||
| 306 | */  | 
            ||
| 307 | private function getDispatcher() : Dispatcher\GroupCountBased  | 
            ||
| 331 | |||
| 332 | /**  | 
            ||
| 333 | * @param array $dispatchData  | 
            ||
| 334 | *  | 
            ||
| 335 | * @return bool  | 
            ||
| 336 | */  | 
            ||
| 337 | private function generateCache(array $dispatchData) : bool  | 
            ||
| 353 | |||
| 354 | /**  | 
            ||
| 355 | * Returns a parsable URI  | 
            ||
| 356 | *  | 
            ||
| 357 | * @return string  | 
            ||
| 358 | */  | 
            ||
| 359 | private function detectUri() : string  | 
            ||
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * Return the base URI for a request  | 
            ||
| 383 | *  | 
            ||
| 384 | * @return string  | 
            ||
| 385 | */  | 
            ||
| 386 | private function getBaseUri() : string  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * Throws an exception or return.  | 
            ||
| 405 | *  | 
            ||
| 406 | * @param array $middlewares  | 
            ||
| 407 | * @param array $parameters  | 
            ||
| 408 | * @param array $arguments  | 
            ||
| 409 | *  | 
            ||
| 410 | * @return void  | 
            ||
| 411 | * @throws RouteMiddlewareFailedException if a route middleware returned false.  | 
            ||
| 412 | * @throws InvalidMiddlewareException if a middleware is invalid.  | 
            ||
| 413 | */  | 
            ||
| 414 | private function handleMiddlewares(array $middlewares, array $parameters, array $arguments) : void  | 
            ||
| 432 | }  | 
            ||
| 433 | 
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.