Conditions | 13 |
Paths | 33 |
Total Lines | 82 |
Code Lines | 51 |
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 |
||
21 | public static function fromString($string, Translations $translations, array $options = []) |
||
22 | { |
||
23 | $stream = new StringReader($string); |
||
24 | $magic = self::readInt($stream, 'V'); |
||
25 | |||
26 | if (($magic === self::MAGIC1) || ($magic === self::MAGIC3)) { //to make sure it works for 64-bit platforms |
||
27 | $byteOrder = 'V'; //low endian |
||
28 | } elseif ($magic === (self::MAGIC2 & 0xFFFFFFFF)) { |
||
29 | $byteOrder = 'N'; //big endian |
||
30 | } else { |
||
31 | throw new Exception('Not MO file'); |
||
32 | } |
||
33 | |||
34 | self::readInt($stream, $byteOrder); |
||
35 | |||
36 | $total = self::readInt($stream, $byteOrder); //total string count |
||
37 | $originals = self::readInt($stream, $byteOrder); //offset of original table |
||
38 | $tran = self::readInt($stream, $byteOrder); //offset of translation table |
||
39 | |||
40 | $stream->seekto($originals); |
||
41 | $table_originals = self::readIntArray($stream, $byteOrder, $total * 2); |
||
42 | |||
43 | $stream->seekto($tran); |
||
44 | $table_translations = self::readIntArray($stream, $byteOrder, $total * 2); |
||
45 | |||
46 | for ($i = 0; $i < $total; ++$i) { |
||
47 | $next = $i * 2; |
||
48 | |||
49 | $stream->seekto($table_originals[$next + 2]); |
||
50 | $original = $stream->read($table_originals[$next + 1]); |
||
51 | |||
52 | $stream->seekto($table_translations[$next + 2]); |
||
53 | $translated = $stream->read($table_translations[$next + 1]); |
||
54 | |||
55 | if ($original === '') { |
||
56 | // Headers |
||
57 | foreach (explode("\n", $translated) as $headerLine) { |
||
58 | if ($headerLine === '') { |
||
59 | continue; |
||
60 | } |
||
61 | |||
62 | $headerChunks = preg_split('/:\s*/', $headerLine, 2); |
||
63 | $translations->setHeader($headerChunks[0], isset($headerChunks[1]) ? $headerChunks[1] : ''); |
||
64 | } |
||
65 | |||
66 | continue; |
||
67 | } |
||
68 | |||
69 | $chunks = explode("\x04", $original, 2); |
||
70 | |||
71 | if (isset($chunks[1])) { |
||
72 | $context = $chunks[0]; |
||
73 | $original = $chunks[1]; |
||
74 | } else { |
||
75 | $context = ''; |
||
76 | } |
||
77 | |||
78 | $chunks = explode("\x00", $original, 2); |
||
79 | |||
80 | if (isset($chunks[1])) { |
||
81 | $original = $chunks[0]; |
||
82 | $plural = $chunks[1]; |
||
83 | } else { |
||
84 | $plural = ''; |
||
85 | } |
||
86 | |||
87 | $translation = $translations->insert($context, $original, $plural); |
||
88 | |||
89 | if ($translated === '') { |
||
90 | continue; |
||
91 | } |
||
92 | |||
93 | if ($plural === '') { |
||
94 | $translation->setTranslation($translated); |
||
95 | continue; |
||
96 | } |
||
97 | |||
98 | $v = explode("\x00", $translated); |
||
99 | $translation->setTranslation(array_shift($v)); |
||
100 | $translation->setPluralTranslations($v); |
||
101 | } |
||
102 | } |
||
103 | |||
129 |