| Conditions | 11 |
| Paths | 16 |
| Total Lines | 66 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 118 | public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{ |
||
| 119 | $length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber)); |
||
| 120 | $charmap = array_flip(self::CHAR_MAP_NUMBER); |
||
| 121 | |||
| 122 | // @todo |
||
| 123 | $toNumericChar = function(int $ord) use ($charmap):string{ |
||
| 124 | |||
| 125 | if(isset($charmap[$ord])){ |
||
| 126 | return $charmap[$ord]; |
||
| 127 | } |
||
| 128 | |||
| 129 | throw new QRCodeDataException('invalid character value: '.$ord); |
||
| 130 | }; |
||
| 131 | |||
| 132 | $result = ''; |
||
| 133 | // Read three digits at a time |
||
| 134 | while($length >= 3){ |
||
| 135 | // Each 10 bits encodes three digits |
||
| 136 | if($bitBuffer->available() < 10){ |
||
| 137 | throw new QRCodeDataException('not enough bits available'); |
||
| 138 | } |
||
| 139 | |||
| 140 | $threeDigitsBits = $bitBuffer->read(10); |
||
| 141 | |||
| 142 | if($threeDigitsBits >= 1000){ |
||
| 143 | throw new QRCodeDataException('error decoding numeric value'); |
||
| 144 | } |
||
| 145 | |||
| 146 | $result .= $toNumericChar($threeDigitsBits / 100); |
||
| 147 | $result .= $toNumericChar(($threeDigitsBits / 10) % 10); |
||
| 148 | $result .= $toNumericChar($threeDigitsBits % 10); |
||
| 149 | |||
| 150 | $length -= 3; |
||
| 151 | } |
||
| 152 | |||
| 153 | if($length === 2){ |
||
| 154 | // Two digits left over to read, encoded in 7 bits |
||
| 155 | if($bitBuffer->available() < 7){ |
||
| 156 | throw new QRCodeDataException('not enough bits available'); |
||
| 157 | } |
||
| 158 | |||
| 159 | $twoDigitsBits = $bitBuffer->read(7); |
||
| 160 | |||
| 161 | if($twoDigitsBits >= 100){ |
||
| 162 | throw new QRCodeDataException('error decoding numeric value'); |
||
| 163 | } |
||
| 164 | |||
| 165 | $result .= $toNumericChar($twoDigitsBits / 10); |
||
| 166 | $result .= $toNumericChar($twoDigitsBits % 10); |
||
| 167 | } |
||
| 168 | elseif($length === 1){ |
||
| 169 | // One digit left over to read |
||
| 170 | if($bitBuffer->available() < 4){ |
||
| 171 | throw new QRCodeDataException('not enough bits available'); |
||
| 172 | } |
||
| 173 | |||
| 174 | $digitBits = $bitBuffer->read(4); |
||
| 175 | |||
| 176 | if($digitBits >= 10){ |
||
| 177 | throw new QRCodeDataException('error decoding numeric value'); |
||
| 178 | } |
||
| 179 | |||
| 180 | $result .= $toNumericChar($digitBits); |
||
| 181 | } |
||
| 182 | |||
| 183 | return $result; |
||
| 184 | } |
||
| 187 |