| Conditions | 95 |
| Paths | > 20000 |
| Total Lines | 658 |
| Lines | 30 |
| Ratio | 4.56 % |
| 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 |
||
| 92 | private function parse_block_elements(array $lines, $context = '') |
||
| 93 | { |
||
| 94 | $blocks = array(); |
||
| 95 | |||
| 96 | $block = array( |
||
| 97 | 'type' => '', |
||
| 98 | ); |
||
| 99 | |||
| 100 | foreach ($lines as $line) |
||
| 101 | { |
||
| 102 | # context |
||
| 103 | |||
| 104 | switch ($block['type']) |
||
| 105 | { |
||
| 106 | case 'fenced': |
||
|
|
|||
| 107 | |||
| 108 | if ( ! isset($block['closed'])) |
||
| 109 | { |
||
| 110 | if (preg_match('/^[ ]*'.$block['fence'][0].'{3,}[ ]*$/', $line)) |
||
| 111 | { |
||
| 112 | $block['closed'] = true; |
||
| 113 | } |
||
| 114 | View Code Duplication | else |
|
| 115 | { |
||
| 116 | if ($block['text'] !== '') |
||
| 117 | { |
||
| 118 | $block['text'] .= "\n"; |
||
| 119 | } |
||
| 120 | |||
| 121 | $block['text'] .= $line; |
||
| 122 | } |
||
| 123 | |||
| 124 | continue 2; |
||
| 125 | } |
||
| 126 | |||
| 127 | break; |
||
| 128 | |||
| 129 | case 'markup': |
||
| 130 | |||
| 131 | if ( ! isset($block['closed'])) |
||
| 132 | { |
||
| 133 | if (strpos($line, $block['start']) !== false) # opening tag |
||
| 134 | { |
||
| 135 | $block['depth']++; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (strpos($line, $block['end']) !== false) # closing tag |
||
| 139 | { |
||
| 140 | if ($block['depth'] > 0) |
||
| 141 | { |
||
| 142 | $block['depth']--; |
||
| 143 | } |
||
| 144 | else |
||
| 145 | { |
||
| 146 | $block['closed'] = true; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $block['text'] .= "\n".$line; |
||
| 151 | |||
| 152 | continue 2; |
||
| 153 | } |
||
| 154 | |||
| 155 | break; |
||
| 156 | } |
||
| 157 | |||
| 158 | # ~ |
||
| 159 | |||
| 160 | $indentation = 0; |
||
| 161 | |||
| 162 | while(isset($line[$indentation]) and $line[$indentation] === ' ') |
||
| 163 | { |
||
| 164 | $indentation++; |
||
| 165 | } |
||
| 166 | |||
| 167 | $outdented_line = $indentation > 0 ? ltrim($line) : $line; |
||
| 168 | |||
| 169 | # blank |
||
| 170 | |||
| 171 | if ($outdented_line === '') |
||
| 172 | { |
||
| 173 | $block['interrupted'] = true; |
||
| 174 | |||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | # context |
||
| 179 | |||
| 180 | switch ($block['type']) |
||
| 181 | { |
||
| 182 | case 'quote': |
||
| 183 | |||
| 184 | if ( ! isset($block['interrupted'])) |
||
| 185 | { |
||
| 186 | $line = preg_replace('/^[ ]*>[ ]?/', '', $line); |
||
| 187 | |||
| 188 | $block['lines'] []= $line; |
||
| 189 | |||
| 190 | continue 2; |
||
| 191 | } |
||
| 192 | |||
| 193 | break; |
||
| 194 | |||
| 195 | case 'li': |
||
| 196 | |||
| 197 | if ($block['indentation'] === $indentation and preg_match('/^'.$block['marker'].'[ ]+(.*)/', $outdented_line, $matches)) |
||
| 198 | { |
||
| 199 | unset($block['last']); |
||
| 200 | |||
| 201 | $blocks []= $block; |
||
| 202 | |||
| 203 | $block['last'] = true; |
||
| 204 | $block['lines'] = array($matches[1]); |
||
| 205 | |||
| 206 | unset($block['first']); |
||
| 207 | unset($block['interrupted']); |
||
| 208 | |||
| 209 | continue 2; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ( ! isset($block['interrupted'])) |
||
| 213 | { |
||
| 214 | $line = preg_replace('/^[ ]{0,'.$block['baseline'].'}/', '', $line); |
||
| 215 | |||
| 216 | $block['lines'] []= $line; |
||
| 217 | |||
| 218 | continue 2; |
||
| 219 | } |
||
| 220 | elseif ($line[0] === ' ') |
||
| 221 | { |
||
| 222 | $block['lines'] []= ''; |
||
| 223 | |||
| 224 | $line = preg_replace('/^[ ]{0,'.$block['baseline'].'}/', '', $line); |
||
| 225 | |||
| 226 | $block['lines'] []= $line; |
||
| 227 | |||
| 228 | unset($block['interrupted']); |
||
| 229 | |||
| 230 | continue 2; |
||
| 231 | } |
||
| 232 | |||
| 233 | break; |
||
| 234 | } |
||
| 235 | |||
| 236 | # indentation sensitive types |
||
| 237 | |||
| 238 | switch ($line[0]) |
||
| 239 | { |
||
| 240 | case ' ': |
||
| 241 | |||
| 242 | # code |
||
| 243 | |||
| 244 | if ($indentation >= 4) |
||
| 245 | { |
||
| 246 | $code_line = substr($line, 4); |
||
| 247 | |||
| 248 | if ($block['type'] === 'code') |
||
| 249 | { |
||
| 250 | if (isset($block['interrupted'])) |
||
| 251 | { |
||
| 252 | $block['text'] .= "\n"; |
||
| 253 | |||
| 254 | unset($block['interrupted']); |
||
| 255 | } |
||
| 256 | |||
| 257 | $block['text'] .= "\n".$code_line; |
||
| 258 | } |
||
| 259 | else |
||
| 260 | { |
||
| 261 | $blocks []= $block; |
||
| 262 | |||
| 263 | $block = array( |
||
| 264 | 'type' => 'code', |
||
| 265 | 'text' => $code_line, |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | continue 2; |
||
| 270 | } |
||
| 271 | |||
| 272 | break; |
||
| 273 | |||
| 274 | case '#': |
||
| 275 | |||
| 276 | # atx heading (#) |
||
| 277 | |||
| 278 | if (isset($line[1])) |
||
| 279 | { |
||
| 280 | $blocks []= $block; |
||
| 281 | |||
| 282 | $level = 1; |
||
| 283 | |||
| 284 | while (isset($line[$level]) and $line[$level] === '#') |
||
| 285 | { |
||
| 286 | $level++; |
||
| 287 | } |
||
| 288 | |||
| 289 | $block = array( |
||
| 290 | 'type' => 'heading', |
||
| 291 | 'text' => trim($line, '# '), |
||
| 292 | 'level' => $level, |
||
| 293 | ); |
||
| 294 | |||
| 295 | continue 2; |
||
| 296 | } |
||
| 297 | |||
| 298 | break; |
||
| 299 | |||
| 300 | case '-': |
||
| 301 | case '=': |
||
| 302 | |||
| 303 | # setext heading (===) |
||
| 304 | |||
| 305 | if ($block['type'] === 'paragraph' and isset($block['interrupted']) === false) |
||
| 306 | { |
||
| 307 | $chopped_line = chop($line); |
||
| 308 | |||
| 309 | $i = 1; |
||
| 310 | |||
| 311 | while (isset($chopped_line[$i])) |
||
| 312 | { |
||
| 313 | if ($chopped_line[$i] !== $line[0]) |
||
| 314 | { |
||
| 315 | break 2; |
||
| 316 | } |
||
| 317 | |||
| 318 | $i++; |
||
| 319 | } |
||
| 320 | |||
| 321 | $block['type'] = 'heading'; |
||
| 322 | |||
| 323 | $block['level'] = $line[0] === '-' ? 2 : 1; |
||
| 324 | |||
| 325 | continue 2; |
||
| 326 | } |
||
| 327 | |||
| 328 | break; |
||
| 329 | } |
||
| 330 | |||
| 331 | # indentation insensitive types |
||
| 332 | |||
| 333 | switch ($outdented_line[0]) |
||
| 334 | { |
||
| 335 | case '<': |
||
| 336 | |||
| 337 | $position = strpos($outdented_line, '>'); |
||
| 338 | |||
| 339 | if ($position > 1) |
||
| 340 | { |
||
| 341 | $substring = substr($outdented_line, 1, $position - 1); |
||
| 342 | |||
| 343 | $substring = chop($substring); |
||
| 344 | |||
| 345 | if (substr($substring, -1) === '/') |
||
| 346 | { |
||
| 347 | $is_self_closing = true; |
||
| 348 | |||
| 349 | $substring = substr($substring, 0, -1); |
||
| 350 | } |
||
| 351 | |||
| 352 | $position = strpos($substring, ' '); |
||
| 353 | |||
| 354 | if ($position) |
||
| 355 | { |
||
| 356 | $name = substr($substring, 0, $position); |
||
| 357 | } |
||
| 358 | else |
||
| 359 | { |
||
| 360 | $name = $substring; |
||
| 361 | } |
||
| 362 | |||
| 363 | if ( ! ctype_alpha($name)) |
||
| 364 | { |
||
| 365 | break; |
||
| 366 | } |
||
| 367 | |||
| 368 | if (in_array($name, self::$text_level_elements)) |
||
| 369 | { |
||
| 370 | break; |
||
| 371 | } |
||
| 372 | |||
| 373 | $blocks []= $block; |
||
| 374 | |||
| 375 | if (isset($is_self_closing)) |
||
| 376 | { |
||
| 377 | $block = array( |
||
| 378 | 'type' => 'self-closing tag', |
||
| 379 | 'text' => $outdented_line, |
||
| 380 | ); |
||
| 381 | |||
| 382 | unset($is_self_closing); |
||
| 383 | |||
| 384 | continue 2; |
||
| 385 | } |
||
| 386 | |||
| 387 | $block = array( |
||
| 388 | 'type' => 'markup', |
||
| 389 | 'text' => $outdented_line, |
||
| 390 | 'start' => '<'.$name.'>', |
||
| 391 | 'end' => '</'.$name.'>', |
||
| 392 | 'depth' => 0, |
||
| 393 | ); |
||
| 394 | |||
| 395 | if (strpos($outdented_line, $block['end'])) |
||
| 396 | { |
||
| 397 | $block['closed'] = true; |
||
| 398 | } |
||
| 399 | |||
| 400 | continue 2; |
||
| 401 | } |
||
| 402 | |||
| 403 | break; |
||
| 404 | |||
| 405 | case '>': |
||
| 406 | |||
| 407 | # quote |
||
| 408 | |||
| 409 | if (preg_match('/^>[ ]?(.*)/', $outdented_line, $matches)) |
||
| 410 | { |
||
| 411 | $blocks []= $block; |
||
| 412 | |||
| 413 | $block = array( |
||
| 414 | 'type' => 'quote', |
||
| 415 | 'lines' => array( |
||
| 416 | $matches[1], |
||
| 417 | ), |
||
| 418 | ); |
||
| 419 | |||
| 420 | continue 2; |
||
| 421 | } |
||
| 422 | |||
| 423 | break; |
||
| 424 | |||
| 425 | case '[': |
||
| 426 | |||
| 427 | # reference |
||
| 428 | |||
| 429 | $position = strpos($outdented_line, ']:'); |
||
| 430 | |||
| 431 | if ($position) |
||
| 432 | { |
||
| 433 | $reference = array(); |
||
| 434 | |||
| 435 | $label = substr($outdented_line, 1, $position - 1); |
||
| 436 | $label = strtolower($label); |
||
| 437 | |||
| 438 | $substring = substr($outdented_line, $position + 2); |
||
| 439 | $substring = trim($substring); |
||
| 440 | |||
| 441 | if ($substring === '') |
||
| 442 | { |
||
| 443 | break; |
||
| 444 | } |
||
| 445 | |||
| 446 | if ($substring[0] === '<') |
||
| 447 | { |
||
| 448 | $position = strpos($substring, '>'); |
||
| 449 | |||
| 450 | if ($position === false) |
||
| 451 | { |
||
| 452 | break; |
||
| 453 | } |
||
| 454 | |||
| 455 | $reference['»'] = substr($substring, 1, $position - 1); |
||
| 456 | |||
| 457 | $substring = substr($substring, $position + 1); |
||
| 458 | } |
||
| 459 | else |
||
| 460 | { |
||
| 461 | $position = strpos($substring, ' '); |
||
| 462 | |||
| 463 | if ($position === false) |
||
| 464 | { |
||
| 465 | $reference['»'] = $substring; |
||
| 466 | |||
| 467 | $substring = false; |
||
| 468 | } |
||
| 469 | else |
||
| 470 | { |
||
| 471 | $reference['»'] = substr($substring, 0, $position); |
||
| 472 | |||
| 473 | $substring = substr($substring, $position + 1); |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | if ($substring !== false) |
||
| 478 | { |
||
| 479 | if ($substring[0] !== '"' and $substring[0] !== "'" and $substring[0] !== '(') |
||
| 480 | { |
||
| 481 | break; |
||
| 482 | } |
||
| 483 | |||
| 484 | $last_char = substr($substring, -1); |
||
| 485 | |||
| 486 | if ($last_char !== '"' and $last_char !== "'" and $last_char !== ')') |
||
| 487 | { |
||
| 488 | break; |
||
| 489 | } |
||
| 490 | |||
| 491 | $reference['#'] = substr($substring, 1, -1); |
||
| 492 | } |
||
| 493 | |||
| 494 | $this->reference_map[$label] = $reference; |
||
| 495 | |||
| 496 | continue 2; |
||
| 497 | } |
||
| 498 | |||
| 499 | break; |
||
| 500 | |||
| 501 | case '`': |
||
| 502 | case '~': |
||
| 503 | |||
| 504 | # fenced code block |
||
| 505 | |||
| 506 | if (preg_match('/^([`]{3,}|[~]{3,})[ ]*(\S+)?[ ]*$/', $outdented_line, $matches)) |
||
| 507 | { |
||
| 508 | $blocks []= $block; |
||
| 509 | |||
| 510 | $block = array( |
||
| 511 | 'type' => 'fenced', |
||
| 512 | 'text' => '', |
||
| 513 | 'fence' => $matches[1], |
||
| 514 | ); |
||
| 515 | |||
| 516 | if (isset($matches[2])) |
||
| 517 | { |
||
| 518 | $block['language'] = $matches[2]; |
||
| 519 | } |
||
| 520 | |||
| 521 | continue 2; |
||
| 522 | } |
||
| 523 | |||
| 524 | break; |
||
| 525 | |||
| 526 | case '*': |
||
| 527 | case '+': |
||
| 528 | case '-': |
||
| 529 | case '_': |
||
| 530 | |||
| 531 | # hr |
||
| 532 | |||
| 533 | if (preg_match('/^([-*_])([ ]{0,2}\1){2,}[ ]*$/', $outdented_line)) |
||
| 534 | { |
||
| 535 | $blocks []= $block; |
||
| 536 | |||
| 537 | $block = array( |
||
| 538 | 'type' => 'rule', |
||
| 539 | ); |
||
| 540 | |||
| 541 | continue 2; |
||
| 542 | } |
||
| 543 | |||
| 544 | # li |
||
| 545 | |||
| 546 | if (preg_match('/^([*+-][ ]+)(.*)/', $outdented_line, $matches)) |
||
| 547 | { |
||
| 548 | $blocks []= $block; |
||
| 549 | |||
| 550 | $baseline = $indentation + strlen($matches[1]); |
||
| 551 | |||
| 552 | $block = array( |
||
| 553 | 'type' => 'li', |
||
| 554 | 'indentation' => $indentation, |
||
| 555 | 'baseline' => $baseline, |
||
| 556 | 'marker' => '[*+-]', |
||
| 557 | 'first' => true, |
||
| 558 | 'last' => true, |
||
| 559 | 'lines' => array(), |
||
| 560 | ); |
||
| 561 | |||
| 562 | $block['lines'] []= preg_replace('/^[ ]{0,4}/', '', $matches[2]); |
||
| 563 | |||
| 564 | continue 2; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | # li |
||
| 569 | |||
| 570 | if ($outdented_line[0] <= '9' and preg_match('/^(\d+[.][ ]+)(.*)/', $outdented_line, $matches)) |
||
| 571 | { |
||
| 572 | $blocks []= $block; |
||
| 573 | |||
| 574 | $baseline = $indentation + strlen($matches[1]); |
||
| 575 | |||
| 576 | $block = array( |
||
| 577 | 'type' => 'li', |
||
| 578 | 'indentation' => $indentation, |
||
| 579 | 'baseline' => $baseline, |
||
| 580 | 'marker' => '\d+[.]', |
||
| 581 | 'first' => true, |
||
| 582 | 'last' => true, |
||
| 583 | 'ordered' => true, |
||
| 584 | 'lines' => array(), |
||
| 585 | ); |
||
| 586 | |||
| 587 | $block['lines'] []= preg_replace('/^[ ]{0,4}/', '', $matches[2]); |
||
| 588 | |||
| 589 | continue; |
||
| 590 | } |
||
| 591 | |||
| 592 | # paragraph |
||
| 593 | |||
| 594 | if ($block['type'] === 'paragraph') |
||
| 595 | { |
||
| 596 | if (isset($block['interrupted'])) |
||
| 597 | { |
||
| 598 | $blocks []= $block; |
||
| 599 | |||
| 600 | $block['text'] = $line; |
||
| 601 | |||
| 602 | unset($block['interrupted']); |
||
| 603 | } |
||
| 604 | View Code Duplication | else |
|
| 605 | { |
||
| 606 | if ($this->breaks_enabled) |
||
| 607 | { |
||
| 608 | $block['text'] .= ' '; |
||
| 609 | } |
||
| 610 | |||
| 611 | $block['text'] .= "\n".$line; |
||
| 612 | } |
||
| 613 | } |
||
| 614 | else |
||
| 615 | { |
||
| 616 | $blocks []= $block; |
||
| 617 | |||
| 618 | $block = array( |
||
| 619 | 'type' => 'paragraph', |
||
| 620 | 'text' => $line, |
||
| 621 | ); |
||
| 622 | } |
||
| 623 | } |
||
| 624 | |||
| 625 | $blocks []= $block; |
||
| 626 | |||
| 627 | unset($blocks[0]); |
||
| 628 | |||
| 629 | # $blocks » HTML |
||
| 630 | |||
| 631 | $markup = ''; |
||
| 632 | |||
| 633 | foreach ($blocks as $block) |
||
| 634 | { |
||
| 635 | switch ($block['type']) |
||
| 636 | { |
||
| 637 | case 'paragraph': |
||
| 638 | |||
| 639 | $text = $this->parse_span_elements($block['text']); |
||
| 640 | |||
| 641 | if ($context === 'li' and $markup === '') |
||
| 642 | { |
||
| 643 | if (isset($block['interrupted'])) |
||
| 644 | { |
||
| 645 | $markup .= "\n".'<p>'.$text.'</p>'."\n"; |
||
| 646 | } |
||
| 647 | else |
||
| 648 | { |
||
| 649 | $markup .= $text; |
||
| 650 | |||
| 651 | if (isset($blocks[2])) |
||
| 652 | { |
||
| 653 | $markup .= "\n"; |
||
| 654 | } |
||
| 655 | } |
||
| 656 | } |
||
| 657 | else |
||
| 658 | { |
||
| 659 | $markup .= '<p>'.$text.'</p>'."\n"; |
||
| 660 | } |
||
| 661 | |||
| 662 | break; |
||
| 663 | |||
| 664 | case 'quote': |
||
| 665 | |||
| 666 | $text = $this->parse_block_elements($block['lines']); |
||
| 667 | |||
| 668 | $markup .= '<blockquote>'."\n".$text.'</blockquote>'."\n"; |
||
| 669 | |||
| 670 | break; |
||
| 671 | |||
| 672 | case 'code': |
||
| 673 | |||
| 674 | $text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8'); |
||
| 675 | |||
| 676 | $markup .= '<pre><code>'.$text.'</code></pre>'."\n"; |
||
| 677 | |||
| 678 | break; |
||
| 679 | |||
| 680 | case 'fenced': |
||
| 681 | |||
| 682 | $text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8'); |
||
| 683 | |||
| 684 | $markup .= '<pre><code'; |
||
| 685 | |||
| 686 | if (isset($block['language'])) |
||
| 687 | { |
||
| 688 | $markup .= ' class="language-'.$block['language'].'"'; |
||
| 689 | } |
||
| 690 | |||
| 691 | $markup .= '>'.$text.'</code></pre>'."\n"; |
||
| 692 | |||
| 693 | break; |
||
| 694 | |||
| 695 | case 'heading': |
||
| 696 | |||
| 697 | $text = $this->parse_span_elements($block['text']); |
||
| 698 | |||
| 699 | $markup .= '<h'.$block['level'].'>'.$text.'</h'.$block['level'].'>'."\n"; |
||
| 700 | |||
| 701 | break; |
||
| 702 | |||
| 703 | case 'rule': |
||
| 704 | |||
| 705 | $markup .= '<hr />'."\n"; |
||
| 706 | |||
| 707 | break; |
||
| 708 | |||
| 709 | case 'li': |
||
| 710 | |||
| 711 | View Code Duplication | if (isset($block['first'])) |
|
| 712 | { |
||
| 713 | $type = isset($block['ordered']) ? 'ol' : 'ul'; |
||
| 714 | |||
| 715 | $markup .= '<'.$type.'>'."\n"; |
||
| 716 | } |
||
| 717 | |||
| 718 | if (isset($block['interrupted']) and ! isset($block['last'])) |
||
| 719 | { |
||
| 720 | $block['lines'] []= ''; |
||
| 721 | } |
||
| 722 | |||
| 723 | $text = $this->parse_block_elements($block['lines'], 'li'); |
||
| 724 | |||
| 725 | $markup .= '<li>'.$text.'</li>'."\n"; |
||
| 726 | |||
| 727 | View Code Duplication | if (isset($block['last'])) |
|
| 728 | { |
||
| 729 | $type = isset($block['ordered']) ? 'ol' : 'ul'; |
||
| 730 | |||
| 731 | $markup .= '</'.$type.'>'."\n"; |
||
| 732 | } |
||
| 733 | |||
| 734 | break; |
||
| 735 | |||
| 736 | case 'markup': |
||
| 737 | |||
| 738 | $markup .= $block['text']."\n"; |
||
| 739 | |||
| 740 | break; |
||
| 741 | |||
| 742 | default: |
||
| 743 | |||
| 744 | $markup .= $block['text']."\n"; |
||
| 745 | } |
||
| 746 | } |
||
| 747 | |||
| 748 | return $markup; |
||
| 749 | } |
||
| 750 | |||
| 1136 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.