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 |
||
8 | class Route |
||
9 | { |
||
10 | 4 | public function __construct( |
|
29 | |||
30 | /** |
||
31 | * @param Application $app |
||
32 | * @param callable $function |
||
33 | * @param Request $request |
||
34 | * @return Response |
||
35 | */ |
||
36 | 1 | public function invoke(Application $app, callable $function, Request $request) |
|
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | 1 | public function getSummary() |
|
71 | |||
72 | /** |
||
73 | * @param string $summary |
||
74 | */ |
||
75 | public function setSummary($summary) |
||
79 | |||
80 | /** |
||
81 | * @return string |
||
82 | */ |
||
83 | 1 | public function getDescription() |
|
87 | |||
88 | /** |
||
89 | * @param string $description |
||
90 | */ |
||
91 | public function setDescription($description) |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 5 | public function getMethod() |
|
103 | |||
104 | /** |
||
105 | * @param string $method |
||
106 | */ |
||
107 | public function setMethod($method) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 5 | public function getUri() |
|
119 | |||
120 | /** |
||
121 | * @param string $uri |
||
122 | */ |
||
123 | public function setUri($uri) |
||
127 | |||
128 | /** |
||
129 | * @return RequestHandler |
||
130 | */ |
||
131 | 6 | public function getRequestHandler() |
|
135 | |||
136 | /** |
||
137 | * @param RequestHandler $requestHandler |
||
138 | */ |
||
139 | public function setRequestHandler($requestHandler) |
||
143 | |||
144 | /** |
||
145 | * @return ResponseHandler |
||
146 | */ |
||
147 | 5 | public function getResponseHandler() |
|
151 | |||
152 | /** |
||
153 | * @param ResponseHandler $responseHandler |
||
154 | */ |
||
155 | public function setResponseHandler($responseHandler) |
||
159 | |||
160 | /** |
||
161 | * @return ExceptionHandler |
||
162 | */ |
||
163 | 3 | public function getExceptionHandler() |
|
167 | |||
168 | /** |
||
169 | * @param ExceptionHandler $exceptionHandler |
||
170 | */ |
||
171 | public function setExceptionHandler($exceptionHandler) |
||
175 | |||
176 | |||
177 | /** |
||
178 | * @return string[] |
||
179 | */ |
||
180 | public function getHooks() |
||
184 | |||
185 | /** |
||
186 | * @param string[] $hooks |
||
187 | */ |
||
188 | public function setHooks($hooks) |
||
192 | |||
193 | 2 | public function addHook($className) |
|
197 | |||
198 | /** |
||
199 | * @return string[] |
||
200 | */ |
||
201 | 3 | public function getPathParams() |
|
205 | |||
206 | /** |
||
207 | * @param string[] $pathParams |
||
208 | */ |
||
209 | public function setPathParams($pathParams) |
||
213 | /** |
||
214 | * @param string $pathParam |
||
215 | */ |
||
216 | 3 | public function addPathParam($pathParam) |
|
221 | |||
222 | /** |
||
223 | * @param string $name |
||
224 | * @return bool |
||
225 | */ |
||
226 | 3 | public function hasPathParam($name) |
|
230 | |||
231 | /** |
||
232 | * @var RequestHandler |
||
233 | */ |
||
234 | private $requestHandler; |
||
235 | |||
236 | /** |
||
237 | * @var ResponseHandler |
||
238 | */ |
||
239 | private $responseHandler; |
||
240 | |||
241 | /** |
||
242 | * @var ExceptionHandler |
||
243 | */ |
||
244 | private $exceptionHandler; |
||
245 | |||
246 | /** |
||
247 | * http method |
||
248 | * @var string |
||
249 | */ |
||
250 | private $method; |
||
251 | |||
252 | /** |
||
253 | * uri |
||
254 | * @var string |
||
255 | */ |
||
256 | private $uri; |
||
257 | |||
258 | /** |
||
259 | * @var string |
||
260 | */ |
||
261 | private $summary = ''; |
||
262 | /** |
||
263 | * @var string |
||
264 | */ |
||
265 | private $description=''; |
||
266 | |||
267 | /** |
||
268 | * hook class names |
||
269 | * @var string[] |
||
270 | */ |
||
271 | private $hooks=[]; |
||
272 | |||
273 | /** |
||
274 | * @var string[] |
||
275 | */ |
||
276 | private $pathParams =[]; |
||
277 | |||
278 | } |
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.