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