Conditions | 14 |
Paths | 23 |
Total Lines | 87 |
Code Lines | 48 |
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 |
||
185 | public function import_from_reader($reader) |
||
186 | { |
||
187 | $endian_string = $this->get_byteorder($reader->readint32()); |
||
188 | if (false === $endian_string) { |
||
189 | return false; |
||
190 | } |
||
191 | $reader->setEndian($endian_string); |
||
192 | |||
193 | $endian = ('big' == $endian_string) ? 'N' : 'V'; |
||
194 | |||
195 | $header = $reader->read(24); |
||
196 | if ($reader->strlen($header) != 24) { |
||
197 | return false; |
||
198 | } |
||
199 | |||
200 | // parse header |
||
201 | $header = unpack("{$endian}revision/{$endian}total/{$endian}originals_lenghts_addr/{$endian}translations_lenghts_addr/{$endian}hash_length/{$endian}hash_addr", $header); |
||
202 | if (!is_array($header)) { |
||
203 | return false; |
||
204 | } |
||
205 | |||
206 | // support revision 0 of MO format specs, only |
||
207 | if ($header['revision'] != 0) { |
||
208 | return false; |
||
209 | } |
||
210 | |||
211 | // seek to data blocks |
||
212 | $reader->seekto($header['originals_lenghts_addr']); |
||
213 | |||
214 | // read originals' indices |
||
215 | $originals_lengths_length = $header['translations_lenghts_addr'] - $header['originals_lenghts_addr']; |
||
216 | if ($originals_lengths_length != $header['total'] * 8) { |
||
217 | return false; |
||
218 | } |
||
219 | |||
220 | $originals = $reader->read($originals_lengths_length); |
||
221 | if ($reader->strlen($originals) != $originals_lengths_length) { |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | // read translations' indices |
||
226 | $translations_lenghts_length = $header['hash_addr'] - $header['translations_lenghts_addr']; |
||
227 | if ($translations_lenghts_length != $header['total'] * 8) { |
||
228 | return false; |
||
229 | } |
||
230 | |||
231 | $translations = $reader->read($translations_lenghts_length); |
||
232 | if ($reader->strlen($translations) != $translations_lenghts_length) { |
||
233 | return false; |
||
234 | } |
||
235 | |||
236 | // transform raw data into set of indices |
||
237 | $originals = $reader->str_split($originals, 8); |
||
238 | $translations = $reader->str_split($translations, 8); |
||
239 | |||
240 | // skip hash table |
||
241 | $strings_addr = $header['hash_addr'] + $header['hash_length'] * 4; |
||
242 | |||
243 | $reader->seekto($strings_addr); |
||
244 | |||
245 | $strings = $reader->read_all(); |
||
246 | $reader->close(); |
||
247 | |||
248 | for ($i = 0; $i < $header['total']; $i++) { |
||
249 | $o = unpack("{$endian}length/{$endian}pos", $originals[$i]); |
||
250 | $t = unpack("{$endian}length/{$endian}pos", $translations[$i]); |
||
251 | if (!$o || !$t) { |
||
252 | return false; |
||
253 | } |
||
254 | |||
255 | // adjust offset due to reading strings to separate space before |
||
256 | $o['pos'] -= $strings_addr; |
||
257 | $t['pos'] -= $strings_addr; |
||
258 | |||
259 | $original = $reader->substr($strings, $o['pos'], $o['length']); |
||
260 | $translation = $reader->substr($strings, $t['pos'], $t['length']); |
||
261 | |||
262 | if ('' === $original) { |
||
263 | $this->set_headers($this->make_headers($translation)); |
||
264 | } else { |
||
265 | $entry = &$this->make_entry($original, $translation); |
||
266 | $this->entries[$entry->key()] = &$entry; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | return true; |
||
271 | } |
||
272 | |||
315 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.