| Conditions | 57 |
| Paths | 6 |
| Total Lines | 264 |
| Code Lines | 152 |
| 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 |
||
| 643 | private function parse_span_elements($text, $markers = array('![', '&', '*', '<', '[', '_', '`', 'http', '~~')) |
||
| 644 | { |
||
| 645 | if (false === isset($text[2]) or $markers === array()) { |
||
| 646 | return $text; |
||
| 647 | } |
||
| 648 | |||
| 649 | # ~ |
||
| 650 | |||
| 651 | $markup = ''; |
||
| 652 | |||
| 653 | while ($markers) { |
||
| 654 | $closest_marker = null; |
||
| 655 | $closest_marker_index = 0; |
||
| 656 | $closest_marker_position = null; |
||
| 657 | |||
| 658 | foreach ($markers as $index => $marker) { |
||
| 659 | $marker_position = strpos($text, $marker); |
||
| 660 | |||
| 661 | if (false === $marker_position) { |
||
| 662 | unset($markers[$index]); |
||
| 663 | |||
| 664 | continue; |
||
| 665 | } |
||
| 666 | |||
| 667 | if (null === $closest_marker or $marker_position < $closest_marker_position) { |
||
| 668 | $closest_marker = $marker; |
||
| 669 | $closest_marker_index = $index; |
||
| 670 | $closest_marker_position = $marker_position; |
||
| 671 | } |
||
| 672 | } |
||
| 673 | |||
| 674 | # ~ |
||
| 675 | |||
| 676 | if (null === $closest_marker or false === isset($text[$closest_marker_position + 2])) { |
||
| 677 | $markup .= $text; |
||
| 678 | |||
| 679 | break; |
||
| 680 | } else { |
||
| 681 | $markup .= substr($text, 0, $closest_marker_position); |
||
| 682 | } |
||
| 683 | |||
| 684 | $text = substr($text, $closest_marker_position); |
||
| 685 | |||
| 686 | # ~ |
||
| 687 | |||
| 688 | unset($markers[$closest_marker_index]); |
||
| 689 | |||
| 690 | # ~ |
||
| 691 | |||
| 692 | switch ($closest_marker) { |
||
| 693 | case '![': |
||
| 694 | case '[': |
||
| 695 | |||
| 696 | if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches)) { |
||
| 697 | $element = array( |
||
| 698 | '!' => '!' === $text[0], |
||
| 699 | 'a' => $matches[1] |
||
| 700 | ); |
||
| 701 | |||
| 702 | $offset = strlen($matches[0]); |
||
| 703 | |||
| 704 | $element['!'] and ++$offset; |
||
| 705 | |||
| 706 | $remaining_text = substr($text, $offset); |
||
| 707 | |||
| 708 | if ('(' === $remaining_text[0] and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remaining_text, $matches)) { |
||
| 709 | $element['»'] = $matches[1]; |
||
| 710 | |||
| 711 | if (isset($matches[2])) { |
||
| 712 | $element['#'] = $matches[2]; |
||
| 713 | } |
||
| 714 | |||
| 715 | $offset += strlen($matches[0]); |
||
| 716 | } elseif ($this->reference_map) { |
||
| 717 | $reference = $element['a']; |
||
| 718 | |||
| 719 | if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches)) { |
||
| 720 | $reference = $matches[1] ?: $element['a']; |
||
| 721 | |||
| 722 | $offset += strlen($matches[0]); |
||
| 723 | } |
||
| 724 | |||
| 725 | $reference = strtolower($reference); |
||
| 726 | |||
| 727 | if (isset($this->reference_map[$reference])) { |
||
| 728 | $element['»'] = $this->reference_map[$reference]['»']; |
||
| 729 | |||
| 730 | if (isset($this->reference_map[$reference]['#'])) { |
||
| 731 | $element['#'] = $this->reference_map[$reference]['#']; |
||
| 732 | } |
||
| 733 | } else { |
||
| 734 | unset($element); |
||
| 735 | } |
||
| 736 | } else { |
||
| 737 | unset($element); |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | if (isset($element)) { |
||
| 742 | $element['»'] = str_replace('&', '&', $element['»']); |
||
| 743 | $element['»'] = str_replace('<', '<', $element['»']); |
||
| 744 | |||
| 745 | if ($element['!']) { |
||
| 746 | $markup .= '<img alt="' . $element['a'] . '" src="' . $element['»'] . '" >'; |
||
| 747 | } else { |
||
| 748 | $element['a'] = $this->parse_span_elements($element['a'], $markers); |
||
| 749 | |||
| 750 | $markup .= isset($element['#']) ? '<a href="' . $element['»'] . '" title="' . $element['#'] . '">' . $element['a'] . '</a>' : '<a href="' . $element['»'] . '">' . $element['a'] . '</a>'; |
||
| 751 | } |
||
| 752 | |||
| 753 | unset($element); |
||
| 754 | } else { |
||
| 755 | $markup .= $closest_marker; |
||
| 756 | |||
| 757 | $offset = '![' === $closest_marker ? 2 : 1; |
||
| 758 | } |
||
| 759 | |||
| 760 | break; |
||
| 761 | |||
| 762 | case '&': |
||
| 763 | |||
| 764 | if (preg_match('/^&#?\w+;/', $text, $matches)) { |
||
| 765 | $markup .= $matches[0]; |
||
| 766 | |||
| 767 | $offset = strlen($matches[0]); |
||
| 768 | } else { |
||
| 769 | $markup .= '&'; |
||
| 770 | |||
| 771 | $offset = 1; |
||
| 772 | } |
||
| 773 | |||
| 774 | break; |
||
| 775 | |||
| 776 | case '*': |
||
| 777 | case '_': |
||
| 778 | |||
| 779 | if ($text[1] === $closest_marker and preg_match($this->strong_regex[$closest_marker], $text, $matches)) { |
||
| 780 | $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
| 781 | |||
| 782 | $markup .= '<strong>' . $matches[1] . '</strong>'; |
||
| 783 | } elseif (preg_match($this->em_regex[$closest_marker], $text, $matches)) { |
||
| 784 | $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
| 785 | |||
| 786 | $markup .= '<em>' . $matches[1] . '</em>'; |
||
| 787 | } elseif ($text[1] === $closest_marker and preg_match($this->strong_em_regex[$closest_marker], $text, $matches)) { |
||
| 788 | $matches[2] = $this->parse_span_elements($matches[2], $markers); |
||
| 789 | |||
| 790 | $matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
| 791 | $matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers); |
||
| 792 | |||
| 793 | $markup .= '<strong>' . $matches[1] . '<em>' . $matches[2] . '</em>' . $matches[3] . '</strong>'; |
||
| 794 | } elseif (preg_match($this->em_strong_regex[$closest_marker], $text, $matches)) { |
||
| 795 | $matches[2] = $this->parse_span_elements($matches[2], $markers); |
||
| 796 | |||
| 797 | $matches[1] and $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
| 798 | $matches[3] and $matches[3] = $this->parse_span_elements($matches[3], $markers); |
||
| 799 | |||
| 800 | $markup .= '<em>' . $matches[1] . '<strong>' . $matches[2] . '</strong>' . $matches[3] . '</em>'; |
||
| 801 | } |
||
| 802 | |||
| 803 | if (isset($matches) and $matches) { |
||
| 804 | $offset = strlen($matches[0]); |
||
| 805 | } else { |
||
| 806 | $markup .= $closest_marker; |
||
| 807 | |||
| 808 | $offset = 1; |
||
| 809 | } |
||
| 810 | |||
| 811 | break; |
||
| 812 | |||
| 813 | case '<': |
||
| 814 | |||
| 815 | if (false !== strpos($text, '>')) { |
||
| 816 | if ('h' === $text[1] and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $text, $matches)) { |
||
| 817 | $element_url = $matches[1]; |
||
| 818 | $element_url = str_replace('&', '&', $element_url); |
||
| 819 | $element_url = str_replace('<', '<', $element_url); |
||
| 820 | |||
| 821 | $markup .= '<a href="' . $element_url . '">' . $element_url . '</a>'; |
||
| 822 | |||
| 823 | $offset = strlen($matches[0]); |
||
| 824 | } elseif (preg_match('/^<\/?\w.*?>/', $text, $matches)) { |
||
| 825 | $markup .= $matches[0]; |
||
| 826 | |||
| 827 | $offset = strlen($matches[0]); |
||
| 828 | } else { |
||
| 829 | $markup .= '<'; |
||
| 830 | |||
| 831 | $offset = 1; |
||
| 832 | } |
||
| 833 | } else { |
||
| 834 | $markup .= '<'; |
||
| 835 | |||
| 836 | $offset = 1; |
||
| 837 | } |
||
| 838 | |||
| 839 | break; |
||
| 840 | |||
| 841 | case '`': |
||
| 842 | |||
| 843 | if (preg_match('/^`(.+?)`/', $text, $matches)) { |
||
| 844 | $element_text = $matches[1]; |
||
| 845 | $element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8'); |
||
| 846 | |||
| 847 | if ($this->escape_sequence_map and false !== strpos($element_text, "\x1A")) { |
||
| 848 | $element_text = strtr($element_text, $this->escape_sequence_map); |
||
| 849 | } |
||
| 850 | |||
| 851 | $markup .= '<code>' . $element_text . '</code>'; |
||
| 852 | |||
| 853 | $offset = strlen($matches[0]); |
||
| 854 | } else { |
||
| 855 | $markup .= '`'; |
||
| 856 | |||
| 857 | $offset = 1; |
||
| 858 | } |
||
| 859 | |||
| 860 | break; |
||
| 861 | |||
| 862 | case 'http': |
||
| 863 | |||
| 864 | if (preg_match('/^https?:[\/]{2}[^\s]+\b/i', $text, $matches)) { |
||
| 865 | $element_url = $matches[0]; |
||
| 866 | $element_url = str_replace('&', '&', $element_url); |
||
| 867 | $element_url = str_replace('<', '<', $element_url); |
||
| 868 | |||
| 869 | $markup .= '<a href="' . $element_url . '">' . $element_url . '</a>'; |
||
| 870 | |||
| 871 | $offset = strlen($matches[0]); |
||
| 872 | } else { |
||
| 873 | $markup .= 'http'; |
||
| 874 | |||
| 875 | $offset = 4; |
||
| 876 | } |
||
| 877 | |||
| 878 | break; |
||
| 879 | |||
| 880 | case '~~': |
||
| 881 | |||
| 882 | if (preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $text, $matches)) { |
||
| 883 | $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
| 884 | |||
| 885 | $markup .= '<del>' . $matches[1] . '</del>'; |
||
| 886 | |||
| 887 | $offset = strlen($matches[0]); |
||
| 888 | } else { |
||
| 889 | $markup .= '~~'; |
||
| 890 | |||
| 891 | $offset = 2; |
||
| 892 | } |
||
| 893 | |||
| 894 | break; |
||
| 895 | } |
||
| 896 | |||
| 897 | if (isset($offset)) { |
||
| 898 | $text = substr($text, $offset); |
||
| 899 | } |
||
| 900 | |||
| 901 | $markers[$closest_marker_index] = $closest_marker; |
||
| 902 | } |
||
| 903 | |||
| 904 | $markup = str_replace($this->break_marker, '<br >' . "\n", $markup); |
||
| 905 | |||
| 906 | return $markup; |
||
| 907 | } |
||
| 969 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.