| Conditions | 43 |
| Paths | 120 |
| Total Lines | 202 |
| Code Lines | 108 |
| Lines | 43 |
| Ratio | 21.29 % |
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 |
||
| 311 | function pdf_read_value(&$c, $token = null) {
|
||
| 312 | if (is_null($token)) {
|
||
| 313 | $token = $this->pdf_read_token($c); |
||
| 314 | } |
||
| 315 | |||
| 316 | if ($token === false) {
|
||
| 317 | return false; |
||
| 318 | } |
||
| 319 | |||
| 320 | switch ($token) {
|
||
| 321 | case '<': |
||
| 322 | // This is a hex string. |
||
| 323 | // Read the value, then the terminator |
||
| 324 | |||
| 325 | $pos = $c->offset; |
||
| 326 | |||
| 327 | while(1) {
|
||
| 328 | |||
| 329 | $match = strpos ($c->buffer, '>', $pos); |
||
| 330 | |||
| 331 | // If you can't find it, try |
||
| 332 | // reading more data from the stream |
||
| 333 | |||
| 334 | if ($match === false) {
|
||
| 335 | if (!$c->increase_length()) {
|
||
| 336 | return false; |
||
| 337 | } else {
|
||
| 338 | continue; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | $result = substr ($c->buffer, $c->offset, $match - $c->offset); |
||
| 343 | $c->offset = $match + 1; |
||
| 344 | |||
| 345 | return array (PDF_TYPE_HEX, $result); |
||
| 346 | } |
||
| 347 | |||
| 348 | break; |
||
| 349 | View Code Duplication | case '<<': |
|
| 350 | // This is a dictionary. |
||
| 351 | |||
| 352 | $result = array(); |
||
| 353 | |||
| 354 | // Recurse into this function until we reach |
||
| 355 | // the end of the dictionary. |
||
| 356 | while (($key = $this->pdf_read_token($c)) !== '>>') {
|
||
| 357 | if ($key === false) {
|
||
| 358 | return false; |
||
| 359 | } |
||
| 360 | |||
| 361 | if (($value = $this->pdf_read_value($c)) === false) {
|
||
| 362 | return false; |
||
| 363 | } |
||
| 364 | $result[$key] = $value; |
||
| 365 | } |
||
| 366 | |||
| 367 | return array (PDF_TYPE_DICTIONARY, $result); |
||
| 368 | |||
| 369 | View Code Duplication | case '[': |
|
| 370 | // This is an array. |
||
| 371 | |||
| 372 | $result = array(); |
||
| 373 | |||
| 374 | // Recurse into this function until we reach |
||
| 375 | // the end of the array. |
||
| 376 | while (($token = $this->pdf_read_token($c)) !== ']') {
|
||
| 377 | if ($token === false) {
|
||
| 378 | return false; |
||
| 379 | } |
||
| 380 | |||
| 381 | if (($value = $this->pdf_read_value($c, $token)) === false) {
|
||
| 382 | return false; |
||
| 383 | } |
||
| 384 | |||
| 385 | $result[] = $value; |
||
| 386 | } |
||
| 387 | |||
| 388 | return array (PDF_TYPE_ARRAY, $result); |
||
| 389 | |||
| 390 | case '(' :
|
||
| 391 | // This is a string |
||
| 392 | $pos = $c->offset; |
||
| 393 | |||
| 394 | $openBrackets = 1; |
||
| 395 | do {
|
||
| 396 | for (; $openBrackets != 0 && $pos < $c->length; $pos++) {
|
||
| 397 | switch (ord($c->buffer[$pos])) {
|
||
| 398 | case 0x28: // '('
|
||
| 399 | $openBrackets++; |
||
| 400 | break; |
||
| 401 | case 0x29: // ')' |
||
| 402 | $openBrackets--; |
||
| 403 | break; |
||
| 404 | case 0x5C: // backslash |
||
| 405 | $pos++; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | } while($openBrackets != 0 && $c->increase_length()); |
||
| 409 | |||
| 410 | $result = substr($c->buffer, $c->offset, $pos - $c->offset - 1); |
||
| 411 | $c->offset = $pos; |
||
| 412 | |||
| 413 | return array (PDF_TYPE_STRING, $result); |
||
| 414 | |||
| 415 | |||
| 416 | case "stream": |
||
| 417 | $o_pos = ftell($c->file)-strlen($c->buffer); |
||
| 418 | $o_offset = $c->offset; |
||
| 419 | |||
| 420 | $c->reset($startpos = $o_pos + $o_offset); |
||
| 421 | |||
| 422 | $e = 0; // ensure line breaks in front of the stream |
||
| 423 | View Code Duplication | if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13)) |
|
| 424 | $e++; |
||
| 425 | View Code Duplication | if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10)) |
|
| 426 | $e++; |
||
| 427 | |||
| 428 | if ($this->actual_obj[1][1]['/Length'][0] == PDF_TYPE_OBJREF) {
|
||
| 429 | $tmp_c =& new pdf_context($this->f); |
||
| 430 | $tmp_length = $this->pdf_resolve_object($tmp_c,$this->actual_obj[1][1]['/Length']); |
||
| 431 | $length = $tmp_length[1][1]; |
||
| 432 | } else {
|
||
| 433 | $length = $this->actual_obj[1][1]['/Length'][1]; |
||
| 434 | } |
||
| 435 | |||
| 436 | if ($length > 0) {
|
||
| 437 | $c->reset($startpos+$e,$length); |
||
| 438 | $v = $c->buffer; |
||
| 439 | } else {
|
||
| 440 | $v = ''; |
||
| 441 | } |
||
| 442 | $c->reset($startpos+$e+$length+9); // 9 = strlen("endstream")
|
||
| 443 | |||
| 444 | return array(PDF_TYPE_STREAM, $v); |
||
| 445 | |||
| 446 | case '%': |
||
| 447 | // this is a comment - just jump over it |
||
| 448 | $pos = $c->offset; |
||
| 449 | while(1) {
|
||
| 450 | // PHP 4.3.3 required |
||
| 451 | #$match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);
|
||
| 452 | // alternative |
||
| 453 | $match = preg_match("/(\r\n|\r|\n)/", substr($c->buffer, $pos), $m);
|
||
| 454 | if ($match === false) {
|
||
| 455 | if (!$c->increase_length()) {
|
||
| 456 | return false; |
||
| 457 | } else {
|
||
| 458 | continue; |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | // PHP 4.3.3 required |
||
| 463 | #$c->offset = $m[0][1]+strlen($m[0][0]); |
||
| 464 | // alternative |
||
| 465 | $c->offset = strpos($c->buffer, $m[0], $pos)+strlen($m[0]); |
||
| 466 | |||
| 467 | return $this->pdf_read_value($c); |
||
| 468 | } |
||
| 469 | |||
| 470 | default : |
||
| 471 | if (is_numeric ($token)) {
|
||
| 472 | // A numeric token. Make sure that |
||
| 473 | // it is not part of something else. |
||
| 474 | if (($tok2 = $this->pdf_read_token ($c)) !== false) {
|
||
| 475 | if (is_numeric ($tok2)) {
|
||
| 476 | |||
| 477 | // Two numeric tokens in a row. |
||
| 478 | // In this case, we're probably in |
||
| 479 | // front of either an object reference |
||
| 480 | // or an object specification. |
||
| 481 | // Determine the case and return the data |
||
| 482 | if (($tok3 = $this->pdf_read_token ($c)) !== false) {
|
||
| 483 | switch ($tok3) {
|
||
| 484 | case 'obj' : |
||
| 485 | return array (PDF_TYPE_OBJDEC, (int) $token, (int) $tok2); |
||
| 486 | case 'R' : |
||
| 487 | return array (PDF_TYPE_OBJREF, (int) $token, (int) $tok2); |
||
| 488 | } |
||
| 489 | // If we get to this point, that numeric value up |
||
| 490 | // there was just a numeric value. Push the extra |
||
| 491 | // tokens back into the stack and return the value. |
||
| 492 | array_push ($c->stack, $tok3); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | array_push ($c->stack, $tok2); |
||
| 497 | } |
||
| 498 | |||
| 499 | if ($token === (string)((int)$token)) |
||
| 500 | return array (PDF_TYPE_NUMERIC, (int)$token); |
||
| 501 | else |
||
| 502 | return array (PDF_TYPE_REAL, (float)$token); |
||
| 503 | } else if ($token == 'true' || $token == 'false') {
|
||
| 504 | return array (PDF_TYPE_BOOLEAN, $token == 'true'); |
||
| 505 | } else {
|
||
| 506 | |||
| 507 | // Just a token. Return it. |
||
| 508 | return array (PDF_TYPE_TOKEN, $token); |
||
| 509 | } |
||
| 510 | |||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 690 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.