| Total Complexity | 49 |
| Total Lines | 321 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Dispatch 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 Dispatch, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class Dispatch |
||
| 12 | { |
||
| 13 | /** @var bool|string */ |
||
| 14 | protected $projectUrl; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | protected $patch; |
||
| 18 | |||
| 19 | /** @var string */ |
||
| 20 | protected $separator; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | protected $httpMethod; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | protected $routes; |
||
| 27 | |||
| 28 | /** @var null|string */ |
||
| 29 | protected $group; |
||
| 30 | |||
| 31 | /** @var null|array */ |
||
| 32 | protected $route; |
||
| 33 | |||
| 34 | /** @var null|string */ |
||
| 35 | protected $namespace; |
||
| 36 | |||
| 37 | /** @var null|array */ |
||
| 38 | protected $data; |
||
| 39 | |||
| 40 | /** @var int */ |
||
| 41 | protected $error; |
||
| 42 | |||
| 43 | /** @const int Bad Request */ |
||
| 44 | public const BAD_REQUEST = 400; |
||
| 45 | |||
| 46 | /** @const int Not Found */ |
||
| 47 | public const NOT_FOUND = 404; |
||
| 48 | |||
| 49 | /** @const int Method Not Allowed */ |
||
| 50 | public const METHOD_NOT_ALLOWED = 405; |
||
| 51 | |||
| 52 | /** @const int Not Implemented */ |
||
| 53 | public const NOT_IMPLEMENTED = 501; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Dispatch constructor. |
||
| 57 | * |
||
| 58 | * @param string $projectUrl |
||
| 59 | * @param null|string $separator |
||
| 60 | */ |
||
| 61 | public function __construct(string $projectUrl, ?string $separator = ":") |
||
| 62 | { |
||
| 63 | $this->projectUrl = (substr($projectUrl, "-1") == "/" ? substr($projectUrl, 0, -1) : $projectUrl); |
||
| 64 | $this->patch = (filter_input(INPUT_GET, "route", FILTER_DEFAULT) ?? "/"); |
||
| 65 | $this->separator = ($separator ?? ":"); |
||
| 66 | $this->httpMethod = $_SERVER['REQUEST_METHOD']; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function __debugInfo() |
||
| 73 | { |
||
| 74 | return $this->routes; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param null|string $group |
||
| 79 | * @return Dispatch |
||
| 80 | */ |
||
| 81 | public function group(?string $group): Dispatch |
||
| 82 | { |
||
| 83 | $this->group = ($group ? str_replace("/", "", $group) : null); |
||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param null|string $namespace |
||
| 89 | * @return Dispatch |
||
| 90 | */ |
||
| 91 | public function namespace(?string $namespace): Dispatch |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return null|array |
||
| 99 | */ |
||
| 100 | public function data(): ?array |
||
| 101 | { |
||
| 102 | return $this->data; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return null|int |
||
| 107 | */ |
||
| 108 | public function error(): ?int |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $name |
||
| 115 | * @param array|null $data |
||
| 116 | * @return string|null |
||
| 117 | */ |
||
| 118 | public function route(string $name, array $data = null): ?string |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $name |
||
| 130 | * @param array $route_item |
||
| 131 | * @param array|null $data |
||
| 132 | * @return string|null |
||
| 133 | */ |
||
| 134 | private function treat(string $name, array $route_item, array $data = null): ?string |
||
| 135 | { |
||
| 136 | if (!empty($route_item["name"]) && $route_item["name"] == $name) { |
||
| 137 | $route = $route_item["route"]; |
||
| 138 | if (!empty($data)) { |
||
| 139 | $arguments = []; |
||
| 140 | $params = []; |
||
| 141 | foreach ($data as $key => $value) { |
||
| 142 | if (!strstr($route, "{{$key}}")) { |
||
| 143 | $params[$key] = $value; |
||
| 144 | } |
||
| 145 | $arguments["{{$key}}"] = $value; |
||
| 146 | } |
||
| 147 | $route = $this->process($route, $arguments, $params); |
||
| 148 | } |
||
| 149 | return "{$this->projectUrl}{$route}"; |
||
| 150 | } |
||
| 151 | return null; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $route |
||
| 156 | * @param array $arguments |
||
| 157 | * @param array|null $params |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | private function process(string $route, array $arguments, array $params = null): string |
||
| 161 | { |
||
| 162 | $params = (!empty($params) ? "?" . http_build_query($params) : null); |
||
| 163 | return str_replace(array_keys($arguments), array_values($arguments), $route) . "{$params}"; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $route |
||
| 168 | * @param array|null $data |
||
| 169 | */ |
||
| 170 | public function redirect(string $route, array $data = null): void |
||
| 171 | { |
||
| 172 | if ($name = $this->route($route, $data)) { |
||
|
|
|||
| 173 | header("Location: {$name}"); |
||
| 174 | exit; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (filter_var($route, FILTER_VALIDATE_URL)) { |
||
| 178 | header("Location: {$route}"); |
||
| 179 | exit; |
||
| 180 | } |
||
| 181 | |||
| 182 | $route = (substr($route, 0, 1) == "/" ? $route : "/{$route}"); |
||
| 183 | header("Location: {$this->projectUrl}{$route}"); |
||
| 184 | exit; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function dispatch(): bool |
||
| 191 | { |
||
| 192 | if (empty($this->routes) || empty($this->routes[$this->httpMethod])) { |
||
| 193 | $this->error = self::NOT_IMPLEMENTED; |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->route = null; |
||
| 198 | foreach ($this->routes[$this->httpMethod] as $key => $route) { |
||
| 199 | if (preg_match("~^" . $key . "$~", $this->patch, $found)) { |
||
| 200 | $this->route = $route; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | return $this->execute(); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | private function execute() |
||
| 211 | { |
||
| 212 | if ($this->route) { |
||
| 213 | if (is_callable($this->route['handler'])) { |
||
| 214 | call_user_func($this->route['handler'], ($this->route['data'] ?? [])); |
||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | $controller = $this->route['handler']; |
||
| 219 | $method = $this->route['action']; |
||
| 220 | |||
| 221 | if (class_exists($controller)) { |
||
| 222 | $newController = new $controller($this); |
||
| 223 | if (method_exists($controller, $method)) { |
||
| 224 | $newController->$method(($this->route['data'] ?? [])); |
||
| 225 | return true; |
||
| 226 | } |
||
| 227 | |||
| 228 | $this->error = self::METHOD_NOT_ALLOWED; |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | |||
| 232 | $this->error = self::BAD_REQUEST; |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->error = self::NOT_FOUND; |
||
| 237 | return false; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $method |
||
| 242 | * @param string $route |
||
| 243 | * @param string|callable $handler |
||
| 244 | * @param null|string |
||
| 245 | * @return Dispatch |
||
| 246 | */ |
||
| 247 | protected function addRoute(string $method, string $route, $handler, string $name = null): Dispatch |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * httpMethod form spoofing |
||
| 283 | */ |
||
| 284 | protected function formSpoofing(): void |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param $handler |
||
| 317 | * @param $namespace |
||
| 318 | * @return string|callable |
||
| 319 | */ |
||
| 320 | private function handler($handler, $namespace) |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param $handler |
||
| 327 | * @return null|string |
||
| 328 | */ |
||
| 329 | private function action($handler): ?string |
||
| 332 | } |
||
| 333 | } |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.