Conditions | 26 |
Paths | 487 |
Total Lines | 126 |
Lines | 21 |
Ratio | 16.67 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
98 | public function onUdpPacket($pct) |
||
99 | { |
||
100 | if (mb_orig_strlen($pct) < 10) { |
||
101 | return; |
||
102 | } |
||
103 | $orig = $pct; |
||
104 | $this->response = []; |
||
105 | Binary::getWord($pct); // ID |
||
106 | $bitmap = Binary::getBitmap(Binary::getByte($pct)) . Binary::getBitmap(Binary::getByte($pct)); |
||
107 | //$qr = (int) $bitmap[0]; |
||
108 | $opcode = bindec(substr($bitmap, 1, 4)); |
||
109 | //$aa = (int) $bitmap[5]; |
||
110 | //$tc = (int) $bitmap[6]; |
||
111 | //$rd = (int) $bitmap[7]; |
||
112 | //$ra = (int) $bitmap[8]; |
||
113 | //$z = bindec(substr($bitmap, 9, 3)); |
||
114 | $rcode = bindec(mb_orig_substr($bitmap, 12)); |
||
115 | $this->response['status'] = [ |
||
116 | 'rcode' => $rcode, |
||
117 | 'msg' => $this->getMessageByRcode($rcode) |
||
118 | ]; |
||
119 | $qdcount = Binary::getWord($pct); |
||
120 | $ancount = Binary::getWord($pct); |
||
121 | $nscount = Binary::getWord($pct); |
||
122 | $arcount = Binary::getWord($pct); |
||
123 | for ($i = 0; $i < $qdcount; ++$i) { |
||
124 | $name = Binary::parseLabels($pct, $orig); |
||
125 | $typeInt = Binary::getWord($pct); |
||
126 | $type = isset(Pool::$type[$typeInt]) ? Pool::$type[$typeInt] : 'UNK(' . $typeInt . ')'; |
||
127 | $classInt = Binary::getWord($pct); |
||
128 | $class = isset(Pool::$class[$classInt]) ? Pool::$class[$classInt] : 'UNK(' . $classInt . ')'; |
||
129 | if (!isset($this->response[$type])) { |
||
130 | $this->response[$type] = []; |
||
131 | } |
||
132 | $record = [ |
||
133 | 'name' => $name, |
||
134 | 'type' => $type, |
||
135 | 'class' => $class, |
||
136 | ]; |
||
137 | $this->response['query'][] = $record; |
||
138 | } |
||
139 | $getResRecord = function (&$pct) use ($orig) { |
||
140 | $name = Binary::parseLabels($pct, $orig); |
||
141 | $typeInt = Binary::getWord($pct); |
||
142 | $type = isset(Pool::$type[$typeInt]) ? Pool::$type[$typeInt] : 'UNK(' . $typeInt . ')'; |
||
143 | $classInt = Binary::getWord($pct); |
||
144 | $class = isset(Pool::$class[$classInt]) ? Pool::$class[$classInt] : 'UNK(' . $classInt . ')'; |
||
145 | $ttl = Binary::getDWord($pct); |
||
146 | $length = Binary::getWord($pct); |
||
147 | $data = mb_orig_substr($pct, 0, $length); |
||
148 | $pct = mb_orig_substr($pct, $length); |
||
149 | |||
150 | $record = [ |
||
151 | 'name' => $name, |
||
152 | 'type' => $type, |
||
153 | 'class' => $class, |
||
154 | 'ttl' => $ttl, |
||
155 | ]; |
||
156 | |||
157 | if (($type === 'A') || ($type === 'AAAA')) { |
||
158 | if ($data === "\x00") { |
||
159 | $record['ip'] = false; |
||
160 | $record['ttl'] = 5; |
||
161 | } else { |
||
162 | $record['ip'] = inet_ntop($data); |
||
163 | } |
||
164 | } elseif ($type === 'NS') { |
||
165 | $record['ns'] = Binary::parseLabels($data, $orig); |
||
166 | } elseif ($type === 'CNAME') { |
||
167 | $record['cname'] = Binary::parseLabels($data, $orig); |
||
168 | } elseif ($type === 'SOA') { |
||
169 | $record['mname'] = Binary::parseLabels($data, $orig); |
||
170 | $record['rname'] = Binary::parseLabels($data, $orig); |
||
171 | $record['serial'] = Binary::getDWord($data); |
||
172 | $record['refresh'] = Binary::getDWord($data); |
||
173 | $record['retry'] = Binary::getDWord($data); |
||
174 | $record['expire'] = Binary::getDWord($data); |
||
175 | $record['nx'] = Binary::getDWord($data); |
||
176 | } elseif ($type === 'MX') { |
||
177 | $record['preference'] = Binary::getWord($data); |
||
178 | $record['exchange'] = Binary::parseLabels($data, $orig); |
||
179 | } elseif ($type === 'TXT') { |
||
180 | $record['text'] = ''; |
||
181 | $lastLength = -1; |
||
182 | while (($length = mb_orig_strlen($data)) > 0 && ($length !== $lastLength)) { |
||
183 | $record['text'] .= Binary::parseLabels($data, $orig); |
||
184 | $lastLength = $length; |
||
185 | } |
||
186 | } elseif ($type === 'SRV') { |
||
187 | $record['priority'] = Binary::getWord($data); |
||
188 | $record['weight'] = Binary::getWord($data); |
||
189 | $record['port'] = Binary::getWord($data); |
||
190 | $record['target'] = Binary::parseLabels($data, $orig); |
||
191 | } |
||
192 | |||
193 | return $record; |
||
194 | }; |
||
195 | View Code Duplication | for ($i = 0; $i < $ancount; ++$i) { |
|
196 | $record = $getResRecord($pct); |
||
197 | if (!isset($this->response[$record['type']])) { |
||
198 | $this->response[$record['type']] = []; |
||
199 | } |
||
200 | $this->response[$record['type']][] = $record; |
||
201 | } |
||
202 | View Code Duplication | for ($i = 0; $i < $nscount; ++$i) { |
|
203 | $record = $getResRecord($pct); |
||
204 | if (!isset($this->response[$record['type']])) { |
||
205 | $this->response[$record['type']] = []; |
||
206 | } |
||
207 | $this->response[$record['type']][] = $record; |
||
208 | } |
||
209 | View Code Duplication | for ($i = 0; $i < $arcount; ++$i) { |
|
210 | $record = $getResRecord($pct); |
||
211 | if (!isset($this->response[$record['type']])) { |
||
212 | $this->response[$record['type']] = []; |
||
213 | } |
||
214 | $this->response[$record['type']][] = $record; |
||
215 | } |
||
216 | $this->onResponse->executeOne($this->response); |
||
217 | if (!$this->keepalive) { |
||
218 | $this->finish(); |
||
219 | return; |
||
220 | } else { |
||
221 | $this->checkFree(); |
||
222 | } |
||
223 | } |
||
224 | |||
303 |