Completed
Pull Request — master (#106)
by
unknown
01:55
created

CsvDictionary   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B fromString() 0 25 5
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
7
/**
8
 * Class to get gettext strings from plain json.
9
 */
10
class CsvDictionary extends Extractor implements ExtractorInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function fromString($string, Translations $translations = null, $file = '')
16
    {
17
        if ($translations === null) {
18
            $translations = new Translations();
19
        }
20
21
        $tmpFile = tempnam(sys_get_temp_dir(), 'gettext_');
22
        file_put_contents($tmpFile, $string);
23
        $handle = fopen($tmpFile, 'r');
24
25
        $entries = [];
26
        while ($row = fgetcsv($handle)) {
27
            $entries[$row[0]] = $row[1];
28
        }
29
30
        fclose($handle);
31
32
        if ($entries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            foreach ($entries as $original => $translation) {
34
                $translations->insert(null, $original)->setTranslation($translation);
35
            }
36
        }
37
38
        return $translations;
39
    }
40
}
41