Po   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 192
Duplicated Lines 7.29 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 14
loc 192
rs 9
c 0
b 0
f 0
wmc 35
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
D fromString() 14 123 27
B fixMultiLines() 0 16 5
B convertString() 0 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use Gettext\Utils\HeadersExtractorTrait;
8
9
/**
10
 * Class to get gettext strings from php files returning arrays.
11
 */
12
class Po extends Extractor implements ExtractorInterface
13
{
14
    use HeadersExtractorTrait;
15
16
    /**
17
     * Parses a .po file and append the translations found in the Translations instance.
18
     *
19
     * {@inheritdoc}
20
     */
21
    public static function fromString($string, Translations $translations, array $options = [])
22
    {
23
        $lines = explode("\n", $string);
24
        $i = 0;
25
26
        $translation = new Translation('', '');
27
28
        for ($n = count($lines); $i < $n; ++$i) {
29
            $line = trim($lines[$i]);
30
            $line = self::fixMultiLines($line, $lines, $i);
31
32
            if ($line === '') {
33
                if ($translation->is('', '')) {
34
                    self::extractHeaders($translation->getTranslation(), $translations);
35
                } elseif ($translation->hasOriginal()) {
36
                    $translations[] = $translation;
37
                }
38
39
                $translation = new Translation('', '');
40
                continue;
41
            }
42
43
            $splitLine = preg_split('/\s+/', $line, 2);
44
            $key = $splitLine[0];
45
            $data = isset($splitLine[1]) ? $splitLine[1] : '';
46
47
            switch ($key) {
48
                case '#':
49
                    $translation->addComment($data);
50
                    $append = null;
51
                    break;
52
53
                case '#.':
54
                    $translation->addExtractedComment($data);
55
                    $append = null;
56
                    break;
57
58
                case '#,':
59
                    foreach (array_map('trim', explode(',', trim($data))) as $value) {
60
                        $translation->addFlag($value);
61
                    }
62
                    $append = null;
63
                    break;
64
65
                case '#:':
66
                    foreach (preg_split('/\s+/', trim($data)) as $value) {
67
                        if (preg_match('/^(.+)(:(\d*))?$/U', $value, $matches)) {
68
                            $translation->addReference($matches[1], isset($matches[3]) ? $matches[3] : null);
69
                        }
70
                    }
71
                    $append = null;
72
                    break;
73
74
                case 'msgctxt':
75
                    $translation = $translation->getClone(self::convertString($data));
76
                    $append = 'Context';
77
                    break;
78
79
                case 'msgid':
80
                    $translation = $translation->getClone(null, self::convertString($data));
81
                    $append = 'Original';
82
                    break;
83
84
                case 'msgid_plural':
85
                    $translation->setPlural(self::convertString($data));
86
                    $append = 'Plural';
87
                    break;
88
89
                case 'msgstr':
90
                case 'msgstr[0]':
91
                    $translation->setTranslation(self::convertString($data));
92
                    $append = 'Translation';
93
                    break;
94
95
                case 'msgstr[1]':
96
                    $translation->setPluralTranslations([self::convertString($data)]);
97
                    $append = 'PluralTranslation';
98
                    break;
99
100
                default:
101 View Code Duplication
                    if (strpos($key, 'msgstr[') === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
                        $p = $translation->getPluralTranslations();
103
                        $p[] = self::convertString($data);
104
105
                        $translation->setPluralTranslations($p);
106
                        $append = 'PluralTranslation';
107
                        break;
108
                    }
109
110
                    if (isset($append)) {
111
                        if ($append === 'Context') {
112
                            $translation = $translation->getClone($translation->getContext()
113
                                ."\n"
114
                                .self::convertString($data));
115
                            break;
116
                        }
117
118
                        if ($append === 'Original') {
119
                            $translation = $translation->getClone(null, $translation->getOriginal()
120
                                ."\n"
121
                                .self::convertString($data));
122
                            break;
123
                        }
124
125 View Code Duplication
                        if ($append === 'PluralTranslation') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
                            $p = $translation->getPluralTranslations();
127
                            $p[] = array_pop($p)."\n".self::convertString($data);
128
                            $translation->setPluralTranslations($p);
129
                            break;
130
                        }
131
132
                        $getMethod = 'get'.$append;
133
                        $setMethod = 'set'.$append;
134
                        $translation->$setMethod($translation->$getMethod()."\n".self::convertString($data));
135
                    }
136
                    break;
137
            }
138
        }
139
140
        if ($translation->hasOriginal() && !in_array($translation, iterator_to_array($translations))) {
141
            $translations[] = $translation;
142
        }
143
    }
144
145
    /**
146
     * Gets one string from multiline strings.
147
     *
148
     * @param string $line
149
     * @param array  $lines
150
     * @param int    &$i
151
     *
152
     * @return string
153
     */
154
    private static function fixMultiLines($line, array $lines, &$i)
155
    {
156
        for ($j = $i, $t = count($lines); $j < $t; ++$j) {
157
            if (substr($line, -1, 1) == '"'
158
                && isset($lines[$j + 1])
159
                && substr(trim($lines[$j + 1]), 0, 1) == '"'
160
            ) {
161
                $line = substr($line, 0, -1).substr(trim($lines[$j + 1]), 1);
162
            } else {
163
                $i = $j;
164
                break;
165
            }
166
        }
167
168
        return $line;
169
    }
170
171
    /**
172
     * Convert a string from its PO representation.
173
     *
174
     * @param string $value
175
     *
176
     * @return string
177
     */
178
    public static function convertString($value)
179
    {
180
        if (!$value) {
181
            return '';
182
        }
183
184
        if ($value[0] === '"') {
185
            $value = substr($value, 1, -1);
186
        }
187
188
        return strtr(
189
            $value,
190
            [
191
                '\\\\' => '\\',
192
                '\\a' => "\x07",
193
                '\\b' => "\x08",
194
                '\\t' => "\t",
195
                '\\n' => "\n",
196
                '\\v' => "\x0b",
197
                '\\f' => "\x0c",
198
                '\\r' => "\r",
199
                '\\"' => '"',
200
            ]
201
        );
202
    }
203
}
204