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 |
||
| 27 | class DecoderPlugin implements Plugin |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var bool Whether this plugin decode stream with value in the Content-Encoding header (default to true). |
||
| 31 | * |
||
| 32 | * If set to false only the Transfer-Encoding header will be used. |
||
| 33 | */ |
||
| 34 | private $useContentEncoding; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param array $config { |
||
| 38 | * |
||
| 39 | * @var bool $use_content_encoding Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true). |
||
| 40 | * } |
||
| 41 | */ |
||
| 42 | 7 | View Code Duplication | public function __construct(array $config = []) |
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 5 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Decode a response body given its Transfer-Encoding or Content-Encoding value. |
||
| 74 | * |
||
| 75 | * @param ResponseInterface $response Response to decode |
||
| 76 | * |
||
| 77 | * @return ResponseInterface New response decoded |
||
| 78 | */ |
||
| 79 | 5 | private function decodeResponse(ResponseInterface $response) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Decode a response on a specific header (content encoding or transfer encoding mainly). |
||
| 92 | * |
||
| 93 | * @param string $headerName Name of the header |
||
| 94 | * @param ResponseInterface $response Response |
||
| 95 | * |
||
| 96 | * @return ResponseInterface A new instance of the response decoded |
||
| 97 | */ |
||
| 98 | 5 | private function decodeOnEncodingHeader($headerName, ResponseInterface $response) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Decorate a stream given an encoding. |
||
| 124 | * |
||
| 125 | * @param string $encoding |
||
| 126 | * @param StreamInterface $stream |
||
| 127 | * |
||
| 128 | * @return StreamInterface|false A new stream interface or false if encoding is not supported |
||
| 129 | */ |
||
| 130 | 4 | private function decorateStream($encoding, StreamInterface $stream) |
|
| 150 | } |
||
| 151 |
If you suppress an error, we recommend checking for the error condition explicitly: