| Conditions | 14 |
| Paths | 124 |
| Total Lines | 85 |
| Code Lines | 48 |
| 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 |
||
| 23 | public function validate($css, $config, $context) |
||
| 24 | { |
||
| 25 | $css = $this->parseCDATA($css); |
||
| 26 | |||
| 27 | $definition = $config->getCSSDefinition(); |
||
| 28 | $allow_duplicates = $config->get("CSS.AllowDuplicates"); |
||
| 29 | |||
| 30 | // we're going to break the spec and explode by semicolons. |
||
| 31 | // This is because semicolon rarely appears in escaped form |
||
| 32 | // Doing this is generally flaky but fast |
||
| 33 | // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI |
||
| 34 | // for details |
||
| 35 | |||
| 36 | $declarations = explode(';', $css); |
||
| 37 | $propvalues = array(); |
||
| 38 | $new_declarations = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Name of the current CSS property being validated. |
||
| 42 | */ |
||
| 43 | $property = false; |
||
| 44 | $context->register('CurrentCSSProperty', $property); |
||
| 45 | |||
| 46 | foreach ($declarations as $declaration) { |
||
| 47 | if (!$declaration) { |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | if (!strpos($declaration, ':')) { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | list($property, $value) = explode(':', $declaration, 2); |
||
| 54 | $property = trim($property); |
||
| 55 | $value = trim($value); |
||
| 56 | $ok = false; |
||
| 57 | do { |
||
| 58 | if (isset($definition->info[$property])) { |
||
| 59 | $ok = true; |
||
| 60 | break; |
||
| 61 | } |
||
| 62 | if (ctype_lower($property)) { |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | $property = strtolower($property); |
||
| 66 | if (isset($definition->info[$property])) { |
||
| 67 | $ok = true; |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | } while (0); |
||
| 71 | if (!$ok) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | // inefficient call, since the validator will do this again |
||
| 75 | if (strtolower(trim($value)) !== 'inherit') { |
||
| 76 | // inherit works for everything (but only on the base property) |
||
| 77 | $result = $definition->info[$property]->validate( |
||
| 78 | $value, |
||
| 79 | $config, |
||
| 80 | $context |
||
| 81 | ); |
||
| 82 | } else { |
||
| 83 | $result = 'inherit'; |
||
| 84 | } |
||
| 85 | if ($result === false) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | if ($allow_duplicates) { |
||
| 89 | $new_declarations .= "$property:$result;"; |
||
| 90 | } else { |
||
| 91 | $propvalues[$property] = $result; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $context->destroy('CurrentCSSProperty'); |
||
| 96 | |||
| 97 | // procedure does not write the new CSS simultaneously, so it's |
||
| 98 | // slightly inefficient, but it's the only way of getting rid of |
||
| 99 | // duplicates. Perhaps config to optimize it, but not now. |
||
| 100 | |||
| 101 | foreach ($propvalues as $prop => $value) { |
||
| 102 | $new_declarations .= "$prop:$value;"; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $new_declarations ? $new_declarations : false; |
||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 112 |