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:
Complex classes like Frame often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Frame, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Frame |
||
10 | { |
||
11 | |||
12 | const DECODE_STATUS_MORE_DATA = 0; |
||
13 | const DECODE_STATUS_ERROR = -1; |
||
14 | const OP_CONTINUE = 0; |
||
15 | const OP_TEXT = 1; |
||
16 | const OP_BINARY = 2; |
||
17 | const OP_CLOSE = 8; |
||
18 | const OP_PING = 9; |
||
19 | const OP_PONG = 10; |
||
20 | const MASK_LENGTH = 4; |
||
21 | |||
22 | protected $data = ''; |
||
23 | protected $firstByte = null; |
||
24 | protected $secondByte = 0; |
||
25 | protected $isCoalesced = false; |
||
26 | protected $extendedPayload = ''; |
||
27 | |||
28 | protected $isClosed = false; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @return Frame |
||
33 | */ |
||
34 | public static function parse($data) |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * @param string $data |
||
52 | * @return Frame |
||
53 | */ |
||
54 | public static function generate($data) |
||
60 | |||
61 | /** |
||
62 | * |
||
63 | * @param string $data |
||
64 | * @return Frame |
||
65 | */ |
||
66 | public static function close($data) |
||
73 | |||
74 | protected function __construct($data = null, $final = true, $opcode = self::OP_TEXT) |
||
79 | |||
80 | public function setClosed($isClosed = true) |
||
84 | |||
85 | public function isClosed() |
||
89 | |||
90 | public function getOpcode() |
||
94 | |||
95 | public function setFirstByte($byte) |
||
99 | |||
100 | public function setSecondByte($byte) |
||
104 | |||
105 | public function setData($data) |
||
110 | |||
111 | public function appendData($data) |
||
116 | |||
117 | public function setMask($mask) |
||
122 | |||
123 | public function isMask() |
||
127 | |||
128 | public function generateMaskingKey() |
||
137 | |||
138 | protected function applyMask($maskingKey, $payload = null) |
||
147 | |||
148 | protected function maskPayload($payload) |
||
157 | |||
158 | public function getPayloadLength($encodedData) |
||
182 | |||
183 | public function decode($encodedData) |
||
213 | |||
214 | public function isCoalesced() |
||
218 | |||
219 | public function getRSV1() |
||
223 | |||
224 | public function getRSV2() |
||
228 | |||
229 | public function getRSV3() |
||
233 | |||
234 | protected function verifyPayload() |
||
242 | |||
243 | public function getData() |
||
247 | |||
248 | public function encode() |
||
254 | |||
255 | protected function updatePayloadLength($data, &$secondByte, &$extendedPayload) |
||
276 | |||
277 | } |
||
278 |
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.