| Conditions | 24 |
| Paths | 248 |
| Total Lines | 135 |
| Code Lines | 95 |
| Lines | 15 |
| Ratio | 11.11 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 39 | public function validate($string, $config, $context) |
||
| 40 | { |
||
| 41 | static $system_fonts = array( |
||
| 42 | 'caption' => true, |
||
| 43 | 'icon' => true, |
||
| 44 | 'menu' => true, |
||
| 45 | 'message-box' => true, |
||
| 46 | 'small-caption' => true, |
||
| 47 | 'status-bar' => true |
||
| 48 | ); |
||
| 49 | |||
| 50 | // regular pre-processing |
||
| 51 | $string = $this->parseCDATA($string); |
||
| 52 | if ($string === '') { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | // check if it's one of the keywords |
||
| 57 | $lowercase_string = strtolower($string); |
||
| 58 | if (isset($system_fonts[$lowercase_string])) { |
||
| 59 | return $lowercase_string; |
||
| 60 | } |
||
| 61 | |||
| 62 | $bits = explode(' ', $string); // bits to process |
||
| 63 | $stage = 0; // this indicates what we're looking for |
||
| 64 | $caught = array(); // which stage 0 properties have we caught? |
||
| 65 | $stage_1 = array('font-style', 'font-variant', 'font-weight'); |
||
| 66 | $final = ''; // output |
||
| 67 | |||
| 68 | for ($i = 0, $size = count($bits); $i < $size; $i++) { |
||
| 69 | if ($bits[$i] === '') { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | switch ($stage) { |
||
| 73 | case 0: // attempting to catch font-style, font-variant or font-weight |
||
| 74 | View Code Duplication | foreach ($stage_1 as $validator_name) { |
|
| 75 | if (isset($caught[$validator_name])) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | $r = $this->info[$validator_name]->validate( |
||
| 79 | $bits[$i], |
||
| 80 | $config, |
||
| 81 | $context |
||
| 82 | ); |
||
| 83 | if ($r !== false) { |
||
| 84 | $final .= $r . ' '; |
||
| 85 | $caught[$validator_name] = true; |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | // all three caught, continue on |
||
| 90 | if (count($caught) >= 3) { |
||
| 91 | $stage = 1; |
||
| 92 | } |
||
| 93 | if ($r !== false) { |
||
| 94 | break; |
||
| 95 | } |
||
| 96 | case 1: // attempting to catch font-size and perhaps line-height |
||
| 97 | $found_slash = false; |
||
| 98 | if (strpos($bits[$i], '/') !== false) { |
||
| 99 | list($font_size, $line_height) = |
||
| 100 | explode('/', $bits[$i]); |
||
| 101 | if ($line_height === '') { |
||
| 102 | // ooh, there's a space after the slash! |
||
| 103 | $line_height = false; |
||
| 104 | $found_slash = true; |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | $font_size = $bits[$i]; |
||
| 108 | $line_height = false; |
||
| 109 | } |
||
| 110 | $r = $this->info['font-size']->validate( |
||
| 111 | $font_size, |
||
| 112 | $config, |
||
| 113 | $context |
||
| 114 | ); |
||
| 115 | if ($r !== false) { |
||
| 116 | $final .= $r; |
||
| 117 | // attempt to catch line-height |
||
| 118 | if ($line_height === false) { |
||
| 119 | // we need to scroll forward |
||
| 120 | for ($j = $i + 1; $j < $size; $j++) { |
||
| 121 | if ($bits[$j] === '') { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | if ($bits[$j] === '/') { |
||
| 125 | if ($found_slash) { |
||
| 126 | return false; |
||
| 127 | } else { |
||
| 128 | $found_slash = true; |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | $line_height = $bits[$j]; |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | // slash already found |
||
| 137 | $found_slash = true; |
||
| 138 | $j = $i; |
||
| 139 | } |
||
| 140 | if ($found_slash) { |
||
| 141 | $i = $j; |
||
| 142 | $r = $this->info['line-height']->validate( |
||
| 143 | $line_height, |
||
| 144 | $config, |
||
| 145 | $context |
||
| 146 | ); |
||
| 147 | if ($r !== false) { |
||
| 148 | $final .= '/' . $r; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | $final .= ' '; |
||
| 152 | $stage = 2; |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | return false; |
||
| 156 | case 2: // attempting to catch font-family |
||
| 157 | $font_family = |
||
| 158 | implode(' ', array_slice($bits, $i, $size - $i)); |
||
| 159 | $r = $this->info['font-family']->validate( |
||
| 160 | $font_family, |
||
| 161 | $config, |
||
| 162 | $context |
||
| 163 | ); |
||
| 164 | if ($r !== false) { |
||
| 165 | $final .= $r . ' '; |
||
| 166 | // processing completed successfully |
||
| 167 | return rtrim($final); |
||
| 168 | } |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | } |
||
| 177 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.