Conditions | 21 |
Paths | 128 |
Total Lines | 112 |
Lines | 9 |
Ratio | 8.04 % |
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 |
||
32 | function query( ...$args ) { |
||
33 | $method = array_shift($args); |
||
34 | $request = new IXR_Request($method, $args); |
||
35 | $length = $request->getLength(); |
||
36 | $xml = $request->getXml(); |
||
37 | $r = "\r\n"; |
||
38 | $request = "POST {$this->path} HTTP/1.0$r"; |
||
39 | |||
40 | $this->headers['Host'] = preg_replace( '#^ssl://#', '', $this->server ); |
||
41 | $this->headers['Content-Type'] = 'text/xml'; |
||
42 | $this->headers['User-Agent'] = $this->useragent; |
||
43 | $this->headers['Content-Length'] = $length; |
||
44 | |||
45 | $sslverify = true; |
||
46 | if ( defined( 'VAULTPRESS_NO_SSL' ) && VAULTPRESS_NO_SSL ) { |
||
47 | $sslverify = false; |
||
48 | } |
||
49 | if ( class_exists( 'WP_Http' ) ) { |
||
50 | $args = array( |
||
51 | 'method' => 'POST', |
||
52 | 'body' => $xml, |
||
53 | 'headers' => $this->headers, |
||
54 | 'sslverify' => $sslverify, |
||
55 | ); |
||
56 | if ( $this->timeout ) |
||
57 | $args['timeout'] = $this->timeout; |
||
58 | |||
59 | $http = new WP_Http(); |
||
60 | if ( $this->ssl ) |
||
61 | $url = sprintf( 'https://%s%s', $this->server, $this->path ); |
||
62 | else |
||
63 | $url = sprintf( 'http://%s%s', $this->server, $this->path ); |
||
64 | |||
65 | $result = $http->request( $url, $args ); |
||
66 | if ( is_wp_error( $result ) ) { |
||
67 | foreach( $result->errors as $type => $messages ) { |
||
68 | $this->error = new IXR_Error( |
||
69 | -32702, |
||
70 | sprintf( 'WP_Http error: %s, %s', $type, $messages[0] ) |
||
71 | ); |
||
72 | break; |
||
73 | } |
||
74 | return false; |
||
75 | } else if ( $result['response']['code'] > 299 || $result['response']['code'] < 200 ) { |
||
76 | $this->error = new IXR_Error( |
||
77 | -32701, |
||
78 | sprintf( 'Server rejected request (HTTP response: %s %s)', $result['response']['code'], $result['response']['message']) |
||
79 | ); |
||
80 | return false; |
||
81 | } |
||
82 | // Now parse what we've got back |
||
83 | $this->message = new IXR_Message( $result['body'] ); |
||
84 | } else { |
||
85 | foreach( $this->headers as $header => $value ) { |
||
86 | $request .= "{$header}: {$value}{$r}"; |
||
87 | } |
||
88 | $request .= $r; |
||
89 | |||
90 | $request .= $xml; |
||
91 | // Now send the request |
||
92 | if ( $this->ssl ) |
||
93 | $host = 'ssl://'.$this->server; |
||
94 | else |
||
95 | $host = $this->server; |
||
96 | if ($this->timeout) { |
||
97 | $fp = @fsockopen( $host, $this->port, $errno, $errstr, $this->timeout ); |
||
98 | } else { |
||
99 | $fp = @fsockopen( $host, $this->port, $errno, $errstr ); |
||
100 | } |
||
101 | if (!$fp) { |
||
102 | $this->error = new IXR_Error( -32300, "Transport error - could not open socket: $errno $errstr" ); |
||
103 | return false; |
||
104 | } |
||
105 | fputs( $fp, $request ); |
||
106 | |||
107 | $contents = ''; |
||
108 | $gotFirstLine = false; |
||
109 | $gettingHeaders = true; |
||
110 | |||
111 | while ( !feof($fp) ) { |
||
112 | $line = fgets( $fp, 4096 ); |
||
113 | if ( !$gotFirstLine ) { |
||
114 | // Check line for '200' |
||
115 | if ( strstr($line, '200') === false ) { |
||
116 | $this->error = new IXR_Error( -32301, 'transport error - HTTP status code was not 200' ); |
||
117 | return false; |
||
118 | } |
||
119 | $gotFirstLine = true; |
||
120 | } |
||
121 | if ( trim($line) == '' ) { |
||
122 | $gettingHeaders = false; |
||
123 | } |
||
124 | if ( !$gettingHeaders ) { |
||
125 | $contents .= trim( $line ); |
||
126 | } |
||
127 | } |
||
128 | // Now parse what we've got back |
||
129 | $this->message = new IXR_Message( $contents ); |
||
130 | } |
||
131 | View Code Duplication | if ( !$this->message->parse() ) { |
|
132 | // XML error |
||
133 | $this->error = new IXR_Error( -32700, 'parse error. not well formed' ); |
||
134 | return false; |
||
135 | } |
||
136 | // Is the message a fault? |
||
137 | View Code Duplication | if ( $this->message->messageType == 'fault' ) { |
|
138 | $this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString ); |
||
139 | return false; |
||
140 | } |
||
141 | // Message must be OK |
||
142 | return true; |
||
143 | } |
||
144 | } |
||
145 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.