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 |
||
17 | class ErrorPlugin implements Plugin |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | 3 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
30 | |||
31 | /** |
||
32 | * Transform response to an error if possible. |
||
33 | * |
||
34 | * @param RequestInterface $request Request of the call |
||
35 | * @param ResponseInterface $response Response of the call |
||
36 | * |
||
37 | * @throws ClientErrorException If response status code is a 4xx |
||
38 | * @throws ServerErrorException If response status code is a 5xx |
||
39 | * |
||
40 | * @return ResponseInterface If status code is not in 4xx or 5xx return response |
||
41 | */ |
||
42 | 3 | protected function transformResponseToException(RequestInterface $request, ResponseInterface $response) |
|
54 | } |
||
55 |
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.