Conditions | 28 |
Paths | 144 |
Total Lines | 122 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 4 | 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 |
||
19 | public static function fromString($string, Translations $translations = null, $file = '') |
||
20 | { |
||
21 | if ($translations === null) { |
||
22 | $translations = new Translations(); |
||
23 | } |
||
24 | |||
25 | $lines = explode("\n", $string); |
||
26 | $i = 0; |
||
27 | |||
28 | $translation = new Translation('', ''); |
||
29 | |||
30 | for ($n = count($lines); $i < $n; ++$i) { |
||
31 | $line = trim($lines[$i]); |
||
32 | |||
33 | $line = self::fixMultiLines($line, $lines, $i); |
||
34 | |||
35 | if ($line === '') { |
||
36 | if ($translation->is('', '')) { |
||
37 | self::parseHeaders($translation->getTranslation(), $translations); |
||
38 | } elseif ($translation->hasOriginal()) { |
||
39 | $translations[] = $translation; |
||
40 | } |
||
41 | |||
42 | $translation = new Translation('', ''); |
||
43 | continue; |
||
44 | } |
||
45 | |||
46 | $splitLine = preg_split('/\s+/', $line, 2); |
||
47 | $key = $splitLine[0]; |
||
48 | $data = isset($splitLine[1]) ? $splitLine[1] : ''; |
||
49 | |||
50 | switch ($key) { |
||
51 | case '#': |
||
52 | $translation->addComment($data); |
||
53 | $append = null; |
||
54 | break; |
||
55 | |||
56 | case '#.': |
||
57 | $translation->addExtractedComment($data); |
||
58 | $append = null; |
||
59 | break; |
||
60 | |||
61 | case '#,': |
||
62 | foreach (array_map('trim', explode(',', trim($data))) as $value) { |
||
63 | $translation->addFlag($value); |
||
64 | } |
||
65 | $append = null; |
||
66 | break; |
||
67 | |||
68 | case '#:': |
||
69 | foreach (preg_split('/\s+/', trim($data)) as $value) { |
||
70 | if (preg_match('/^(.+)(:(\d*))?$/U', $value, $matches)) { |
||
71 | $translation->addReference($matches[1], isset($matches[3]) ? $matches[3] : null); |
||
72 | } |
||
73 | } |
||
74 | $append = null; |
||
75 | break; |
||
76 | |||
77 | case 'msgctxt': |
||
78 | $translation = $translation->getClone(Strings::fromPo($data)); |
||
79 | $append = 'Context'; |
||
80 | break; |
||
81 | |||
82 | case 'msgid': |
||
83 | $translation = $translation->getClone(null, Strings::fromPo($data)); |
||
84 | $append = 'Original'; |
||
85 | break; |
||
86 | |||
87 | case 'msgid_plural': |
||
88 | $translation->setPlural(Strings::fromPo($data)); |
||
89 | $append = 'Plural'; |
||
90 | break; |
||
91 | |||
92 | case 'msgstr': |
||
93 | case 'msgstr[0]': |
||
94 | $translation->setTranslation(Strings::fromPo($data)); |
||
95 | $append = 'Translation'; |
||
96 | break; |
||
97 | |||
98 | case 'msgstr[1]': |
||
99 | $translation->setPluralTranslation(Strings::fromPo($data), 0); |
||
100 | $append = 'PluralTranslation'; |
||
101 | break; |
||
102 | |||
103 | default: |
||
104 | if (strpos($key, 'msgstr[') === 0) { |
||
105 | $translation->setPluralTranslation(Strings::fromPo($data), intval(substr($key, 7, -1)) - 1); |
||
106 | $append = 'PluralTranslation'; |
||
107 | break; |
||
108 | } |
||
109 | |||
110 | if (isset($append)) { |
||
111 | if ($append === 'Context') { |
||
112 | $translation = $translation->getClone($translation->getContext()."\n".Strings::fromPo($data)); |
||
113 | break; |
||
114 | } |
||
115 | |||
116 | if ($append === 'Original') { |
||
117 | $translation = $translation->getClone(null, $translation->getOriginal()."\n".Strings::fromPo($data)); |
||
118 | break; |
||
119 | } |
||
120 | |||
121 | if ($append === 'PluralTranslation') { |
||
122 | $key = count($translation->getPluralTranslation()) - 1; |
||
123 | $translation->setPluralTranslation($translation->getPluralTranslation($key)."\n".Strings::fromPo($data), $key); |
||
124 | break; |
||
125 | } |
||
126 | |||
127 | $getMethod = 'get'.$append; |
||
128 | $setMethod = 'set'.$append; |
||
129 | $translation->$setMethod($translation->$getMethod()."\n".Strings::fromPo($data)); |
||
130 | } |
||
131 | break; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | if ($translation->hasOriginal() && !in_array($translation, iterator_to_array($translations))) { |
||
136 | $translations[] = $translation; |
||
137 | } |
||
138 | |||
139 | return $translations; |
||
140 | } |
||
141 | |||
206 |