| Conditions | 14 |
| Paths | 4 |
| Total Lines | 113 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 215 | private function decodeBitStream(array $bytes, Version $version, EccLevel $ecLevel):DecoderResult{ |
||
| 216 | $bits = new BitBuffer($bytes); |
||
| 217 | $symbolSequence = -1; |
||
| 218 | $parityData = -1; |
||
| 219 | $versionNumber = $version->getVersionNumber(); |
||
| 220 | |||
| 221 | $result = ''; |
||
| 222 | $eciCharset = null; |
||
| 223 | # $fc1InEffect = false; |
||
| 224 | |||
| 225 | // While still another segment to read... |
||
| 226 | while($bits->available() >= 4){ |
||
| 227 | $datamode = $bits->read(4); // mode is encoded by 4 bits |
||
| 228 | |||
| 229 | // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here |
||
| 230 | if($datamode === Mode::TERMINATOR){ |
||
| 231 | break; |
||
| 232 | } |
||
| 233 | |||
| 234 | if($datamode === Mode::ECI){ |
||
| 235 | // Count doesn't apply to ECI |
||
| 236 | $eciCharset = ECI::parseValue($bits); |
||
| 237 | } |
||
| 238 | /** @noinspection PhpStatementHasEmptyBodyInspection */ |
||
| 239 | elseif($datamode === Mode::FNC1_FIRST || $datamode === Mode::FNC1_SECOND){ |
||
| 240 | // We do little with FNC1 except alter the parsed result a bit according to the spec |
||
| 241 | # $fc1InEffect = true; |
||
| 242 | } |
||
| 243 | elseif($datamode === Mode::STRCTURED_APPEND){ |
||
| 244 | if($bits->available() < 16){ |
||
| 245 | throw new RuntimeException('structured append: not enough bits left'); |
||
| 246 | } |
||
| 247 | // sequence number and parity is added later to the result metadata |
||
| 248 | // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue |
||
| 249 | $symbolSequence = $bits->read(8); |
||
| 250 | $parityData = $bits->read(8); |
||
| 251 | } |
||
| 252 | else{ |
||
| 253 | // First handle Hanzi mode which does not start with character count |
||
| 254 | /* if($datamode === Mode::DATA_HANZI){ |
||
| 255 | //chinese mode contains a sub set indicator right after mode indicator |
||
| 256 | $subset = $bits->read(4); |
||
| 257 | $length = $bits->read(Mode::getLengthBitsForVersion($datamode, $versionNumber)); |
||
| 258 | if($subset === self::GB2312_SUBSET){ |
||
| 259 | $result .= $this->decodeHanziSegment($bits, $length); |
||
| 260 | } |
||
| 261 | }*/ |
||
| 262 | # else{ |
||
| 263 | // "Normal" QR code modes: |
||
| 264 | if($datamode === Mode::NUMBER){ |
||
| 265 | $result .= Number::decodeSegment($bits, $versionNumber); |
||
| 266 | } |
||
| 267 | elseif($datamode === Mode::ALPHANUM){ |
||
| 268 | $str = AlphaNum::decodeSegment($bits, $versionNumber); |
||
| 269 | |||
| 270 | // See section 6.4.8.1, 6.4.8.2 |
||
| 271 | /* if($fc1InEffect){ |
||
| 272 | $start = \strlen($str); |
||
| 273 | // We need to massage the result a bit if in an FNC1 mode: |
||
| 274 | for($i = $start; $i < $start; $i++){ |
||
| 275 | if($str[$i] === '%'){ |
||
| 276 | if($i < $start - 1 && $str[$i + 1] === '%'){ |
||
| 277 | // %% is rendered as % |
||
| 278 | $str = \substr_replace($str, '', $i + 1, 1);//deleteCharAt(i + 1); |
||
| 279 | } |
||
| 280 | # else{ |
||
| 281 | // In alpha mode, % should be converted to FNC1 separator 0x1D @todo |
||
| 282 | # $str = setCharAt($i, \chr(0x1D)); // ??? |
||
| 283 | # } |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | */ |
||
| 288 | $result .= $str; |
||
| 289 | } |
||
| 290 | elseif($datamode === Mode::BYTE){ |
||
| 291 | $str = Byte::decodeSegment($bits, $versionNumber); |
||
| 292 | |||
| 293 | if($eciCharset !== null){ |
||
| 294 | $encoding = $eciCharset->getName(); |
||
| 295 | |||
| 296 | if($encoding === null){ |
||
| 297 | // The spec isn't clear on this mode; see |
||
| 298 | // section 6.4.5: t does not say which encoding to assuming |
||
| 299 | // upon decoding. I have seen ISO-8859-1 used as well as |
||
| 300 | // Shift_JIS -- without anything like an ECI designator to |
||
| 301 | // give a hint. |
||
| 302 | $encoding = mb_detect_encoding($str, ['ISO-8859-1', 'SJIS', 'UTF-8']); |
||
| 303 | } |
||
| 304 | |||
| 305 | $eciCharset = null; |
||
| 306 | $str = mb_convert_encoding($str, $encoding); |
||
| 307 | } |
||
| 308 | |||
| 309 | $result .= $str; |
||
| 310 | } |
||
| 311 | elseif($datamode === Mode::KANJI){ |
||
| 312 | $result .= Kanji::decodeSegment($bits, $versionNumber); |
||
| 313 | } |
||
| 314 | else{ |
||
| 315 | throw new RuntimeException('invalid data mode'); |
||
| 316 | } |
||
| 317 | # } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | return new DecoderResult([ |
||
| 322 | 'rawBytes' => $bytes, |
||
| 323 | 'text' => $result, |
||
| 324 | 'version' => $version, |
||
| 325 | 'eccLevel' => $ecLevel, |
||
| 326 | 'structuredAppendParity' => $parityData, |
||
| 327 | 'structuredAppendSequence' => $symbolSequence |
||
| 328 | ]); |
||
| 332 |