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 |
||
15 | class GoAwayFrame extends \Hyphper\Frame |
||
16 | { |
||
17 | protected $defined_flags = []; |
||
18 | protected $type = 0x07; |
||
19 | protected $stream_association = self::NO_STREAM; |
||
20 | protected $last_stream_id; |
||
21 | protected $error_code; |
||
22 | protected $additional_data; |
||
23 | |||
24 | 5 | View Code Duplication | public function __construct(array $options = []) |
31 | |||
32 | 1 | public function serializeBody(): string |
|
44 | |||
45 | /** |
||
46 | * Given the body of a frame, parses it into frame data. This populates |
||
47 | * the non-header parts of the frame: that is, it does not populate the |
||
48 | * stream ID or flags. |
||
49 | * |
||
50 | * |
||
51 | * @param string $data |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | 2 | public function parseBody(string $data) |
|
69 | |||
70 | /** |
||
71 | * @param int $last_stream_id |
||
72 | * |
||
73 | * @return GoAwayFrame |
||
74 | */ |
||
75 | 1 | public function setLastStreamId($last_stream_id) |
|
81 | |||
82 | /** |
||
83 | * @return int |
||
84 | */ |
||
85 | public function getLastStreamId() |
||
89 | |||
90 | /** |
||
91 | * @param int $error_code |
||
92 | * |
||
93 | * @return GoAwayFrame |
||
94 | */ |
||
95 | 1 | public function setErrorCode($error_code) |
|
101 | |||
102 | /** |
||
103 | * @return int |
||
104 | */ |
||
105 | public function getErrorCode() |
||
109 | |||
110 | /** |
||
111 | * @param int $additional_data |
||
112 | * |
||
113 | * @return GoAwayFrame |
||
114 | */ |
||
115 | 1 | public function setAdditionalData($additional_data) |
|
121 | |||
122 | /** |
||
123 | * @return int |
||
124 | */ |
||
125 | 1 | public function getAdditionalData() |
|
129 | } |
||
130 |
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.