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 |
||
20 | class Api |
||
21 | { |
||
22 | /** @var RouteResolverInterface */ |
||
23 | private $routeResolver; |
||
24 | |||
25 | /** @var RouteContainer */ |
||
26 | private $routes; |
||
27 | |||
28 | /** @var Request */ |
||
29 | private $request; |
||
30 | |||
31 | /** @var Response */ |
||
32 | private $response; |
||
33 | |||
34 | /** @var Container */ |
||
35 | private $container; |
||
36 | |||
37 | /** @var ValidatorInterface */ |
||
38 | private $validator; |
||
39 | |||
40 | /** |
||
41 | * @param RouteResolverInterface $routeResolver |
||
42 | * @param Request $request |
||
43 | * @param Response $response |
||
44 | * @param Container $container |
||
45 | * @param ValidatorInterface $validator |
||
46 | */ |
||
47 | 16 | public function __construct( |
|
61 | |||
62 | /** |
||
63 | * Add api call |
||
64 | * @param string $method |
||
65 | * @param string $pattern |
||
66 | * @param string $handler |
||
67 | * @param array $params |
||
68 | */ |
||
69 | 16 | public function add($method, $pattern, $handler, $params = []) |
|
73 | |||
74 | /** |
||
75 | * Add get api call |
||
76 | * @param string $pattern |
||
77 | * @param string $handler |
||
78 | * @param array $params |
||
79 | */ |
||
80 | 14 | public function get($pattern, $handler, $params = []) |
|
84 | |||
85 | /** |
||
86 | * Add post api call |
||
87 | * @param string $pattern |
||
88 | * @param string $handler |
||
89 | * @param array $params |
||
90 | */ |
||
91 | 4 | public function post($pattern, $handler, $params = []) |
|
95 | |||
96 | /** |
||
97 | * Add put api call |
||
98 | * @param string $pattern |
||
99 | * @param string $handler |
||
100 | * @param array $params |
||
101 | */ |
||
102 | 2 | public function put($pattern, $handler, $params = []) |
|
106 | |||
107 | /** |
||
108 | * Add patch api call |
||
109 | * @param string $pattern |
||
110 | * @param string $handler |
||
111 | * @param array $params |
||
112 | */ |
||
113 | 2 | public function patch($pattern, $handler, $params = []) |
|
117 | |||
118 | /** |
||
119 | * Add delete api call |
||
120 | * @param string $pattern |
||
121 | * @param string $handler |
||
122 | * @param array $params |
||
123 | */ |
||
124 | 2 | public function delete($pattern, $handler, $params = []) |
|
128 | |||
129 | /** |
||
130 | * Add options api call |
||
131 | * @param string $pattern |
||
132 | * @param string $handler |
||
133 | * @param array $params |
||
134 | */ |
||
135 | 2 | public function options($pattern, $handler, $params = []) |
|
139 | |||
140 | /** |
||
141 | * Returns all registered routes |
||
142 | * @return RouteContainer |
||
143 | */ |
||
144 | 2 | public function getRoutes() |
|
148 | |||
149 | /** |
||
150 | * @param string $url |
||
151 | * @return IResponse |
||
152 | * @throws UnresolvedHandlerException |
||
153 | * @throws UnresolvedRouteException |
||
154 | * @throws ValidationFailedException |
||
155 | */ |
||
156 | 14 | public function run($url) |
|
182 | |||
183 | /** |
||
184 | * Get service by type or name from container |
||
185 | * @param string $entry |
||
186 | * @return bool|object |
||
187 | */ |
||
188 | 12 | View Code Duplication | private function getFromContainer($entry) |
200 | } |
||
201 |
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.