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