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 |
||
21 | class ErrorPlugin implements Plugin |
||
22 | { |
||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | 3 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
34 | |||
35 | /** |
||
36 | * Transform response to an error if possible. |
||
37 | * |
||
38 | * @param RequestInterface $request Request of the call |
||
39 | * @param ResponseInterface $response Response of the call |
||
40 | * |
||
41 | * @throws ClientErrorException If response status code is a 4xx |
||
42 | * @throws ServerErrorException If response status code is a 5xx |
||
43 | * |
||
44 | * @return ResponseInterface If status code is not in 4xx or 5xx return response |
||
45 | */ |
||
46 | 3 | protected function transformResponseToException(RequestInterface $request, ResponseInterface $response) |
|
58 | } |
||
59 |
If you suppress an error, we recommend checking for the error condition explicitly: