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 |
||
20 | class SettingsFrame extends \Hyphper\Frame |
||
21 | { |
||
22 | protected $defined_flags = [Flag::ACK]; |
||
23 | protected $type = 0x04; |
||
24 | protected $stream_association = self::NO_STREAM; |
||
25 | protected $settings; |
||
26 | |||
27 | /** |
||
28 | * The byte that signals the SETTINGS_HEADER_TABLE_SIZE setting. |
||
29 | */ |
||
30 | const HEADER_TABLE_SIZE = 0x01; |
||
31 | |||
32 | /** |
||
33 | * The byte that signals the SETTINGS_ENABLE_PUSH setting. |
||
34 | */ |
||
35 | const ENABLE_PUSH = 0x02; |
||
36 | /** |
||
37 | * The byte that signals the SETTINGS_MAX_CONCURRENT_STREAMS setting. |
||
38 | */ |
||
39 | const MAX_CONCURRENT_STREAMS = 0x03; |
||
40 | |||
41 | /** |
||
42 | * The byte that signals the SETTINGS_INITIAL_WINDOW_SIZE setting. |
||
43 | */ |
||
44 | const INITIAL_WINDOW_SIZE = 0x04; |
||
45 | |||
46 | /** |
||
47 | * The byte that signals the SETTINGS_MAX_FRAME_SIZE setting. |
||
48 | */ |
||
49 | const MAX_FRAME_SIZE = 0x05; |
||
50 | |||
51 | /** |
||
52 | * The byte that signals the SETTINGS_MAX_HEADER_LIST_SIZE setting. |
||
53 | */ |
||
54 | const MAX_HEADER_LIST_SIZE = 0x06; |
||
55 | |||
56 | /** |
||
57 | * SettingsFrame constructor. |
||
58 | * |
||
59 | * @param array $options |
||
60 | * @throws InvalidFrameException |
||
61 | */ |
||
62 | 9 | public function __construct(array $options = []) // array $settings = [], ... $args) |
|
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 1 | public function serializeBody(): string |
|
87 | |||
88 | /** |
||
89 | * Given the body of a frame, parses it into frame data. This populates |
||
90 | * the non-header parts of the frame: that is, it does not populate the |
||
91 | * stream ID or flags. |
||
92 | * |
||
93 | * |
||
94 | * @param string $data |
||
95 | * @throws InvalidFrameException |
||
96 | * @return void |
||
97 | */ |
||
98 | 2 | public function parseBody(string $data) |
|
112 | |||
113 | /** |
||
114 | * @return array|mixed |
||
115 | */ |
||
116 | 3 | public function getSettings() |
|
120 | |||
121 | /** |
||
122 | * @param array|mixed $settings |
||
123 | * @return SettingsFrame |
||
124 | */ |
||
125 | 1 | public function setSettings($settings) |
|
130 | } |
||
131 |
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.