Po::toString()   D
last analyzed

Complexity

Conditions 16
Paths 260

Size

Total Lines 60
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 32
nc 260
nop 2
dl 0
loc 60
rs 4.642
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
7
class Po extends Generator implements GeneratorInterface
8
{
9
    public static $options = [
10
        'noLocation' => false,
11
    ];
12
13
    /**
14
     * {@parentDoc}.
15
     */
16
    public static function toString(Translations $translations, array $options = [])
17
    {
18
        $options += static::$options;
19
20
        $pluralForm = $translations->getPluralForms();
21
        $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null;
22
        $lines = ['msgid ""', 'msgstr ""'];
23
24
        foreach ($translations->getHeaders() as $name => $value) {
25
            $lines[] = sprintf('"%s: %s\\n"', $name, $value);
26
        }
27
28
        $lines[] = '';
29
30
        //Translations
31
        foreach ($translations as $translation) {
32
            if ($translation->hasComments()) {
33
                foreach ($translation->getComments() as $comment) {
34
                    $lines[] = '# '.$comment;
35
                }
36
            }
37
38
            if ($translation->hasExtractedComments()) {
39
                foreach ($translation->getExtractedComments() as $comment) {
40
                    $lines[] = '#. '.$comment;
41
                }
42
            }
43
44
            if (!$options['noLocation'] && $translation->hasReferences()) {
45
                foreach ($translation->getReferences() as $reference) {
46
                    $lines[] = '#: '.$reference[0].(!is_null($reference[1]) ? ':'.$reference[1] : null);
47
                }
48
            }
49
50
            if ($translation->hasFlags()) {
51
                $lines[] = '#, '.implode(',', $translation->getFlags());
52
            }
53
54
            if ($translation->hasContext()) {
55
                $lines[] = 'msgctxt '.self::convertString($translation->getContext());
56
            }
57
58
            self::addLines($lines, 'msgid', $translation->getOriginal());
59
60
            if ($translation->hasPlural()) {
61
                self::addLines($lines, 'msgid_plural', $translation->getPlural());
62
                self::addLines($lines, 'msgstr[0]', $translation->getTranslation());
63
64
                foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) {
65
                    self::addLines($lines, 'msgstr['.($k + 1).']', $v);
66
                }
67
            } else {
68
                self::addLines($lines, 'msgstr', $translation->getTranslation());
69
            }
70
71
            $lines[] = '';
72
        }
73
74
        return implode("\n", $lines);
75
    }
76
77
    /**
78
     * Escapes and adds double quotes to a string.
79
     *
80
     * @param string $string
81
     *
82
     * @return string
83
     */
84
    private static function multilineQuote($string)
85
    {
86
        $lines = explode("\n", $string);
87
        $last = count($lines) - 1;
88
89
        foreach ($lines as $k => $line) {
90
            if ($k === $last) {
91
                $lines[$k] = self::convertString($line);
92
            } else {
93
                $lines[$k] = self::convertString($line."\n");
94
            }
95
        }
96
97
        return $lines;
98
    }
99
100
    /**
101
     * Add one or more lines depending whether the string is multiline or not.
102
     *
103
     * @param array  &$lines
104
     * @param string $name
105
     * @param string $value
106
     */
107
    private static function addLines(array &$lines, $name, $value)
108
    {
109
        $newLines = self::multilineQuote($value);
110
111
        if (count($newLines) === 1) {
112
            $lines[] = $name.' '.$newLines[0];
113
        } else {
114
            $lines[] = $name.' ""';
115
116
            foreach ($newLines as $line) {
117
                $lines[] = $line;
118
            }
119
        }
120
    }
121
122
    /**
123
     * Convert a string to its PO representation.
124
     *
125
     * @param string $value
126
     *
127
     * @return string
128
     */
129
    public static function convertString($value)
130
    {
131
        return '"'.strtr(
132
            $value,
133
            [
134
                "\x00" => '',
135
                '\\' => '\\\\',
136
                "\t" => '\t',
137
                "\n" => '\n',
138
                '"' => '\\"',
139
            ]
140
        ).'"';
141
    }
142
}
143