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