Conditions | 13 |
Paths | 54 |
Total Lines | 86 |
Code Lines | 48 |
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 |
||
168 | private static function HTCPPurge( array $urlArr ) { |
||
169 | global $wgHTCPRouting, $wgHTCPMulticastTTL; |
||
170 | |||
171 | // HTCP CLR operation |
||
172 | $htcpOpCLR = 4; |
||
173 | |||
174 | // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h) |
||
175 | if ( !defined( "IPPROTO_IP" ) ) { |
||
176 | define( "IPPROTO_IP", 0 ); |
||
177 | define( "IP_MULTICAST_LOOP", 34 ); |
||
178 | define( "IP_MULTICAST_TTL", 33 ); |
||
179 | } |
||
180 | |||
181 | // pfsockopen doesn't work because we need set_sock_opt |
||
182 | $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ); |
||
183 | if ( !$conn ) { |
||
184 | $errstr = socket_strerror( socket_last_error() ); |
||
185 | wfDebugLog( 'squid', __METHOD__ . |
||
186 | ": Error opening UDP socket: $errstr" ); |
||
187 | |||
188 | return; |
||
189 | } |
||
190 | |||
191 | // Set socket options |
||
192 | socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 ); |
||
193 | if ( $wgHTCPMulticastTTL != 1 ) { |
||
194 | // Set multicast time to live (hop count) option on socket |
||
195 | socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL, |
||
196 | $wgHTCPMulticastTTL ); |
||
197 | } |
||
198 | |||
199 | // Get sequential trx IDs for packet loss counting |
||
200 | $ids = UIDGenerator::newSequentialPerNodeIDs( |
||
201 | 'squidhtcppurge', 32, count( $urlArr ), UIDGenerator::QUICK_VOLATILE |
||
202 | ); |
||
203 | |||
204 | foreach ( $urlArr as $url ) { |
||
205 | if ( !is_string( $url ) ) { |
||
206 | throw new MWException( 'Bad purge URL' ); |
||
207 | } |
||
208 | $url = self::expand( $url ); |
||
209 | $conf = self::getRuleForURL( $url, $wgHTCPRouting ); |
||
|
|||
210 | if ( !$conf ) { |
||
211 | wfDebugLog( 'squid', __METHOD__ . |
||
212 | "No HTCP rule configured for URL {$url} , skipping" ); |
||
213 | continue; |
||
214 | } |
||
215 | |||
216 | if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) { |
||
217 | // Normalize single entries |
||
218 | $conf = [ $conf ]; |
||
219 | } |
||
220 | foreach ( $conf as $subconf ) { |
||
221 | if ( !isset( $subconf['host'] ) || !isset( $subconf['port'] ) ) { |
||
222 | throw new MWException( "Invalid HTCP rule for URL $url\n" ); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // Construct a minimal HTCP request diagram |
||
227 | // as per RFC 2756 |
||
228 | // Opcode 'CLR', no response desired, no auth |
||
229 | $htcpTransID = current( $ids ); |
||
230 | next( $ids ); |
||
231 | |||
232 | $htcpSpecifier = pack( 'na4na*na8n', |
||
233 | 4, 'HEAD', strlen( $url ), $url, |
||
234 | 8, 'HTTP/1.0', 0 ); |
||
235 | |||
236 | $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier ); |
||
237 | $htcpLen = 4 + $htcpDataLen + 2; |
||
238 | |||
239 | // Note! Squid gets the bit order of the first |
||
240 | // word wrong, wrt the RFC. Apparently no other |
||
241 | // implementation exists, so adapt to Squid |
||
242 | $htcpPacket = pack( 'nxxnCxNxxa*n', |
||
243 | $htcpLen, $htcpDataLen, $htcpOpCLR, |
||
244 | $htcpTransID, $htcpSpecifier, 2 ); |
||
245 | |||
246 | wfDebugLog( 'squid', __METHOD__ . |
||
247 | "Purging URL $url via HTCP" ); |
||
248 | foreach ( $conf as $subconf ) { |
||
249 | socket_sendto( $conn, $htcpPacket, $htcpLen, 0, |
||
250 | $subconf['host'], $subconf['port'] ); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
296 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.