Conditions | 26 |
Paths | 487 |
Total Lines | 127 |
Code Lines | 97 |
Lines | 21 |
Ratio | 16.54 % |
Changes | 5 | ||
Bugs | 4 | Features | 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 |
||
87 | public function onUdpPacket($pct) |
||
88 | { |
||
89 | if(mb_orig_strlen($pct) < 10){ |
||
90 | return; |
||
91 | } |
||
92 | $orig = $pct; |
||
93 | $this->response = []; |
||
94 | /*$id = */ |
||
95 | Binary::getWord($pct); |
||
96 | $bitmap = Binary::getBitmap(Binary::getByte($pct)) . Binary::getBitmap(Binary::getByte($pct)); |
||
97 | //$qr = (int) $bitmap[0]; |
||
98 | $opcode = bindec(substr($bitmap, 1, 4)); |
||
99 | //$aa = (int) $bitmap[5]; |
||
100 | //$tc = (int) $bitmap[6]; |
||
101 | //$rd = (int) $bitmap[7]; |
||
102 | //$ra = (int) $bitmap[8]; |
||
103 | //$z = bindec(substr($bitmap, 9, 3)); |
||
104 | $rcode = bindec(mb_orig_substr($bitmap, 12)); |
||
105 | $this->response['status'] = [ |
||
106 | 'rcode' => $rcode, |
||
107 | 'msg' => $this->getMessageByRcode($rcode) |
||
108 | ]; |
||
109 | $qdcount = Binary::getWord($pct); |
||
110 | $ancount = Binary::getWord($pct); |
||
111 | $nscount = Binary::getWord($pct); |
||
112 | $arcount = Binary::getWord($pct); |
||
113 | for ($i = 0; $i < $qdcount; ++$i) { |
||
114 | $name = Binary::parseLabels($pct, $orig); |
||
115 | $typeInt = Binary::getWord($pct); |
||
116 | $type = isset(Pool::$type[$typeInt]) ? Pool::$type[$typeInt] : 'UNK(' . $typeInt . ')'; |
||
117 | $classInt = Binary::getWord($pct); |
||
118 | $class = isset(Pool::$class[$classInt]) ? Pool::$class[$classInt] : 'UNK(' . $classInt . ')'; |
||
119 | if (!isset($this->response[$type])) { |
||
120 | $this->response[$type] = []; |
||
121 | } |
||
122 | $record = [ |
||
123 | 'name' => $name, |
||
124 | 'type' => $type, |
||
125 | 'class' => $class, |
||
126 | ]; |
||
127 | $this->response['query'][] = $record; |
||
128 | } |
||
129 | $getResRecord = function (&$pct) use ($orig) { |
||
130 | $name = Binary::parseLabels($pct, $orig); |
||
131 | $typeInt = Binary::getWord($pct); |
||
132 | $type = isset(Pool::$type[$typeInt]) ? Pool::$type[$typeInt] : 'UNK(' . $typeInt . ')'; |
||
133 | $classInt = Binary::getWord($pct); |
||
134 | $class = isset(Pool::$class[$classInt]) ? Pool::$class[$classInt] : 'UNK(' . $classInt . ')'; |
||
135 | $ttl = Binary::getDWord($pct); |
||
136 | $length = Binary::getWord($pct); |
||
137 | $data = mb_orig_substr($pct, 0, $length); |
||
138 | $pct = mb_orig_substr($pct, $length); |
||
139 | |||
140 | $record = [ |
||
141 | 'name' => $name, |
||
142 | 'type' => $type, |
||
143 | 'class' => $class, |
||
144 | 'ttl' => $ttl, |
||
145 | ]; |
||
146 | |||
147 | if (($type === 'A') || ($type === 'AAAA')) { |
||
148 | if ($data === "\x00") { |
||
149 | $record['ip'] = false; |
||
150 | $record['ttl'] = 5; |
||
151 | } else { |
||
152 | $record['ip'] = inet_ntop($data); |
||
153 | } |
||
154 | }elseif ($type === 'NS') { |
||
155 | $record['ns'] = Binary::parseLabels($data, $orig); |
||
156 | }elseif ($type === 'CNAME') { |
||
157 | $record['cname'] = Binary::parseLabels($data, $orig); |
||
158 | } |
||
159 | elseif ($type === 'SOA') { |
||
160 | $record['mname'] = Binary::parseLabels($data, $orig); |
||
161 | $record['rname'] = Binary::parseLabels($data, $orig); |
||
162 | $record['serial'] = Binary::getDWord($data); |
||
163 | $record['refresh'] = Binary::getDWord($data); |
||
164 | $record['retry'] = Binary::getDWord($data); |
||
165 | $record['expire'] = Binary::getDWord($data); |
||
166 | $record['nx'] = Binary::getDWord($data); |
||
167 | }elseif ($type === 'MX') { |
||
168 | $record['preference'] = Binary::getWord($data); |
||
169 | $record['exchange'] = Binary::parseLabels($data, $orig); |
||
170 | } elseif ($type === 'TXT') { |
||
171 | $txt = []; |
||
172 | while(mb_orig_strlen($data) > 0 && count($txt) < 1000) { |
||
173 | $txt[] = Binary::parseLabels($data, $orig); |
||
174 | } |
||
175 | $record['text'] = implode('', $txt); |
||
176 | } elseif ($type === 'SRV') { |
||
177 | $record['priority'] = Binary::getWord($data); |
||
178 | $record['weight'] = Binary::getWord($data); |
||
179 | $record['port'] = Binary::getWord($data); |
||
180 | $record['target'] = Binary::parseLabels($data, $orig); |
||
181 | } |
||
182 | |||
183 | return $record; |
||
184 | }; |
||
185 | View Code Duplication | for ($i = 0; $i < $ancount; ++$i) { |
|
186 | $record = $getResRecord($pct); |
||
187 | if (!isset($this->response[$record['type']])) { |
||
188 | $this->response[$record['type']] = []; |
||
189 | } |
||
190 | $this->response[$record['type']][] = $record; |
||
191 | } |
||
192 | View Code Duplication | for ($i = 0; $i < $nscount; ++$i) { |
|
193 | $record = $getResRecord($pct); |
||
194 | if (!isset($this->response[$record['type']])) { |
||
195 | $this->response[$record['type']] = []; |
||
196 | } |
||
197 | $this->response[$record['type']][] = $record; |
||
198 | } |
||
199 | View Code Duplication | for ($i = 0; $i < $arcount; ++$i) { |
|
200 | $record = $getResRecord($pct); |
||
201 | if (!isset($this->response[$record['type']])) { |
||
202 | $this->response[$record['type']] = []; |
||
203 | } |
||
204 | $this->response[$record['type']][] = $record; |
||
205 | } |
||
206 | $this->onResponse->executeOne($this->response); |
||
207 | if (!$this->keepalive) { |
||
208 | $this->finish(); |
||
209 | return; |
||
210 | } else { |
||
211 | $this->checkFree(); |
||
212 | } |
||
213 | } |
||
214 | |||
306 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.