Conditions | 14 |
Paths | 624 |
Total Lines | 103 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
173 | function phpmo_write_mo_file($hash, $out) { |
||
174 | // sort by msgid |
||
175 | ksort($hash, SORT_STRING); |
||
176 | // our mo file data |
||
177 | $mo = ''; |
||
178 | // header data |
||
179 | $offsets = array(); |
||
180 | $ids = ''; |
||
181 | $strings = ''; |
||
182 | |||
183 | foreach ($hash as $entry) { |
||
184 | $id = $entry['msgid']; |
||
185 | if (isset($entry['msgid_plural'])) { |
||
186 | $id .= "\x00" . $entry['msgid_plural']; |
||
187 | } |
||
188 | // context is merged into id, separated by EOT (\x04) |
||
189 | if (array_key_exists('msgctxt', $entry)) { |
||
190 | $id = $entry['msgctxt'] . "\x04" . $id; |
||
191 | } |
||
192 | // plural msgstrs are NUL-separated |
||
193 | foreach ($entry['msgstr'] as $key => $msgstr) { |
||
194 | if (!$msgstr) { |
||
195 | unset($entry['msgstr'][$key]); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $str = implode("\x00", $entry['msgstr']); |
||
200 | // keep track of offsets |
||
201 | $offsets[] = array( |
||
202 | strlen( |
||
203 | $ids |
||
204 | ), strlen($id), strlen($strings), strlen($str)); |
||
205 | // plural msgids are not stored (?) |
||
206 | $ids .= $id . "\x00"; |
||
207 | $strings .= $str . "\x00"; |
||
208 | } |
||
209 | |||
210 | // keys start after the header (7 words) + index tables ($#hash * 4 words) |
||
211 | $key_start = 7 * 4 + sizeof($hash) * 4 * 4; |
||
212 | // values start right after the keys |
||
213 | $value_start = $key_start + strlen($ids); |
||
214 | // first all key offsets, then all value offsets |
||
215 | $key_offsets = array(); |
||
216 | $value_offsets = array(); |
||
217 | // calculate |
||
218 | foreach ($offsets as $v) { |
||
219 | list ($o1, $l1, $o2, $l2) = $v; |
||
220 | $key_offsets[] = $l1; |
||
221 | $key_offsets[] = $o1 + $key_start; |
||
222 | $value_offsets[] = $l2; |
||
223 | $value_offsets[] = $o2 + $value_start; |
||
224 | } |
||
225 | $offsets = array_merge($key_offsets, $value_offsets); |
||
226 | |||
227 | // write header |
||
228 | $mo .= pack( |
||
229 | 'Iiiiiii', |
||
230 | 0x950412de, // magic number |
||
231 | 0, // version |
||
232 | sizeof($hash), // number of entries in the catalog |
||
233 | 7 * 4, // key index offset |
||
234 | 7 * 4 + sizeof($hash) * 8, // value index offset, |
||
235 | 0, // hashtable size (unused, thus 0) |
||
236 | $key_start // hashtable offset |
||
237 | ); |
||
238 | // offsets |
||
239 | foreach ($offsets as $offset) { |
||
240 | $mo .= pack('i', $offset); |
||
241 | } |
||
242 | // ids |
||
243 | $mo .= $ids; |
||
244 | // strings |
||
245 | $mo .= $strings; |
||
246 | |||
247 | $directory = dirname($out); |
||
248 | $current_mo_file_name = array_pop(glob($directory . '/*.mo')); |
||
249 | if (!$current_mo_file_name) { |
||
250 | $current_mo_file_name = array_pop(glob($directory . '\*.mo')); |
||
251 | if (!$current_mo_file_name) { |
||
252 | $current_mo_file_name = $out; |
||
253 | } |
||
254 | } |
||
255 | |||
256 | $old_mo_files = glob($directory . '/*.mo'); |
||
257 | if ($old_mo_files && is_array($old_mo_files)) { |
||
258 | foreach ($old_mo_files as $file) { |
||
259 | unlink($file); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | $addition_time = time(); |
||
264 | if (!preg_match('/[\d]+/', $current_mo_file_name)) { |
||
265 | $new_mo_file_name = str_replace('.mo', '_' . $addition_time . '.mo', $current_mo_file_name); |
||
266 | } else { |
||
267 | $new_mo_file_name = preg_replace('/_[\d]+/', '_' . $addition_time, $current_mo_file_name); |
||
268 | } |
||
269 | |||
270 | // rename($current_mo_file_name, $new_mo_file_name); |
||
271 | |||
272 | file_put_contents($new_mo_file_name, $mo); |
||
273 | |||
274 | chmod($new_mo_file_name, 0777); |
||
275 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.