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