| Conditions | 41 |
| Paths | 0 |
| Total Lines | 139 |
| 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 |
||
| 256 | public function parseMultipart() |
||
| 257 | { |
||
| 258 | start: |
||
| 259 | if ($this->frozen) { |
||
| 260 | return; |
||
| 261 | } |
||
| 262 | if ($this->state === self::STATE_SEEKBOUNDARY) { |
||
| 263 | // seek to the nearest boundary |
||
| 264 | if (($p = $this->search('--' . $this->boundary . "\r\n")) === false) { |
||
| 265 | return; |
||
| 266 | } |
||
| 267 | // we have found the nearest boundary at position $p |
||
| 268 | if ($p > 0) { |
||
| 269 | $extra = $this->read($p); |
||
| 270 | if ($extra !== "\r\n") { |
||
| 271 | $this->log('parseBody(): SEEKBOUNDARY: got unexpected data before boundary (length = ' . $p . '): ' . Debug::exportBytes($extra)); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | $this->drain(mb_orig_strlen($this->boundary) + 4); // drain |
||
| 275 | $this->state = self::STATE_HEADERS; |
||
| 276 | } |
||
| 277 | if ($this->state === self::STATE_HEADERS) { |
||
| 278 | // parse the part's headers |
||
| 279 | $this->curPartDisp = false; |
||
| 280 | $i = 0; |
||
| 281 | do { |
||
| 282 | $l = $this->readline(\EventBuffer::EOL_CRLF); |
||
| 283 | if ($l === null) { |
||
| 284 | return; |
||
| 285 | } |
||
| 286 | if ($l === '') { |
||
| 287 | break; |
||
| 288 | } |
||
| 289 | |||
| 290 | $e = explode(':', $l, 2); |
||
| 291 | $e[0] = strtr(strtoupper($e[0]), Generic::$htr); |
||
| 292 | if (isset($e[1])) { |
||
| 293 | $e[1] = ltrim($e[1]); |
||
| 294 | } |
||
| 295 | if (($e[0] === 'CONTENT_DISPOSITION') && isset($e[1])) { |
||
| 296 | Generic::parseStr($e[1], $this->curPartDisp, true); |
||
| 297 | if (!isset($this->curPartDisp['form-data'])) { |
||
| 298 | break; |
||
| 299 | } |
||
| 300 | if (!isset($this->curPartDisp['name'])) { |
||
| 301 | break; |
||
| 302 | } |
||
| 303 | $this->curPartDisp['name'] = trim($this->curPartDisp['name'], '"'); |
||
| 304 | $name = $this->curPartDisp['name']; |
||
| 305 | if (isset($this->curPartDisp['filename'])) { |
||
| 306 | $this->curPartDisp['filename'] = trim($this->curPartDisp['filename'], '"'); |
||
| 307 | if (!ini_get('file_uploads')) { |
||
| 308 | break; |
||
| 309 | } |
||
| 310 | $this->req->attrs->files[$name] = [ |
||
| 311 | 'name' => $this->curPartDisp['filename'], |
||
| 312 | 'type' => '', |
||
| 313 | 'tmp_name' => null, |
||
| 314 | 'fp' => null, |
||
| 315 | 'error' => UPLOAD_ERR_OK, |
||
| 316 | 'size' => 0, |
||
| 317 | ]; |
||
| 318 | $this->curPart = &$this->req->attrs->files[$name]; |
||
| 319 | $this->req->onUploadFileStart($this); |
||
| 320 | $this->state = self::STATE_UPLOAD; |
||
| 321 | } else { |
||
| 322 | $this->curPart = &$this->req->attrs->post[$name]; |
||
| 323 | $this->curPart = ''; |
||
| 324 | } |
||
| 325 | } elseif (($e[0] === 'CONTENT_TYPE') && isset($e[1])) { |
||
| 326 | if (isset($this->curPartDisp['name']) && isset($this->curPartDisp['filename'])) { |
||
| 327 | $this->curPart['type'] = $e[1]; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } while ($i++ < 10); |
||
| 331 | if ($this->state === self::STATE_HEADERS) { |
||
| 332 | $this->state = self::STATE_BODY; |
||
| 333 | } |
||
| 334 | goto start; |
||
| 335 | } |
||
| 336 | if (($this->state === self::STATE_BODY) || ($this->state === self::STATE_UPLOAD)) { |
||
| 337 | // process the body |
||
| 338 | $chunkEnd1 = $this->search("\r\n--" . $this->boundary . "\r\n"); |
||
| 339 | $chunkEnd2 = $this->search("\r\n--" . $this->boundary . "--\r\n"); |
||
| 340 | if ($chunkEnd1 === false && $chunkEnd2 === false) { |
||
| 341 | /* we have only piece of Part in buffer */ |
||
| 342 | $l = $this->length - mb_orig_strlen($this->boundary) - 8; |
||
| 343 | if ($l <= 0) { |
||
| 344 | return; |
||
| 345 | } |
||
| 346 | if (($this->state === self::STATE_BODY) && isset($this->curPartDisp['name'])) { |
||
| 347 | $this->curPart .= $this->read($l); |
||
| 348 | } elseif (($this->state === self::STATE_UPLOAD) && isset($this->curPartDisp['filename'])) { |
||
| 349 | $this->curPart['size'] += $l; |
||
| 350 | if ($this->req->getUploadMaxSize() < $this->curPart['size']) { |
||
| 351 | $this->curPart['error'] = UPLOAD_ERR_INI_SIZE; |
||
| 352 | $this->req->header('413 Request Entity Too Large'); |
||
| 353 | $this->req->out(''); |
||
| 354 | $this->req->finish(); |
||
| 355 | } elseif ($this->maxFileSize && ($this->maxFileSize < $this->curPart['size'])) { |
||
| 356 | $this->curPart['error'] = UPLOAD_ERR_FORM_SIZE; |
||
| 357 | $this->req->header('413 Request Entity Too Large'); |
||
| 358 | $this->req->out(''); |
||
| 359 | $this->req->finish(); |
||
| 360 | } else { |
||
| 361 | $this->curChunkSize = $l; |
||
| 362 | $this->req->onUploadFileChunk($this); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } else { /* we have entire Part in buffer */ |
||
| 366 | |||
| 367 | if ($chunkEnd1 === false) { |
||
| 368 | $l = $chunkEnd2; |
||
| 369 | $endOfMsg = true; |
||
| 370 | } else { |
||
| 371 | $l = $chunkEnd1; |
||
| 372 | $endOfMsg = false; |
||
| 373 | } |
||
| 374 | |||
| 375 | if (($this->state === self::STATE_BODY) && isset($this->curPartDisp['name'])) { |
||
| 376 | $this->curPart .= $this->read($l); |
||
| 377 | if ($this->curPartDisp['name'] === 'MAX_FILE_SIZE') { |
||
| 378 | $this->maxFileSize = (int)$this->curPart; |
||
| 379 | } |
||
| 380 | } elseif (($this->state === self::STATE_UPLOAD) && isset($this->curPartDisp['filename'])) { |
||
| 381 | $this->curPart['size'] += $l; |
||
| 382 | $this->curChunkSize = $l; |
||
| 383 | $this->req->onUploadFileChunk($this, true); |
||
| 384 | } |
||
| 385 | |||
| 386 | $this->state = self::STATE_SEEKBOUNDARY; |
||
| 387 | if ($endOfMsg) { // end of whole message |
||
| 388 | $this->sendEOF(); |
||
| 389 | } else { |
||
| 390 | goto start; // let's read the next part |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 438 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.