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 |
||
14 | class HttpReader implements HttpReaderInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var int |
||
18 | */ |
||
19 | const DEFAULT_MAX_SIZE = 0x4000; // 16 kB |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | const DEFAULT_START_LINE_LENGTH = 0x400; // 1 kB |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | const HTTP_EOM = "\r\n\r\n"; |
||
30 | |||
31 | /** |
||
32 | * @var HttpParserInterface |
||
33 | */ |
||
34 | protected $parser; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $maxFrameSize; |
||
40 | |||
41 | /** |
||
42 | * @param mixed[] $options |
||
43 | */ |
||
44 | 77 | public function __construct($options = []) |
|
52 | |||
53 | /** |
||
54 | * |
||
55 | */ |
||
56 | 32 | public function __destruct() |
|
61 | |||
62 | /** |
||
63 | * @override |
||
64 | * @inheritDoc |
||
65 | */ |
||
66 | 6 | View Code Duplication | public function readRequest(BufferInterface $buffer, $data) |
93 | |||
94 | /** |
||
95 | * @override |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | 3 | View Code Duplication | public function readResponse(BufferInterface $buffer, $data) |
125 | } |
||
126 |
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.