Conditions | 54 |
Paths | 6 |
Total Lines | 349 |
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 |
||
751 | private function parse_span_elements($text, $markers = array(" \n", '![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~')) |
||
752 | { |
||
753 | if (isset($text[1]) === false or $markers === array()) |
||
754 | { |
||
755 | return $text; |
||
756 | } |
||
757 | |||
758 | # ~ |
||
759 | |||
760 | $markup = ''; |
||
761 | |||
762 | while ($markers) |
||
763 | { |
||
764 | $closest_marker = null; |
||
765 | $closest_marker_index = 0; |
||
766 | $closest_marker_position = null; |
||
767 | |||
768 | foreach ($markers as $index => $marker) |
||
769 | { |
||
770 | $marker_position = strpos($text, $marker); |
||
771 | |||
772 | if ($marker_position === false) |
||
773 | { |
||
774 | unset($markers[$index]); |
||
775 | |||
776 | continue; |
||
777 | } |
||
778 | |||
779 | if ($closest_marker === null or $marker_position < $closest_marker_position) |
||
780 | { |
||
781 | $closest_marker = $marker; |
||
782 | $closest_marker_index = $index; |
||
783 | $closest_marker_position = $marker_position; |
||
784 | } |
||
785 | } |
||
786 | |||
787 | # ~ |
||
788 | |||
789 | if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false) |
||
790 | { |
||
791 | $markup .= $text; |
||
792 | |||
793 | break; |
||
794 | } |
||
795 | else |
||
796 | { |
||
797 | $markup .= substr($text, 0, $closest_marker_position); |
||
798 | } |
||
799 | |||
800 | $text = substr($text, $closest_marker_position); |
||
801 | |||
802 | # ~ |
||
803 | |||
804 | unset($markers[$closest_marker_index]); |
||
805 | |||
806 | # ~ |
||
807 | |||
808 | switch ($closest_marker) |
||
809 | { |
||
810 | case " \n": |
||
811 | |||
812 | $markup .= '<br />'."\n"; |
||
813 | |||
814 | $offset = 3; |
||
815 | |||
816 | break; |
||
817 | |||
818 | case '![': |
||
819 | case '[': |
||
820 | |||
821 | if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches)) |
||
822 | { |
||
823 | $element = array( |
||
824 | '!' => $text[0] === '!', |
||
825 | 'a' => $matches[1], |
||
826 | ); |
||
827 | |||
828 | $offset = strlen($matches[0]); |
||
829 | |||
830 | if ($element['!']) |
||
831 | { |
||
832 | $offset++; |
||
833 | } |
||
834 | |||
835 | $remaining_text = substr($text, $offset); |
||
836 | |||
837 | if ($remaining_text[0] === '(' and preg_match('/\([ ]*(.*?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*\)/', $remaining_text, $matches)) |
||
838 | { |
||
839 | $element['»'] = $matches[1]; |
||
840 | |||
841 | if (isset($matches[2])) |
||
842 | { |
||
843 | $element['#'] = $matches[2]; |
||
844 | } |
||
845 | |||
846 | $offset += strlen($matches[0]); |
||
847 | } |
||
848 | elseif ($this->reference_map) |
||
849 | { |
||
850 | $reference = $element['a']; |
||
851 | |||
852 | if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches)) |
||
853 | { |
||
854 | $reference = $matches[1] ? $matches[1] : $element['a']; |
||
855 | |||
856 | $offset += strlen($matches[0]); |
||
857 | } |
||
858 | |||
859 | $reference = strtolower($reference); |
||
860 | |||
861 | if (isset($this->reference_map[$reference])) |
||
862 | { |
||
863 | $element['»'] = $this->reference_map[$reference]['»']; |
||
864 | |||
865 | if (isset($this->reference_map[$reference]['#'])) |
||
866 | { |
||
867 | $element['#'] = $this->reference_map[$reference]['#']; |
||
868 | } |
||
869 | } |
||
870 | else |
||
871 | { |
||
872 | unset($element); |
||
873 | } |
||
874 | } |
||
875 | else |
||
876 | { |
||
877 | unset($element); |
||
878 | } |
||
879 | } |
||
880 | |||
881 | if (isset($element)) |
||
882 | { |
||
883 | $element['»'] = str_replace('&', '&', $element['»']); |
||
884 | $element['»'] = str_replace('<', '<', $element['»']); |
||
885 | |||
886 | if ($element['!']) |
||
887 | { |
||
888 | $markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'"'; |
||
889 | |||
890 | if (isset($element['#'])) |
||
891 | { |
||
892 | $markup .= ' title="'.$element['#'].'"'; |
||
893 | } |
||
894 | |||
895 | $markup .= ' />'; |
||
896 | } |
||
897 | else |
||
898 | { |
||
899 | $element['a'] = $this->parse_span_elements($element['a'], $markers); |
||
900 | |||
901 | $markup .= '<a href="'.$element['»'].'"'; |
||
902 | |||
903 | if (isset($element['#'])) |
||
904 | { |
||
905 | $markup .= ' title="'.$element['#'].'"'; |
||
906 | } |
||
907 | |||
908 | $markup .= '>'.$element['a'].'</a>'; |
||
909 | } |
||
910 | |||
911 | unset($element); |
||
912 | } |
||
913 | else |
||
914 | { |
||
915 | $markup .= $closest_marker; |
||
916 | |||
917 | $offset = $closest_marker === '![' ? 2 : 1; |
||
918 | } |
||
919 | |||
920 | break; |
||
921 | |||
922 | case '&': |
||
923 | |||
924 | if (preg_match('/^&#?\w+;/', $text, $matches)) |
||
925 | { |
||
926 | $markup .= $matches[0]; |
||
927 | |||
928 | $offset = strlen($matches[0]); |
||
929 | } |
||
930 | else |
||
931 | { |
||
932 | $markup .= '&'; |
||
933 | |||
934 | $offset = 1; |
||
935 | } |
||
936 | |||
937 | break; |
||
938 | |||
939 | case '*': |
||
940 | case '_': |
||
941 | |||
942 | if ($text[1] === $closest_marker and preg_match(self::$strong_regex[$closest_marker], $text, $matches)) |
||
943 | { |
||
944 | $markers[] = $closest_marker; |
||
945 | $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
946 | |||
947 | $markup .= '<strong>'.$matches[1].'</strong>'; |
||
948 | } |
||
949 | elseif (preg_match(self::$em_regex[$closest_marker], $text, $matches)) |
||
950 | { |
||
951 | $markers[] = $closest_marker; |
||
952 | $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
953 | |||
954 | $markup .= '<em>'.$matches[1].'</em>'; |
||
955 | } |
||
956 | |||
957 | if (isset($matches) and $matches) |
||
958 | { |
||
959 | $offset = strlen($matches[0]); |
||
960 | } |
||
961 | else |
||
962 | { |
||
963 | $markup .= $closest_marker; |
||
964 | |||
965 | $offset = 1; |
||
966 | } |
||
967 | |||
968 | break; |
||
969 | |||
970 | case '<': |
||
971 | |||
972 | if (strpos($text, '>') !== false) |
||
973 | { |
||
974 | if ($text[1] === 'h' and preg_match('/^<(https?:[\/]{2}[^\s]+?)>/i', $text, $matches)) |
||
975 | { |
||
976 | $element_url = $matches[1]; |
||
977 | $element_url = str_replace('&', '&', $element_url); |
||
978 | $element_url = str_replace('<', '<', $element_url); |
||
979 | |||
980 | $markup .= '<a href="'.$element_url.'">'.$element_url.'</a>'; |
||
981 | |||
982 | $offset = strlen($matches[0]); |
||
983 | } |
||
984 | elseif (strpos($text, '@') > 1 and preg_match('/<(\S+?@\S+?)>/', $text, $matches)) |
||
985 | { |
||
986 | $markup .= '<a href="mailto:'.$matches[1].'">'.$matches[1].'</a>'; |
||
987 | |||
988 | $offset = strlen($matches[0]); |
||
989 | } |
||
990 | elseif (preg_match('/^<\/?\w.*?>/', $text, $matches)) |
||
991 | { |
||
992 | $markup .= $matches[0]; |
||
993 | |||
994 | $offset = strlen($matches[0]); |
||
995 | } |
||
996 | else |
||
997 | { |
||
998 | $markup .= '<'; |
||
999 | |||
1000 | $offset = 1; |
||
1001 | } |
||
1002 | } |
||
1003 | else |
||
1004 | { |
||
1005 | $markup .= '<'; |
||
1006 | |||
1007 | $offset = 1; |
||
1008 | } |
||
1009 | |||
1010 | break; |
||
1011 | |||
1012 | case '\\': |
||
1013 | |||
1014 | if (in_array($text[1], self::$special_characters)) |
||
1015 | { |
||
1016 | $markup .= $text[1]; |
||
1017 | |||
1018 | $offset = 2; |
||
1019 | } |
||
1020 | else |
||
1021 | { |
||
1022 | $markup .= '\\'; |
||
1023 | |||
1024 | $offset = 1; |
||
1025 | } |
||
1026 | |||
1027 | break; |
||
1028 | |||
1029 | case '`': |
||
1030 | |||
1031 | if (preg_match('/^(`+)[ ]*(.+?)[ ]*(?<!`)\1(?!`)/', $text, $matches)) |
||
1032 | { |
||
1033 | $element_text = $matches[2]; |
||
1034 | $element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8'); |
||
1035 | |||
1036 | $markup .= '<code>'.$element_text.'</code>'; |
||
1037 | |||
1038 | $offset = strlen($matches[0]); |
||
1039 | } |
||
1040 | else |
||
1041 | { |
||
1042 | $markup .= '`'; |
||
1043 | |||
1044 | $offset = 1; |
||
1045 | } |
||
1046 | |||
1047 | break; |
||
1048 | |||
1049 | case 'http': |
||
1050 | |||
1051 | if (preg_match('/^https?:[\/]{2}[^\s]+\b\/*/ui', $text, $matches)) |
||
1052 | { |
||
1053 | $element_url = $matches[0]; |
||
1054 | $element_url = str_replace('&', '&', $element_url); |
||
1055 | $element_url = str_replace('<', '<', $element_url); |
||
1056 | |||
1057 | $markup .= '<a href="'.$element_url.'">'.$element_url.'</a>'; |
||
1058 | |||
1059 | $offset = strlen($matches[0]); |
||
1060 | } |
||
1061 | else |
||
1062 | { |
||
1063 | $markup .= 'http'; |
||
1064 | |||
1065 | $offset = 4; |
||
1066 | } |
||
1067 | |||
1068 | break; |
||
1069 | |||
1070 | case '~~': |
||
1071 | |||
1072 | if (preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $text, $matches)) |
||
1073 | { |
||
1074 | $matches[1] = $this->parse_span_elements($matches[1], $markers); |
||
1075 | |||
1076 | $markup .= '<del>'.$matches[1].'</del>'; |
||
1077 | |||
1078 | $offset = strlen($matches[0]); |
||
1079 | } |
||
1080 | else |
||
1081 | { |
||
1082 | $markup .= '~~'; |
||
1083 | |||
1084 | $offset = 2; |
||
1085 | } |
||
1086 | |||
1087 | break; |
||
1088 | } |
||
1089 | |||
1090 | if (isset($offset)) |
||
1091 | { |
||
1092 | $text = substr($text, $offset); |
||
1093 | } |
||
1094 | |||
1095 | $markers[$closest_marker_index] = $closest_marker; |
||
1096 | } |
||
1097 | |||
1098 | return $markup; |
||
1099 | } |
||
1100 | |||
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.