Conditions | 15 |
Paths | 2 |
Total Lines | 44 |
Code Lines | 31 |
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 |
||
95 | public function parseAttributes(string $text): array |
||
96 | { |
||
97 | $atts = []; |
||
98 | $patterns = implode('|', [ |
||
99 | '([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)', // attribute="value" |
||
100 | '([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)', // attribute='value' |
||
101 | '([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)', // attribute=value |
||
102 | '"([^"]*)"(?:\s|$)', // "attribute" |
||
103 | '\'([^\']*)\'(?:\s|$)', // 'attribute' |
||
104 | '(\S+)(?:\s|$)', // attribute |
||
105 | ]); |
||
106 | $pattern = "/{$patterns}/"; |
||
107 | $text = preg_replace("/[\x{00a0}\x{200b}]+/u", ' ', $text); |
||
108 | if (preg_match_all($pattern, (string) $text, $match, PREG_SET_ORDER)) { |
||
109 | foreach ($match as $m) { |
||
110 | if (!empty($m[1])) { |
||
111 | $atts[strtolower($m[1])] = stripcslashes($m[2]); |
||
112 | } elseif (!empty($m[3])) { |
||
113 | $atts[strtolower($m[3])] = stripcslashes($m[4]); |
||
114 | } elseif (!empty($m[5])) { |
||
115 | $atts[strtolower($m[5])] = stripcslashes($m[6]); |
||
116 | } elseif (isset($m[7]) && strlen($m[7])) { |
||
117 | $atts[strtolower($m[7])] = true; |
||
118 | } elseif (isset($m[8]) && strlen($m[8])) { |
||
119 | $atts[strtolower($m[8])] = true; |
||
120 | } elseif (isset($m[9])) { |
||
121 | $atts[strtolower($m[9])] = true; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | // Reject any unclosed HTML elements |
||
126 | foreach ($atts as &$value) { |
||
127 | if (!is_string($value)) { |
||
128 | continue; |
||
129 | } |
||
130 | if (strpos($value, '<') !== false) { |
||
131 | if (1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) { |
||
132 | $value = ''; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return $atts; |
||
139 | } |
||
160 |