| Conditions | 16 |
| Paths | 12 |
| Total Lines | 78 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 158 | private function loadToArray() |
||
| 159 | { |
||
| 160 | if (!empty($this->chars)) { |
||
| 161 | return; |
||
| 162 | } |
||
| 163 | |||
| 164 | $len = \strlen($this->raw); |
||
| 165 | $inside = false; // are we "inside" of evaluating a valid UTF-8 char? |
||
| 166 | $invalid = false; |
||
| 167 | |||
| 168 | for ($offset = 0; $offset < $len; $offset++) { |
||
| 169 | $char = $this->raw{$offset}; |
||
| 170 | $ord = \ord($char); |
||
| 171 | |||
| 172 | if ($inside === false) { |
||
| 173 | $bytes = self::charLength($ord); |
||
| 174 | |||
| 175 | if ($bytes > 1 && $offset + $bytes <= $len && $invalid === false) { |
||
| 176 | // valid UTF-8 multibyte start |
||
| 177 | $inside = true; |
||
| 178 | $cache = $char; |
||
| 179 | $ordcache = ($ord & self::$spec[$bytes]['mask']) << (6 * ($bytes - 1)); |
||
| 180 | $originOffset = $offset; |
||
| 181 | } elseif ($ord < self::$spec[2]['start']) { |
||
| 182 | // ASCII 7-bit char |
||
| 183 | $this->chars[] = [$char, $ord]; |
||
| 184 | } else { |
||
| 185 | // either C0/C1 block or higher; map from cp1252 to utf8 or just convert |
||
| 186 | $ord = (isset(self::$winc1umap[$ord])) ? self::$winc1umap[$ord] : $ord; |
||
| 187 | $this->chars[] = [self::cpToUtf8Char($ord), $ord]; |
||
| 188 | $invalid = false; |
||
| 189 | } |
||
| 190 | continue; |
||
| 191 | } |
||
| 192 | |||
| 193 | // $inside === true, i.e. *should be* continuation character |
||
| 194 | if (($ord & 0b11000000) !== 0b10000000) { |
||
| 195 | // actually, it's not one, so now the whole UTF-8 char is invalid |
||
| 196 | // go back and force it to parse as ISO or 1252 |
||
| 197 | $inside = false; |
||
| 198 | $invalid = true; |
||
| 199 | $offset = $originOffset - 1; |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | |||
| 203 | // put this byte's data where it needs to go |
||
| 204 | $ordcache |= ($ord & 0b00111111) << (6 * ($bytes - 1 - ($offset - $originOffset))); |
||
| 205 | $cache .= $char; |
||
| 206 | |||
| 207 | if ($originOffset + ($bytes - 1) === $offset) { |
||
| 208 | // we're done parsing this char, now let's verify |
||
| 209 | $inside = false; |
||
| 210 | |||
| 211 | // check for overlong, surrogate, too large, BOM, or C0/C1 |
||
| 212 | $overlong = ($ordcache < self::$spec[$bytes]['start']); |
||
| 213 | $surrogate = ($ordcache & 0xFFFFF800 === 0xD800); |
||
| 214 | $toobig = ($ordcache > 0x10FFFF); |
||
| 215 | |||
| 216 | if ($overlong || $surrogate || $toobig) { |
||
| 217 | $invalid = true; |
||
| 218 | $offset = $originOffset - 1; |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | if ($ordcache === 0xFEFF) { // BOM |
||
| 223 | if ($originOffset !== 0) { |
||
| 224 | // if not at beginning, store as word joiner U+2060 |
||
| 225 | $this->chars[] = [\chr(0xE2) . \chr(0x81) . \chr(0xA0), 0x2060]; |
||
| 226 | } |
||
| 227 | // otherwise discard |
||
| 228 | continue; |
||
| 229 | } |
||
| 230 | |||
| 231 | // verification passed, now store it |
||
| 232 | $this->chars[] = [$cache, $ordcache]; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 238 |