Conditions | 19 |
Paths | 62 |
Total Lines | 109 |
Code Lines | 60 |
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 |
||
179 | protected function explodeAttributes($attr) |
||
180 | { |
||
181 | $attrarr = array(); |
||
182 | $mode = 0; |
||
183 | $attrname = ''; |
||
184 | $skip = false; |
||
185 | |||
186 | while (strlen($attr) != 0) { |
||
187 | |||
188 | $working = 0; |
||
189 | |||
190 | switch ($mode) { |
||
191 | case 0: |
||
192 | // Attribute name, href for instance. |
||
193 | if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) { |
||
194 | $attrname = strtolower($match[1]); |
||
195 | $skip = ($attrname == 'style' || substr($attrname, 0, 2) == 'on'); |
||
196 | $working = $mode = 1; |
||
197 | $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr); |
||
198 | } |
||
199 | break; |
||
200 | case 1: |
||
201 | // Equals sign or valueless ("selected"). |
||
202 | if (preg_match('/^\s*=\s*/', $attr)) { |
||
203 | $working = 1; |
||
204 | $mode = 2; |
||
205 | $attr = preg_replace('/^\s*=\s*/', '', $attr); |
||
206 | break; |
||
207 | } |
||
208 | |||
209 | if (preg_match('/^\s+/', $attr)) { |
||
210 | $working = 1; |
||
211 | $mode = 0; |
||
212 | |||
213 | if (!$skip) { |
||
214 | $attrarr[] = $attrname; |
||
215 | } |
||
216 | |||
217 | $attr = preg_replace('/^\s+/', '', $attr); |
||
218 | } |
||
219 | break; |
||
220 | case 2: |
||
221 | // Attribute value, a URL after href= for instance. |
||
222 | if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match)) { |
||
223 | $thisval = $this->badProtocol($match[1]); |
||
224 | |||
225 | if (!$skip) { |
||
226 | $attrarr[] = "$attrname=\"$thisval\""; |
||
227 | } |
||
228 | |||
229 | $working = 1; |
||
230 | $mode = 0; |
||
231 | $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr); |
||
232 | break; |
||
233 | } |
||
234 | |||
235 | if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match)) { |
||
236 | |||
237 | $thisval = $this->badProtocol($match[1]); |
||
238 | |||
239 | if (!$skip) { |
||
240 | $attrarr[] = "$attrname='$thisval'"; |
||
241 | } |
||
242 | |||
243 | $working = 1; |
||
244 | $mode = 0; |
||
245 | $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr); |
||
246 | break; |
||
247 | } |
||
248 | |||
249 | if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match)) { |
||
250 | $thisval = $this->badProtocol($match[1]); |
||
251 | |||
252 | if (!$skip) { |
||
253 | $attrarr[] = "$attrname=\"$thisval\""; |
||
254 | } |
||
255 | |||
256 | $working = 1; |
||
257 | $mode = 0; |
||
258 | $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr); |
||
259 | } |
||
260 | break; |
||
261 | } |
||
262 | |||
263 | if ($working == 0) { |
||
264 | // Not well formed; remove and try again. |
||
265 | $attr = preg_replace('/ |
||
266 | ^ |
||
267 | ( |
||
268 | "[^"]*("|$) # - a string that starts with a double quote, up until the next double quote or the end of the string |
||
269 | | # or |
||
270 | \'[^\']*(\'|$)| # - a string that starts with a quote, up until the next quote or the end of the string |
||
271 | | # or |
||
272 | \S # - a non-whitespace character |
||
273 | )* # any number of the above three |
||
274 | \s* # any number of whitespaces |
||
275 | /x', '', $attr); |
||
276 | |||
277 | $mode = 0; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | // The attribute list ends with a valueless attribute like "selected". |
||
282 | if ($mode == 1 && !$skip) { |
||
283 | $attrarr[] = $attrname; |
||
284 | } |
||
285 | |||
286 | return $attrarr; |
||
287 | } |
||
288 | |||
333 |