| Total Complexity | 46 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Route 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 Route, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Route |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Current module name |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $moduleName; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Module options |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $moduleOptions = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Identifies the group middleware |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $isGroupMiddlewares; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Identifies the group |
||
| 48 | * @var boolean |
||
| 49 | */ |
||
| 50 | private $isGroup = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Current group name |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $currentGroupName = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Current route |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $currentRoute = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Virtual routes |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $virtualRoutes = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Class constructor |
||
| 72 | * @param array $module |
||
| 73 | */ |
||
| 74 | public function __construct(array $module) |
||
| 75 | { |
||
| 76 | $this->virtualRoutes['*'] = []; |
||
| 77 | $this->moduleName = key($module); |
||
| 78 | $this->moduleOptions = $module[key($module)]; |
||
| 79 | |||
| 80 | if (config()->has('resource_cache') && !config()->has('view_cache')){ |
||
| 81 | config()->import(new Setup('config', 'view_cache')); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Adds new route entry to routes |
||
| 87 | * @param string $route |
||
| 88 | * @param string $method |
||
| 89 | * @param array $params |
||
| 90 | * @return Route |
||
| 91 | */ |
||
| 92 | public function add(string $route, string $method, ...$params): Route |
||
| 93 | { |
||
| 94 | $this->currentRoute = [ |
||
| 95 | 'route' => !empty($this->moduleOptions['prefix']) ? $this->moduleOptions['prefix'] . '/' . $route : $route, |
||
| 96 | 'prefix' => $this->moduleOptions['prefix'], |
||
| 97 | 'method' => $method, |
||
| 98 | 'module' => $this->moduleName |
||
| 99 | ]; |
||
| 100 | |||
| 101 | if ($this->canSetCacheToCurrentRoute()){ |
||
| 102 | $this->currentRoute['cache'] = [ |
||
| 103 | 'shouldCache' => true, |
||
| 104 | 'ttl' => config()->get('view_cache.ttl', 300), |
||
| 105 | ]; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (is_callable($params[0])) { |
||
| 109 | $this->currentRoute['callback'] = $params[0]; |
||
| 110 | } else { |
||
| 111 | $this->currentRoute['controller'] = $params[0]; |
||
| 112 | $this->currentRoute['action'] = $params[1]; |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($this->currentGroupName) { |
||
| 116 | $this->currentRoute['group'] = $this->currentGroupName; |
||
| 117 | $this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute; |
||
| 118 | } else { |
||
| 119 | $this->isGroup = false; |
||
| 120 | $this->isGroupMiddlewares = false; |
||
| 121 | $this->virtualRoutes['*'][] = $this->currentRoute; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Adds new get route entry to routes |
||
| 129 | * @param string $route |
||
| 130 | * @param array $params |
||
| 131 | * @return Route |
||
| 132 | */ |
||
| 133 | public function get(string $route, ...$params): Route |
||
| 134 | { |
||
| 135 | return $this->add($route, 'GET', ...$params); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Adds new post route entry to routes |
||
| 140 | * @param string $route |
||
| 141 | * @param array $params |
||
| 142 | * @return Route |
||
| 143 | */ |
||
| 144 | public function post(string $route, ...$params): Route |
||
| 145 | { |
||
| 146 | return $this->add($route, 'POST', ...$params); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Starts a named group of routes |
||
| 151 | * @param string $groupName |
||
| 152 | * @param Closure $callback |
||
| 153 | * @return Route |
||
| 154 | */ |
||
| 155 | public function group(string $groupName, Closure $callback): Route |
||
| 156 | { |
||
| 157 | $this->currentGroupName = $groupName; |
||
| 158 | |||
| 159 | $this->isGroup = true; |
||
| 160 | $this->isGroupMiddlewares = false; |
||
| 161 | $callback($this); |
||
| 162 | $this->isGroupMiddlewares = true; |
||
| 163 | $this->currentGroupName = null; |
||
| 164 | |||
| 165 | return $this; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Adds middlewares to routes and route groups |
||
| 170 | * @param array $middlewares |
||
| 171 | * @return Route |
||
| 172 | */ |
||
| 173 | public function middlewares(array $middlewares = []): Route |
||
| 174 | { |
||
| 175 | if (!$this->isGroup) { |
||
| 176 | end($this->virtualRoutes['*']); |
||
| 177 | $lastKey = key($this->virtualRoutes['*']); |
||
| 178 | $this->assignMiddlewaresToRoute($this->virtualRoutes['*'][$lastKey], $middlewares); |
||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | end($this->virtualRoutes); |
||
| 183 | $lastKeyOfFirstRound = key($this->virtualRoutes); |
||
| 184 | |||
| 185 | if (!$this->isGroupMiddlewares) { |
||
| 186 | end($this->virtualRoutes[$lastKeyOfFirstRound]); |
||
| 187 | $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]); |
||
| 188 | $this->assignMiddlewaresToRoute($this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound], $middlewares); |
||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
||
| 193 | $this->assignMiddlewaresToRoute($route, $middlewares); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $this; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function cacheable(bool $shouldCache, int $ttl = null): Route |
||
| 200 | { |
||
| 201 | if (!$ttl) { |
||
|
|
|||
| 202 | $ttl = config()->get('view_cache.ttl', 300); |
||
| 203 | } |
||
| 204 | |||
| 205 | if (isset($_COOKIE['PHPSESSID'])){ |
||
| 206 | if (!$this->isGroup){ |
||
| 207 | end($this->virtualRoutes['*']); |
||
| 208 | $lastKey = key($this->virtualRoutes['*']); |
||
| 209 | |||
| 210 | if (!$shouldCache && key_exists('cache', $this->virtualRoutes['*'][$lastKey])) { |
||
| 211 | unset($this->virtualRoutes['*'][$lastKey]['cache']); |
||
| 212 | }else{ |
||
| 213 | $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | end($this->virtualRoutes); |
||
| 220 | $lastKeyOfFirstRound = key($this->virtualRoutes); |
||
| 221 | |||
| 222 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
||
| 223 | if (!$shouldCache && key_exists('cache', $route)) { |
||
| 224 | unset($route['cache']); |
||
| 225 | }else{ |
||
| 226 | $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Sets a unique name for a route |
||
| 236 | * @param string $name |
||
| 237 | * @return Route |
||
| 238 | * @throws RouteException |
||
| 239 | */ |
||
| 240 | public function name(string $name): Route |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Gets the run-time routes |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function getRuntimeRoutes(): array |
||
| 270 | { |
||
| 271 | $runtimeRoutes = []; |
||
| 272 | foreach ($this->virtualRoutes as $virtualRoute) { |
||
| 273 | foreach ($virtualRoute as $route) { |
||
| 274 | $runtimeRoutes[] = $route; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | return $runtimeRoutes; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the virtual routes |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | public function getVirtualRoutes(): array |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Assigns middlewares to the route |
||
| 291 | * @param array $route |
||
| 292 | * @param array $middlewares |
||
| 293 | */ |
||
| 294 | private function assignMiddlewaresToRoute(array &$route, array $middlewares) |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | private function assignCacheToCurrentRoute(array &$route, bool $shouldCache, int $ttl) |
||
| 312 | ]; |
||
| 313 | } |
||
| 314 | |||
| 315 | private function canSetCacheToCurrentRoute(): bool |
||
| 323 | } |
||
| 324 | |||
| 326 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: