Conditions | 52 |
Paths | 2624 |
Total Lines | 165 |
Code Lines | 105 |
Lines | 30 |
Ratio | 18.18 % |
Tests | 37 |
CRAP Score | 1060.0015 |
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 |
||
285 | 32 | public static function cleanup($body, $config) |
|
286 | { |
||
287 | |||
288 | 32 | $scriptParts = array(); |
|
289 | |||
290 | do { |
||
291 | 32 | $prefix = md5(rand()); |
|
292 | 32 | } while (strpos($body, $prefix) !== false); |
|
293 | |||
294 | 32 | $callback = function($matches) use ($prefix, &$scriptParts) { |
|
295 | $scriptPartKey = '----'.$prefix . '-' . count($scriptParts).'----'; |
||
296 | $scriptParts[$scriptPartKey] = $matches[0]; |
||
297 | return $scriptPartKey; |
||
298 | 32 | }; |
|
299 | |||
300 | 32 | $newbody = preg_replace_callback('!<script[^>]*>(.|[\r\n])*?</[^>]*script[^>]*>!i', $callback, $body); |
|
301 | |||
302 | 32 | if($newbody) { |
|
303 | 32 | $body = $newbody; |
|
304 | 32 | } |
|
305 | |||
306 | 32 | $body = "<htmlcleaner>$body</htmlcleaner>"; |
|
307 | 32 | $rewrite_rules = $config["rewrite"]; |
|
308 | 32 | $return = ''; |
|
309 | 32 | $parts = htmlcleaner::dessicate($body); |
|
310 | |||
311 | // flip emtied rules so we can use it as indexes |
||
312 | 32 | if (is_array($config["delete_emptied"])) { |
|
313 | $config["delete_emptied"] = array_flip($config["delete_emptied"]); |
||
314 | } |
||
315 | 32 | if (isset($config["delete_empty_containers"]) && is_array($config["delete_empty_containers"])) { |
|
316 | $config["delete_empty_containers"] = array_flip($config["delete_empty_containers"]); |
||
317 | } |
||
318 | 32 | $delete_stack = Array(); |
|
319 | 32 | $skipNodes = 0; |
|
320 | 32 | if(is_array($rewrite_rules)) { |
|
321 | foreach ($rewrite_rules as $tag_rule=> $attrib_rules) { |
||
322 | $escaped_rule = str_replace('/','\/',$tag_rule); |
||
323 | if($tag_rule !== $escaped_rule) { |
||
324 | $rewrite_rules[$escaped_rule] = $attrib_rules; |
||
325 | unset($rewrite_rules[$tag_rule]); |
||
326 | $tag_rule = $escaped_rule; |
||
327 | } |
||
328 | |||
329 | if (is_array($attrib_rules)) { |
||
330 | foreach ($attrib_rules as $attrib_rule=> $value_rules) { |
||
331 | $escaped_rule = str_replace('/','\/',$attrib_rule); |
||
332 | if ($attrib_rule !== $escaped_rule) { |
||
333 | $rewrite_rules[$tag_rule][$escaped_rule] = $value_rules; |
||
334 | unset($rewrite_rules[$tag_rule][$attrib_rule]); |
||
335 | $attrib_rule = $escaped_rule; |
||
336 | } |
||
337 | |||
338 | if (is_array($value_rules)) { |
||
339 | foreach ($value_rules as $value_rule=>$value) { |
||
340 | $escaped_rule = str_replace('/','\/',$value_rule); |
||
341 | if ($value_rule !== $escaped_rule) { |
||
342 | $rewrite_rules[$tag_rule][$attrib_rule][$escaped_rule] = $value; |
||
343 | unset($rewrite_rules[$tag_rule][$attrib_rule][$value_rule]); |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | 32 | foreach ($parts as $i => $part) { |
|
353 | 32 | if ($skipNodes > 0) { |
|
354 | $skipNodes--; |
||
355 | continue; |
||
356 | } |
||
357 | 32 | if ($part->nodeType == HTML_CLEANER_NODE_CLOSINGSTYLE_NONE) { |
|
358 | 32 | if (isset($config["delete_emptied"][$part->nodeName]) |
|
359 | 32 | && count($delete_stack)) { |
|
360 | do { |
||
361 | $closed = array_pop($delete_stack); |
||
362 | } while ($closed["tag"] && $closed["tag"] != $part->nodeName); |
||
363 | if ($closed["delete"]) { |
||
364 | unset($part); |
||
365 | } |
||
366 | } |
||
367 | 32 | } else |
|
368 | 32 | if ($part->nodeType == HTML_CLEANER_NODE_NODETYPE_NODE) { |
|
369 | 32 | if (isset($config["delete_emptied"][$part->nodeName]) |
|
370 | 32 | && count($delete_stack)) { |
|
371 | array_push($delete_stack, Array("tag" => $part->nodeName)); |
||
372 | 32 | } else if (isset($config["delete_empty_containers"][$part->nodeName])) { |
|
373 | if ($part->nodeName != 'a' || !$part->attributes['name']) { // named anchor objects are not containers |
||
374 | if (isset($parts[$i+1]) && $parts[$i+1]->nodeName == $part->nodeName && $parts[$i+1]->nodeType == HTML_CLEANER_NODE_NODETYPE_CLOSINGNODE) { |
||
375 | $skipNodes = 1; |
||
376 | continue; |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | 32 | } |
|
381 | |||
382 | |||
383 | 32 | if ($part && is_array($rewrite_rules)) { |
|
384 | foreach ($rewrite_rules as $tag_rule=>$attrib_rules) { |
||
385 | if (preg_match('/'.$tag_rule.'/is', $part->nodeName)) { |
||
386 | if (is_array($attrib_rules)) { |
||
387 | foreach ($attrib_rules as $attrib_rule=>$value_rules) { |
||
388 | foreach ($part->attributes as $attrib_key=>$attrib_val) { |
||
389 | if (preg_match('/'.$attrib_rule.'/is', $attrib_key)) { |
||
390 | if (is_array($value_rules)) { |
||
391 | foreach ($value_rules as $value_rule=>$value) { |
||
392 | if (preg_match('/'.$value_rule.'/is', $attrib_val)) { |
||
393 | View Code Duplication | if ($value === false) { |
|
394 | unset($part->attributes[$attrib_key]); |
||
395 | if (!count($part->attributes)) { |
||
396 | if (isset($config["delete_emptied"][$part->nodeName])) { |
||
397 | // remove previous config |
||
398 | @array_pop($delete_stack); |
||
399 | array_push($delete_stack, Array("tag" => $part->nodeName, "delete" => true)); |
||
400 | unset($part); |
||
401 | } |
||
402 | break 3; |
||
403 | } |
||
404 | } else { |
||
405 | $part->attributes[$attrib_key] = preg_replace('/^'.$value_rule.'$/is', $value, $part->attributes[$attrib_key]); |
||
406 | } |
||
407 | } |
||
408 | } |
||
409 | View Code Duplication | } else |
|
410 | if ($value_rules === false) { |
||
411 | unset($part->attributes[$attrib_key]); |
||
412 | if (!count($part->attributes)) { |
||
413 | if (isset($config["delete_emptied"][$part->nodeName])) { |
||
414 | // remove previous config |
||
415 | @array_pop($delete_stack); |
||
416 | array_push($delete_stack, Array("tag" => $part->nodeName, "delete" => true)); |
||
417 | unset($part); |
||
418 | } |
||
419 | break 2; |
||
420 | } |
||
421 | } else { |
||
422 | $part->attributes[preg_replace('/^'.$attrib_rule.'$/is', $value_rules, $attrib_key)] = $part->attributes[$attrib_key]; |
||
423 | unset($part->attributes[$attrib_key]); |
||
424 | } |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | } else if ($attrib_rules === false) { |
||
429 | unset($part); |
||
430 | } else { |
||
431 | $part->nodeName = $attrib_rules; |
||
432 | } |
||
433 | break; // tag matched, so skip next rules. |
||
434 | } |
||
435 | } |
||
436 | } |
||
437 | 32 | if ($part && strstr($part->nodeValue,'<?xml:namespace')===false) { |
|
438 | 32 | $return .= $part->toString(); |
|
439 | 32 | } |
|
440 | 32 | } |
|
441 | |||
442 | 32 | $return = str_replace(array_keys($scriptParts), array_values($scriptParts), $return); |
|
443 | |||
444 | //FIXME: htmlcleaner removes the '<' in '</htmlcleaner>' if the html code is broken |
||
445 | // ie: if the last tag in the input isn't properly closed... it should instead |
||
446 | // close any broken tag properly (add quotes and a '>') |
||
447 | |||
448 | 32 | return str_replace('<htmlcleaner>', '', str_replace('</htmlcleaner>', '', $return)); |
|
449 | } |
||
450 | } |
||
462 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.