Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | final class Router |
||
27 | { |
||
28 | /** |
||
29 | * routes array to be registered. |
||
30 | * Some routes may have aliases to be used in templating system |
||
31 | * Route item can be defined using array key as an alias key. |
||
32 | * @var array |
||
33 | */ |
||
34 | private $routes = []; |
||
35 | |||
36 | /** |
||
37 | * aliases array to be registered. |
||
38 | * Each route item is an array has items respectively : Request Method, Request Uri, Controller/Action, Return Type. |
||
39 | * @var array |
||
40 | */ |
||
41 | private $aliases = []; |
||
42 | |||
43 | /** |
||
44 | * HTTP request Method |
||
45 | * @var string |
||
46 | */ |
||
47 | private $method; |
||
48 | |||
49 | /** |
||
50 | * Request Uri |
||
51 | * @var string |
||
52 | */ |
||
53 | private $requestedPath; |
||
54 | |||
55 | /** |
||
56 | * Default return type if not noted in the $routes |
||
57 | * @var string |
||
58 | */ |
||
59 | private $defaultReturnType; |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Translation array. |
||
64 | * Make sures about return type. |
||
65 | * @var array |
||
66 | */ |
||
67 | private static $translations = [ |
||
68 | 'h' => 'html', |
||
69 | 'html' => 'html', |
||
70 | 'r' => 'redirect', |
||
71 | 'redirect' => 'redirect', |
||
72 | 'j' => 'json', |
||
73 | 'json' => 'json', |
||
74 | 't' => 'text', |
||
75 | 'text' => 'text', |
||
76 | 'd' => 'download', |
||
77 | 'download' => 'download' |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * Valid Request Methods array. |
||
82 | * Make sures about requested methods. |
||
83 | * @var array |
||
84 | */ |
||
85 | private static $validRequestMethods = [ |
||
86 | 'GET', |
||
87 | 'OPTIONS', |
||
88 | 'HEAD', |
||
89 | 'POST', |
||
90 | 'PUT', |
||
91 | 'DELETE', |
||
92 | 'PATCH' |
||
93 | ]; |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Valid Request Methods array. |
||
98 | * Make sures about return type. |
||
99 | * Index 0 is also default value. |
||
100 | * @var array |
||
101 | */ |
||
102 | private static $validReturnTypes = [ |
||
103 | 'html', |
||
104 | 'json', |
||
105 | 'text', |
||
106 | 'redirect', |
||
107 | 'download' |
||
108 | ]; |
||
109 | |||
110 | /** |
||
111 | * Router constructor. |
||
112 | * Create new router. |
||
113 | * |
||
114 | * @param string $defaultReturnType |
||
115 | * @param string $method |
||
116 | * @param string $requestedPath |
||
117 | * @param string $folder |
||
118 | * @throws UnexpectedValueException |
||
119 | */ |
||
120 | 11 | public function __construct( |
|
134 | |||
135 | /** |
||
136 | * Remove sub folder from requestedPath if defined |
||
137 | * @param string $requestPath |
||
138 | * @param string $folder |
||
139 | * @return string |
||
140 | */ |
||
141 | 10 | private function extractFolder(string $requestPath, string $folder) |
|
151 | |||
152 | /** |
||
153 | * add route to routes list |
||
154 | * @param string|array requestMethods |
||
155 | * @param string $route |
||
156 | * @param string $action |
||
157 | * @param string $returnType |
||
158 | * @param string $alias |
||
159 | * @throws InvalidArgumentException |
||
160 | * @throws UnexpectedValueException |
||
161 | */ |
||
162 | 9 | public function add($requestMethods, string $route, string $action, string $returnType=null, string $alias=null) |
|
184 | |||
185 | /** |
||
186 | * @param string $method |
||
187 | * @param array $args |
||
188 | * @throws UnexpectedValueException |
||
189 | */ |
||
190 | 2 | public function __call(string $method, array $args) |
|
205 | |||
206 | /** |
||
207 | * Dispatch against the provided HTTP method verb and URI. |
||
208 | * @return array |
||
209 | */ |
||
210 | 3 | private function dispatcher() |
|
225 | |||
226 | /** |
||
227 | * Define Closures for all routes that returns controller info to be used. |
||
228 | * @param FastRoute\RouteCollector $route |
||
229 | */ |
||
230 | 3 | private function addRoutes(FastRoute\RouteCollector $route) |
|
241 | |||
242 | |||
243 | |||
244 | /** |
||
245 | * Get router data that includes route info and aliases |
||
246 | */ |
||
247 | 3 | public function getRoute() |
|
257 | |||
258 | |||
259 | /** |
||
260 | * Get route info for requested uri |
||
261 | * @param array $routeInfo |
||
262 | * @return array $routerData |
||
263 | */ |
||
264 | 3 | private function runDispatcher(array $routeInfo) |
|
280 | |||
281 | /** |
||
282 | * Get routeData according to dispatcher's results |
||
283 | * @param array $routeInfo |
||
284 | * @return array |
||
285 | */ |
||
286 | 3 | private function getRouteData(array $routeInfo) |
|
299 | } |
||
300 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.