Conditions | 13 |
Paths | 22 |
Total Lines | 69 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
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 |
||
116 | public static function gethostbyname_timeout( $domain, $dns, $timeout = 10 ) { |
||
117 | // based off of http://www.php.net/manual/en/function.gethostbyaddr.php#46869 |
||
118 | // @ http://www.askapache.com/pub/php/gethostbyaddr.php |
||
119 | // @ http://www.askapache.com/php/php-fsockopen-dns-udp.html |
||
120 | |||
121 | $data = pack('n6', rand(10, 77), 0x0100, 1, 0, 0, 0); |
||
122 | foreach (explode('.', $domain) as $bit) { |
||
123 | $l = strlen($bit); |
||
124 | $data .= chr($l) . $bit; |
||
125 | } |
||
126 | $data .= pack('n2', 1, 1); // QTYPE=A, QCLASS=IN |
||
127 | |||
128 | $errno = $errstr = 0; |
||
129 | $fp = fsockopen( 'udp://' . $dns, 53, $errno, $errstr, $timeout ); |
||
130 | if (!$fp || !is_resource($fp)) return $errno; |
||
131 | |||
132 | socket_set_timeout( $fp, $timeout ); |
||
133 | $requestsize = fwrite( $fp, $data ); |
||
134 | |||
135 | $max_rx = $requestsize * 3; |
||
136 | $start = time(); |
||
137 | $response_data = ''; |
||
138 | $responsesize = 0; |
||
139 | while ( $received < $max_rx && ( ( time() - $start ) < $timeout ) && ( $buf = fread( $fp, 1 ) ) !== false ) { |
||
|
|||
140 | $responsesize++; |
||
141 | $response_data .= $buf; |
||
142 | } |
||
143 | $info = stream_get_meta_data( $fp ); |
||
144 | fclose( $fp ); |
||
145 | |||
146 | if ( $info[ 'timed_out' ] ) { |
||
147 | echo 'Connection timed out!'; |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | if ( ( time() - $start ) > $timeout ) { |
||
152 | echo 'Response timed out!'; |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | // read answer header |
||
157 | $ans_header = unpack( "nid/nspec/nqdcount/nancount/nnscount/narcount", substr( $response_data, 0, 12 ) ); |
||
158 | |||
159 | if ( ! $ans_header['ancount'] ) { |
||
160 | echo 'No header records!'; |
||
161 | return false; // no answers! |
||
162 | } |
||
163 | |||
164 | // skip question part |
||
165 | $offset = strlen( $domain ) + 4 + 2 + 1; // 4 => QTYPE + QCLASS, 2 => len, 1 => null terminator |
||
166 | |||
167 | // loop and gather our A-records |
||
168 | $loops = 0; |
||
169 | $addresses = array(); |
||
170 | |||
171 | do { |
||
172 | $record_header = unpack("ntype/nclass/Nttl/nlength/C4addr", substr( $response_data, 12 + $offset, 15 ) ); |
||
173 | $offset += $record_header['length'] + 12; // 4 => QTYPE + QCLASS, 4 = TTL, 2 = length |
||
174 | |||
175 | if ( 1 != $record_header['class'] ) { |
||
176 | continue; // for some reason, it wasn't an A record |
||
177 | } |
||
178 | |||
179 | $addresses[] = $record_header['addr1'] . '.' . $record_header['addr2'] . '.' . $record_header['addr3'] . '.' . $record_header['addr4']; |
||
180 | $loops++; |
||
181 | } while ( $record_header['length'] != 0 && $loops < 20 ); |
||
182 | |||
183 | return $addresses; |
||
184 | } |
||
185 | |||
210 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.