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