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 |
||
16 | class ConnectionTuneOk extends Frame |
||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $channelMax; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $frameMax; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $heartbeat; |
||
32 | |||
33 | /** |
||
34 | * @param int $channel |
||
35 | * @param int $channelMax |
||
36 | * @param int $frameMax |
||
37 | * @param int $heartbeat |
||
38 | */ |
||
39 | public function __construct($channel, $channelMax, $frameMax, $heartbeat) |
||
40 | { |
||
41 | $this->channelMax = $channelMax; |
||
42 | $this->frameMax = $frameMax; |
||
43 | $this->heartbeat = $heartbeat; |
||
44 | |||
45 | parent::__construct($channel); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Negotiated maximum channels. |
||
50 | * |
||
51 | * @return int |
||
52 | */ |
||
53 | public function getChannelMax() |
||
54 | { |
||
55 | return $this->channelMax; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Negotiated maximum frame size. |
||
60 | * |
||
61 | * @return int |
||
62 | */ |
||
63 | public function getFrameMax() |
||
64 | { |
||
65 | return $this->frameMax; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Desired heartbeat delay. |
||
70 | * |
||
71 | * @return int |
||
72 | */ |
||
73 | public function getHeartbeat() |
||
74 | { |
||
75 | return $this->heartbeat; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | public function encode() |
||
82 | { |
||
83 | $data = "\x00\x0A\x00\x1F". |
||
84 | Value\ShortValue::encode($this->channelMax). |
||
85 | Value\LongValue::encode($this->frameMax). |
||
86 | Value\ShortValue::encode($this->heartbeat); |
||
87 | |||
88 | return "\x01".pack('nN', $this->channel, strlen($data)).$data."\xCE"; |
||
89 | } |
||
90 | } |
||
91 |