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