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 |
||
23 | class DecoderPlugin implements Plugin |
||
24 | { |
||
25 | /** |
||
26 | * @var bool Whether this plugin decode stream with value in the Content-Encoding header (default to true). |
||
27 | * |
||
28 | * If set to false only the Transfer-Encoding header will be used. |
||
29 | */ |
||
30 | private $useContentEncoding; |
||
31 | |||
32 | /** |
||
33 | * Available options for $config are: |
||
34 | * - use_content_encoding: Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true). |
||
35 | * |
||
36 | * @param array $config |
||
37 | */ |
||
38 | 7 | View Code Duplication | public function __construct(array $config = []) |
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 5 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
67 | |||
68 | /** |
||
69 | * Decode a response body given its Transfer-Encoding or Content-Encoding value. |
||
70 | * |
||
71 | * @param ResponseInterface $response Response to decode |
||
72 | * |
||
73 | * @return ResponseInterface New response decoded |
||
74 | */ |
||
75 | 5 | private function decodeResponse(ResponseInterface $response) |
|
85 | |||
86 | /** |
||
87 | * Decode a response on a specific header (content encoding or transfer encoding mainly). |
||
88 | * |
||
89 | * @param string $headerName Name of the header |
||
90 | * @param ResponseInterface $response Response |
||
91 | * |
||
92 | * @return ResponseInterface A new instance of the response decoded |
||
93 | */ |
||
94 | 5 | private function decodeOnEncodingHeader($headerName, ResponseInterface $response) |
|
117 | |||
118 | /** |
||
119 | * Decorate a stream given an encoding. |
||
120 | * |
||
121 | * @param string $encoding |
||
122 | * @param StreamInterface $stream |
||
123 | * |
||
124 | * @return StreamInterface|false A new stream interface or false if encoding is not supported |
||
125 | */ |
||
126 | 4 | private function decorateStream($encoding, StreamInterface $stream) |
|
146 | } |
||
147 |
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.