Conditions | 16 |
Paths | 772 |
Total Lines | 63 |
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 |
||
9 | public function generateString(Translations $translations): string |
||
10 | { |
||
11 | $pluralForm = $translations->getHeaders()->getPluralForm(); |
||
12 | $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
||
13 | |||
14 | //Headers |
||
15 | $lines = ['msgid ""', 'msgstr ""']; |
||
16 | |||
17 | foreach ($translations->getHeaders() as $name => $value) { |
||
18 | $lines[] = sprintf('"%s: %s\\n"', $name, $value); |
||
19 | } |
||
20 | |||
21 | $lines[] = ''; |
||
22 | |||
23 | //Translations |
||
24 | foreach ($translations as $translation) { |
||
25 | foreach ($translation->getComments() as $comment) { |
||
26 | $lines[] = sprintf('# %s', $comment); |
||
27 | } |
||
28 | |||
29 | foreach ($translation->getExtractedComments() as $comment) { |
||
30 | $lines[] = sprintf('#. %s', $comment); |
||
31 | } |
||
32 | |||
33 | foreach ($translation->getReferences() as $filename => $lineNumbers) { |
||
34 | if (empty($lineNumbers)) { |
||
35 | $lines[] = sprintf('#: %s', $filename); |
||
36 | continue; |
||
37 | } |
||
38 | |||
39 | foreach ($lineNumbers as $number) { |
||
40 | $lines[] = sprintf('#: %s:%d', $filename, $number); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | if (count($translation->getFlags())) { |
||
45 | $lines[] = sprintf('#, %s', implode(',', $translation->getFlags()->toArray())); |
||
46 | } |
||
47 | |||
48 | $prefix = $translation->isDisabled() ? '#~ ' : ''; |
||
49 | |||
50 | if ($context = $translation->getContext()) { |
||
51 | $lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context)); |
||
52 | } |
||
53 | |||
54 | self::appendLines($lines, $prefix, 'msgid', $translation->getOriginal()); |
||
55 | |||
56 | if ($plural = $translation->getPlural()) { |
||
57 | self::appendLines($lines, $prefix, 'msgid_plural', $plural); |
||
58 | self::appendLines($lines, $prefix, 'msgstr[0]', $translation->getTranslation() ?: ''); |
||
59 | |||
60 | foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { |
||
61 | self::appendLines($lines, $prefix, sprintf('msgstr[%d]', $k + 1), $v); |
||
62 | } |
||
63 | } else { |
||
64 | self::appendLines($lines, $prefix, 'msgstr', $translation->getTranslation() ?: ''); |
||
65 | } |
||
66 | |||
67 | $lines[] = ''; |
||
68 | } |
||
69 | |||
70 | return implode("\n", $lines); |
||
71 | } |
||
72 | |||
115 |