Conditions | 49 |
Paths | > 20000 |
Total Lines | 209 |
Code Lines | 149 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
144 | private function invoke($method, $args = array(), $holder, $location = NULL) |
||
145 | { |
||
146 | $api = API::$API[$method]; |
||
147 | if (!$api) { |
||
148 | throw new Ks3ClientException($method . " Not Found API"); |
||
149 | } |
||
150 | if (count($args) !== 0) { |
||
151 | if (count($args) > 1 || !is_array($args[0])) { |
||
152 | throw new Ks3ClientException("this method only needs one array argument"); |
||
153 | } |
||
154 | $args = $args[0]; |
||
155 | } |
||
156 | if (isset($api["redirect"])) { |
||
157 | $api = API::$API[$api["redirect"]]; |
||
158 | } |
||
159 | $request = new Ks3Request(); |
||
160 | if (empty($args["Bucket"])) { |
||
161 | if ($api["needBucket"]) { |
||
162 | throw new Ks3ClientException($method . " this api need bucket"); |
||
163 | } |
||
164 | } else { |
||
165 | $request->bucket = $args["Bucket"]; |
||
166 | } |
||
167 | $position = $api["objectPostion"] ?? "Key"; |
||
168 | if (empty($args[$position])) { |
||
169 | if ($api["needObject"]) { |
||
170 | throw new Ks3ClientException($method . " this api need " . $position); |
||
171 | } |
||
172 | } else { |
||
173 | $key = $args[$position]; |
||
174 | $preEncoding = mb_detect_encoding($key, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); |
||
175 | $holder->msg .= "key encoding " . $preEncoding . "\r\n"; |
||
176 | if (strtolower($preEncoding) !== "utf-8") { |
||
177 | $key = iconv($preEncoding, "UTF-8", $key); |
||
178 | } |
||
179 | $request->key = $key; |
||
180 | } |
||
181 | $method = $api["method"]; |
||
182 | if ($method === "Method") { |
||
183 | if (empty($args["Method"])) { |
||
184 | $request->method = "GET"; |
||
185 | } else { |
||
186 | $request->method = $args["Method"]; |
||
187 | } |
||
188 | } else { |
||
189 | $request->method = $api["method"]; |
||
190 | } |
||
191 | if (KS3_API_USE_HTTPS) { |
||
192 | $request->scheme = "https://"; |
||
193 | } else { |
||
194 | $request->scheme = "http://"; |
||
195 | } |
||
196 | $request->endpoint = $this->endpoint; |
||
197 | //add subresource |
||
198 | if (!empty($api["subResource"])) { |
||
199 | $request->subResource = $api["subResource"]; |
||
200 | } |
||
201 | //add query params |
||
202 | if (isset($api["queryParams"])) { |
||
203 | foreach ($api["queryParams"] as $key => $value) { |
||
204 | $required = FALSE; |
||
205 | if (strpos($value, "!") === 0) { |
||
206 | $required = TRUE; |
||
207 | $value = substr($value, 1); |
||
208 | } |
||
209 | $index = explode("->", $value); |
||
210 | $curIndexArg = $args; |
||
211 | $add = TRUE; |
||
212 | $curkey = ""; |
||
213 | foreach ($index as $key1 => $value1) { |
||
214 | if (!isset($curIndexArg[$value1]) && $value1 !== "*") { |
||
215 | $add = FALSE; |
||
216 | } else { |
||
217 | $curkey = $value1; |
||
218 | //星号表示所有,按照暂时的业务,默认星号后面就没了 |
||
219 | if ($curkey === "*") { |
||
220 | foreach ($curIndexArg as $queryK => $queryV) { |
||
221 | if (!is_array($queryV)) { |
||
222 | $request->addQueryParams($queryK, $queryV); |
||
223 | } |
||
224 | } |
||
225 | $add = FALSE; |
||
226 | $required = FALSE; |
||
227 | break; |
||
228 | } |
||
229 | |||
230 | $curIndexArg = $curIndexArg[$value1]; |
||
231 | } |
||
232 | } |
||
233 | if (!empty($curIndexArg) && $add) { |
||
234 | $request->addQueryParams($curkey, $curIndexArg); |
||
235 | continue; |
||
236 | } |
||
237 | if ($required) { |
||
238 | throw new Ks3ClientException($method . " param " . $value . " is required"); |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | if (isset($api["body"])) { |
||
243 | if (isset($api["body"]["builder"])) { |
||
244 | $builderName = $api["body"]["builder"]; |
||
245 | $builder = new $builderName(); |
||
246 | $request->body = $builder->build($args); |
||
247 | } else if (isset($api["body"]["position"])) { |
||
248 | $position = $api["body"]["position"]; |
||
249 | $index = explode("->", $position); |
||
250 | $curIndexArg = $args; |
||
251 | $add = TRUE; |
||
252 | $curkey = ""; |
||
253 | foreach ($index as $key1 => $value1) { |
||
254 | if (!isset($curIndexArg[$value1])) { |
||
255 | $add = FALSE; |
||
256 | } else { |
||
257 | $curIndexArg = $curIndexArg[$value1]; |
||
258 | $curkey = $value1; |
||
259 | } |
||
260 | } |
||
261 | if (!empty($curIndexArg) && $add) { |
||
262 | $request->body = $curIndexArg; |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | |||
267 | //add ext headers |
||
268 | //TODO |
||
269 | //sign request |
||
270 | $signer = NULL; |
||
271 | if (isset($api["signer"])) { |
||
272 | $signers = explode("->", $api["signer"]); |
||
273 | foreach ($signers as $key => $value) { |
||
274 | $signer = new $value(); |
||
275 | $log = $signer->sign($request, array("accessKey" => $this->accessKey, "secretKey" => $this->secretKey, "args" => $args)); |
||
276 | if (!empty($log)) { |
||
277 | $holder->msg .= $log . "\r\n"; |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | if ($signer === NULL || !($signer instanceof QueryAuthSigner)) { |
||
283 | $url = $request->toUrl($this->endpoint); |
||
284 | if ($location != NULL) { |
||
285 | $url = $location; |
||
286 | } |
||
287 | $httpRequest = new RequestCore($url); |
||
288 | if (KS3_API_DEBUG_MODE === TRUE) { |
||
289 | $httpRequest->debug_mode = TRUE; |
||
290 | } |
||
291 | $httpRequest->set_method($request->method); |
||
292 | foreach ($request->headers as $key => $value) { |
||
293 | $httpRequest->add_header($key, $value); |
||
294 | } |
||
295 | $httpRequest->request_body = $request->body; |
||
296 | |||
297 | if (isset($args["writeCallBack"])) { |
||
298 | $httpRequest->register_streaming_write_callback($args["writeCallBack"]); |
||
299 | } |
||
300 | if (isset($args["readCallBack"])) { |
||
301 | $httpRequest->register_streaming_read_callback($args["readCallBack"]); |
||
302 | } |
||
303 | |||
304 | $read_stream = $request->read_stream; |
||
305 | $read_length = $request->getHeader(Headers::$ContentLength); |
||
306 | $seek_position = $request->seek_position; |
||
307 | if (isset($read_stream)) { |
||
308 | $httpRequest->set_read_stream($read_stream, $read_length); |
||
309 | $httpRequest->set_seek_position($seek_position); |
||
310 | $httpRequest->remove_header(Headers::$ContentLength); |
||
311 | } |
||
312 | $write_stream = $request->write_stream; |
||
313 | if (isset($write_stream)) { |
||
314 | $httpRequest->set_write_stream($write_stream); |
||
315 | } |
||
316 | |||
317 | $holder->msg .= "request url->" . serialize($httpRequest->request_url) . "\r\n"; |
||
318 | $holder->msg .= "request headers->" . serialize($httpRequest->request_headers) . "\r\n"; |
||
319 | $holder->msg .= "request body->" . $httpRequest->request_body . "\r\n"; |
||
320 | $holder->msg .= "request read stream length->" . $read_length . "\r\n"; |
||
321 | $holder->msg .= "request read stream seek position->" . $seek_position . "\r\n"; |
||
322 | $httpRequest->send_request(); |
||
323 | //print_r($httpRequest); |
||
324 | $body = $httpRequest->get_response_body(); |
||
325 | $data = new ResponseCore ($httpRequest->get_response_header(), Utils::replaceNS2($body), $httpRequest->get_response_code()); |
||
326 | |||
327 | if ($data->status == 307) { |
||
328 | $respHeaders = $httpRequest->get_response_header(); |
||
329 | $location = $respHeaders["location"]; |
||
330 | if (strpos($location, "http") === 0) { |
||
331 | $holder->msg .= "response code->" . $httpRequest->get_response_code() . "\r\n"; |
||
332 | $holder->msg .= "response headers->" . serialize($httpRequest->get_response_header()) . "\r\n"; |
||
333 | $holder->msg .= "response body->" . $body . "\r\n"; |
||
334 | $holder->msg .= "retry request to " . $location . "\r\n"; |
||
335 | //array($args)详见invoke开头 |
||
336 | return $this->invoke($method, array($args), $holder, $location); |
||
337 | } |
||
338 | } |
||
339 | $holder->msg .= "response code->" . $httpRequest->get_response_code() . "\r\n"; |
||
340 | $holder->msg .= "response headers->" . serialize($httpRequest->get_response_header()) . "\r\n"; |
||
341 | $holder->msg .= "response body->" . $body . "\r\n"; |
||
342 | $handlers = explode("->", $api["handler"]); |
||
343 | foreach ($handlers as $key => $value) { |
||
344 | $handler = new $value(); |
||
345 | $data = $handler->handle($data); |
||
346 | } |
||
347 | return $data; |
||
348 | } |
||
349 | |||
350 | $url = $request->toUrl($this->endpoint); |
||
351 | $holder->msg .= $url . "\r\n"; |
||
352 | return $url; |
||
353 | } |
||
385 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.