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 |
||
9 | class RouterInjectable |
||
10 | { |
||
11 | /** |
||
12 | * Properties |
||
13 | * |
||
14 | */ |
||
15 | private $routes = []; // All the routes |
||
16 | private $internalRoutes = []; // All internal routes |
||
17 | private $defaultRoute = null; // A default route to catch all |
||
18 | |||
19 | |||
20 | |||
21 | /** |
||
22 | * Get all routes. |
||
23 | * |
||
24 | * @return array with all routes. |
||
25 | */ |
||
26 | public function getAll() |
||
30 | |||
31 | |||
32 | |||
33 | /** |
||
34 | * Get all internal routes. |
||
35 | * |
||
36 | * @return array with internal routes. |
||
37 | */ |
||
38 | public function getInternal() |
||
42 | |||
43 | |||
44 | |||
45 | /** |
||
46 | * Add a route to the router. |
||
47 | * |
||
48 | * @param string $rule for this route |
||
49 | * @param mixed $action null, string or callable to implement a controller for the route |
||
50 | * |
||
51 | * @return class as new route |
||
52 | */ |
||
53 | 5 | View Code Duplication | public function add($rule, $action = null) |
66 | |||
67 | |||
68 | |||
69 | /** |
||
70 | * Add a default route to the router, to use when all other routes fail. |
||
71 | * |
||
72 | * @param mixed $action null, string or callable to implement a controller for the route |
||
73 | * |
||
74 | * @return class as new route |
||
75 | */ |
||
76 | 1 | View Code Duplication | public function addDefault($action) |
85 | |||
86 | |||
87 | |||
88 | /** |
||
89 | * Add an internal (not exposed to url-matching) route to the router. |
||
90 | * |
||
91 | * @param string $rule for this route |
||
92 | * @param mixed $action null, string or callable to implement a controller for the route |
||
93 | * |
||
94 | * @return class as new route |
||
95 | */ |
||
96 | 3 | public function addInternal($rule, $action = null) |
|
103 | |||
104 | |||
105 | |||
106 | /** |
||
107 | * Add an internal (not exposed to url-matching) route to the router. |
||
108 | * |
||
109 | * @param string $rule for this route |
||
110 | * @param mixed $action null, string or callable to implement a |
||
111 | * controller for the route |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | 4 | public function handleInternal($rule) |
|
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * Handle the routes and match them towards the request, dispatch them |
||
128 | * when a match is made. Each route handler may throw exceptions that |
||
129 | * may redirect to an internal route for error handling. |
||
130 | * |
||
131 | * @param string $query the query/route to match a handler for. |
||
132 | * |
||
133 | * @return mixed content returned from route. |
||
134 | */ |
||
135 | 6 | public function handle($query) |
|
160 | } |
||
161 |
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.