Conditions | 40 |
Paths | 1365 |
Total Lines | 146 |
Lines | 11 |
Ratio | 7.53 % |
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 |
||
284 | public function onRead() |
||
285 | { |
||
286 | if ($this->state === self::STATE_BODY) { |
||
287 | goto body; |
||
288 | } |
||
289 | if ($this->reqType === null) { |
||
290 | if ($this->requests->isEmpty()) { |
||
291 | $this->finish(); |
||
292 | return; |
||
293 | } |
||
294 | $this->reqType = $this->requests->shift(); |
||
295 | } |
||
296 | while (($line = $this->readLine()) !== null) { |
||
297 | if ($line !== '') { |
||
298 | if ($this->rawHeaders !== null) { |
||
299 | $this->rawHeaders[] = $line; |
||
300 | } |
||
301 | } else { |
||
302 | if (isset($this->headers['HTTP_CONTENT_LENGTH'])) { |
||
303 | $this->contentLength = (int)$this->headers['HTTP_CONTENT_LENGTH']; |
||
304 | } else { |
||
305 | $this->contentLength = -1; |
||
306 | } |
||
307 | View Code Duplication | if (isset($this->headers['HTTP_TRANSFER_ENCODING'])) { |
|
308 | $e = explode(', ', strtolower($this->headers['HTTP_TRANSFER_ENCODING'])); |
||
309 | $this->chunked = in_array('chunked', $e, true); |
||
310 | } else { |
||
311 | $this->chunked = false; |
||
312 | } |
||
313 | View Code Duplication | if (isset($this->headers['HTTP_CONNECTION'])) { |
|
314 | $e = explode(', ', strtolower($this->headers['HTTP_CONNECTION'])); |
||
315 | $this->keepalive = in_array('keep-alive', $e, true); |
||
316 | } |
||
317 | if (isset($this->headers['HTTP_CONTENT_TYPE'])) { |
||
318 | parse_str('type=' . strtr($this->headers['HTTP_CONTENT_TYPE'], [';' => '&', ' ' => '']), $p); |
||
319 | $this->contentType = $p['type']; |
||
320 | if (isset($p['charset'])) { |
||
321 | $this->charset = strtolower($p['charset']); |
||
322 | } |
||
323 | } |
||
324 | if ($this->contentLength === -1 && !$this->chunked && !$this->keepalive) { |
||
325 | $this->eofTerminated = true; |
||
326 | } |
||
327 | if ($this->reqType === 'HEAD') { |
||
328 | $this->requestFinished(); |
||
329 | } else { |
||
330 | $this->state = self::STATE_BODY; |
||
331 | } |
||
332 | break; |
||
333 | } |
||
334 | if ($this->state === self::STATE_ROOT) { |
||
335 | $this->headers['STATUS'] = $line; |
||
336 | $e = explode(' ', $this->headers['STATUS']); |
||
337 | $this->responseCode = isset($e[1]) ? (int)$e[1] : 0; |
||
338 | $this->state = self::STATE_HEADERS; |
||
339 | } elseif ($this->state === self::STATE_HEADERS) { |
||
340 | $e = explode(': ', $line); |
||
341 | |||
342 | if (isset($e[1])) { |
||
343 | $k = 'HTTP_' . strtoupper(strtr($e[0], Generic::$htr)); |
||
344 | if ($k === 'HTTP_SET_COOKIE') { |
||
345 | parse_str(strtr($e[1], [';' => '&', ' ' => '']), $p); |
||
346 | if (sizeof($p)) { |
||
347 | $this->cookie[$k = key($p)] =& $p; |
||
348 | $p['value'] = $p[$k]; |
||
349 | unset($p[$k], $p); |
||
350 | } |
||
351 | } |
||
352 | if (isset($this->headers[$k])) { |
||
353 | if (is_array($this->headers[$k])) { |
||
354 | $this->headers[$k][] = $e[1]; |
||
355 | } else { |
||
356 | $this->headers[$k] = [$this->headers[$k], $e[1]]; |
||
357 | } |
||
358 | } else { |
||
359 | $this->headers[$k] = $e[1]; |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 | if ($this->state !== self::STATE_BODY) { |
||
365 | return; // not enough data yet |
||
366 | } |
||
367 | body: |
||
368 | if ($this->eofTerminated) { |
||
369 | $body = $this->readUnlimited(); |
||
370 | if ($this->chunkcb) { |
||
371 | $func = $this->chunkcb; |
||
372 | $func($body); |
||
373 | } |
||
374 | $this->body .= $body; |
||
375 | return; |
||
376 | } |
||
377 | if ($this->chunked) { |
||
378 | chunk: |
||
379 | if ($this->curChunkSize === null) { // outside of chunk |
||
380 | $l = $this->readLine(); |
||
381 | if ($l === '') { // skip empty line |
||
382 | goto chunk; |
||
383 | } |
||
384 | if ($l === null) { |
||
385 | return; // not enough data yet |
||
386 | } |
||
387 | if (!ctype_xdigit($l)) { |
||
388 | $this->protocolError = __LINE__; |
||
389 | $this->finish(); // protocol error |
||
390 | return; |
||
391 | } |
||
392 | $this->curChunkSize = hexdec($l); |
||
393 | } |
||
394 | if ($this->curChunkSize !== null) { |
||
395 | if ($this->curChunkSize === 0) { |
||
396 | if ($this->readLine() === '') { |
||
397 | $this->requestFinished(); |
||
398 | return; |
||
399 | } else { // protocol error |
||
400 | $this->protocolError = __LINE__; |
||
401 | $this->finish(); |
||
402 | return; |
||
403 | } |
||
404 | } |
||
405 | $n = $this->curChunkSize - mb_orig_strlen($this->curChunk); |
||
406 | $this->curChunk .= $this->read($n); |
||
407 | if ($this->curChunkSize <= mb_orig_strlen($this->curChunk)) { |
||
408 | if ($this->chunkcb) { |
||
409 | $func = $this->chunkcb; |
||
410 | $func($this->curChunk); |
||
411 | } |
||
412 | $this->body .= $this->curChunk; |
||
413 | $this->curChunkSize = null; |
||
414 | $this->curChunk = ''; |
||
415 | goto chunk; |
||
416 | } |
||
417 | } |
||
418 | } else { |
||
419 | $body = $this->read($this->contentLength - mb_orig_strlen($this->body)); |
||
420 | if ($this->chunkcb) { |
||
421 | $func = $this->chunkcb; |
||
422 | $func($body); |
||
423 | } |
||
424 | $this->body .= $body; |
||
425 | if (($this->contentLength !== -1) && (mb_orig_strlen($this->body) >= $this->contentLength)) { |
||
426 | $this->requestFinished(); |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | |||
476 |
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.