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 BasicConsume extends Frame |
||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $reserved1; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $queue; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $consumerTag; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $noLocal; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | private $noAck; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $exclusive; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $noWait; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $arguments = []; |
||
57 | |||
58 | /** |
||
59 | * @param int $channel |
||
60 | * @param int $reserved1 |
||
61 | * @param string $queue |
||
62 | * @param string $consumerTag |
||
63 | * @param bool $noLocal |
||
64 | * @param bool $noAck |
||
65 | * @param bool $exclusive |
||
66 | * @param bool $noWait |
||
67 | * @param array $arguments |
||
68 | */ |
||
69 | public function __construct($channel, $reserved1, $queue, $consumerTag, $noLocal, $noAck, $exclusive, $noWait, $arguments) |
||
70 | { |
||
71 | $this->reserved1 = $reserved1; |
||
72 | $this->queue = $queue; |
||
73 | $this->consumerTag = $consumerTag; |
||
74 | $this->noLocal = $noLocal; |
||
75 | $this->noAck = $noAck; |
||
76 | $this->exclusive = $exclusive; |
||
77 | $this->noWait = $noWait; |
||
78 | $this->arguments = $arguments; |
||
79 | |||
80 | parent::__construct($channel); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Reserved1. |
||
85 | * |
||
86 | * @return int |
||
87 | */ |
||
88 | public function getReserved1() |
||
89 | { |
||
90 | return $this->reserved1; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Queue. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getQueue() |
||
99 | { |
||
100 | return $this->queue; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * ConsumerTag. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getConsumerTag() |
||
109 | { |
||
110 | return $this->consumerTag; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * NoLocal. |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function isNoLocal() |
||
119 | { |
||
120 | return $this->noLocal; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * NoAck. |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function isNoAck() |
||
129 | { |
||
130 | return $this->noAck; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Request exclusive access. |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function isExclusive() |
||
139 | { |
||
140 | return $this->exclusive; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * NoWait. |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function isNoWait() |
||
149 | { |
||
150 | return $this->noWait; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Arguments for declaration. |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getArguments() |
||
159 | { |
||
160 | return $this->arguments; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function encode() |
||
167 | { |
||
168 | $data = "\x00\x3C\x00\x14". |
||
169 | Value\ShortValue::encode($this->reserved1). |
||
170 | Value\ShortStringValue::encode($this->queue). |
||
171 | Value\ShortStringValue::encode($this->consumerTag). |
||
172 | Value\OctetValue::encode(($this->noLocal ? 1 : 0) | (($this->noAck ? 1 : 0) << 1) | (($this->exclusive ? 1 : 0) << 2) | (($this->noWait ? 1 : 0) << 3)). |
||
173 | Value\TableValue::encode($this->arguments); |
||
174 | |||
175 | return "\x01".pack('nN', $this->channel, strlen($data)).$data."\xCE"; |
||
176 | } |
||
177 | } |
||
178 |