| Total Complexity | 40 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 19 | ||
| Bugs | 0 | Features | 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 |
||
| 7 | class Router{ |
||
| 8 | use Helper, CheckTrait; |
||
| 9 | |||
| 10 | private static $instance = null; |
||
| 11 | private array $routers = []; |
||
| 12 | private ?string $prefix = null; |
||
| 13 | private $group = false; |
||
| 14 | private $lastReturn = null; |
||
| 15 | private bool $instanced = false; |
||
| 16 | |||
| 17 | public function __construct() |
||
| 18 | { |
||
| 19 | return $this; |
||
| 20 | } |
||
| 21 | |||
| 22 | public static function create(): Router |
||
| 23 | { |
||
| 24 | if(!self::getInstance()->instanced){ |
||
| 25 | self::getInstance()->checkConfig(); |
||
| 26 | self::getInstance()->import(ROUTER_CONFIG['path']); |
||
| 27 | self::getInstance()->instanced = true; |
||
| 28 | } |
||
| 29 | |||
| 30 | return self::getInstance(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public static function getInstance(): Router |
||
| 34 | { |
||
| 35 | self::$instance = (is_null(self::$instance)) ? new self() : self::$instance; |
||
| 36 | return self::$instance; |
||
| 37 | } |
||
| 38 | |||
| 39 | public static function any(string $uri, $walking): Router |
||
| 40 | { |
||
| 41 | self::getInstance()->add($uri, $walking, 'get'); |
||
| 42 | self::getInstance()->add($uri, $walking, 'post'); |
||
| 43 | return self::getInstance()->add($uri, $walking, 'ajax'); |
||
| 44 | } |
||
| 45 | |||
| 46 | public static function get(string $uri, $walking): Router |
||
| 47 | { |
||
| 48 | return self::getInstance()->add($uri, $walking, 'get'); |
||
| 49 | } |
||
| 50 | |||
| 51 | public static function post(string $uri, $walking): Router |
||
| 52 | { |
||
| 53 | return self::getInstance()->add($uri, $walking, 'post'); |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function ajax(string $uri, $walking): Router |
||
| 57 | { |
||
| 58 | return self::getInstance()->add($uri, $walking, 'ajax'); |
||
| 59 | } |
||
| 60 | |||
| 61 | public static function form(string $uri, $walking): Router |
||
| 62 | { |
||
| 63 | return self::getInstance()->add($uri, $walking, 'form'); |
||
| 64 | } |
||
| 65 | |||
| 66 | public static function add(string $uri, $walking, string $protocol): Router |
||
| 67 | { |
||
| 68 | return self::getInstance()->set($uri, $walking, $protocol); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function set($url ,$walking , string $protocol): Router |
||
| 72 | { |
||
| 73 | $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
||
| 74 | |||
| 75 | foreach($this->routers as $key => $value){ |
||
| 76 | if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
||
| 77 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->checkTypeRole($walking); |
||
| 82 | |||
| 83 | $route = [ |
||
| 84 | 'url' => $this->prefix.$url, |
||
| 85 | 'role' => $walking, |
||
| 86 | 'protocol' => $protocol, |
||
| 87 | 'filters' => null, |
||
| 88 | 'group' => self::getInstance()->group |
||
| 89 | ]; |
||
| 90 | |||
| 91 | $this->routers[] = $route; |
||
| 92 | |||
| 93 | return self::getInstance(); |
||
| 94 | } |
||
| 95 | |||
| 96 | public static function group(string $prefix,$callback): Router |
||
| 97 | { |
||
| 98 | self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
||
| 99 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
||
| 100 | $callback(); |
||
| 101 | self::getInstance()->group = null; |
||
| 102 | self::getInstance()->prefix = null; |
||
| 103 | self::getInstance()->lastReturn = true; |
||
| 104 | return self::getInstance(); |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function name(string $name): Router |
||
| 108 | { |
||
| 109 | if(self::getInstance()->lastReturn){ |
||
| 110 | throw new Exception("There is no reason to call a {$name} route group."); |
||
| 111 | } |
||
| 112 | |||
| 113 | $currentRoute = end(self::getInstance()->routers); |
||
| 114 | |||
| 115 | foreach(self::getInstance()->routers as $key => $value){ |
||
| 116 | if(array_key_exists($name, self::getInstance()->routers)){ |
||
| 117 | throw new Exception("There is already a route with the name {$name} configured."); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $currentRoute['name'] = $name; |
||
| 122 | |||
| 123 | self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute; |
||
| 124 | |||
| 125 | self::getInstance()->lastReturn = null; |
||
| 126 | |||
| 127 | return self::getInstance(); |
||
| 128 | } |
||
| 129 | |||
| 130 | private function byName(?string $routName) |
||
| 131 | { |
||
| 132 | if(!is_null($routName)){ |
||
| 133 | $currentProtocol = $this->getProtocol(); |
||
| 134 | |||
| 135 | $this->checkName($routName); |
||
| 136 | |||
| 137 | $route = $this->routers[$routName]; |
||
| 138 | |||
| 139 | if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
||
| 140 | throw new Exception('Page not found.',404); |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->checkFiltering($route); |
||
| 144 | |||
| 145 | $this->toHiking($route['role']); |
||
| 146 | throw true; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | public static function dispatch(?string $routeName = null): bool |
||
| 151 | { |
||
| 152 | $instance = self::create(); |
||
| 153 | |||
| 154 | $instance->getInstance()->byName($routeName); |
||
| 155 | |||
| 156 | $currentProtocol = $instance->getInstance()->getProtocol(); |
||
| 157 | |||
| 158 | foreach(array_reverse($instance->getInstance()->routers) as $r => $route){ |
||
| 159 | |||
| 160 | $instance->getInstance()->currentRoute = $route; |
||
| 161 | |||
| 162 | if(!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)){ |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | |||
| 166 | $instance->getInstance()->hasProtocol($route, $currentProtocol); |
||
| 167 | |||
| 168 | $routeLoop = $instance->getInstance()->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
||
| 169 | |||
| 170 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
||
| 171 | |||
| 172 | $routeRequest = $instance->getInstance()->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
||
| 173 | |||
| 174 | if($instance->getInstance()->checkNumparams($routeLoop, $routeRequest) || !$instance->getInstance()->checkParameters($routeLoop, $routeRequest)){ |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | $instance->getInstance()->checkFiltering($route); |
||
| 179 | |||
| 180 | $instance->getInstance()->toHiking($route['role']); |
||
| 181 | return true; |
||
| 182 | } |
||
| 183 | |||
| 184 | $instance->getInstance()->currentRoute = null; |
||
| 185 | |||
| 186 | throw new Exception('Page not found.',404); |
||
| 187 | } |
||
| 188 | |||
| 189 | public static function filter($filters): Router |
||
| 210 | } |
||
| 211 | |||
| 212 | public static function addFilter(array $route, $filter): array |
||
| 231 | } |
||
| 232 | |||
| 233 | } |
||
| 234 |