Completed
Pull Request — master (#106)
by
unknown
03:15 queued 01:17
created

CsvDictionary::toString()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 14
nc 6
nop 1
1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
7
class CsvDictionary extends Generator implements GeneratorInterface
8
{
9
    /**
10
     * {@parentDoc}.
11
     */
12
    public static function toString(Translations $translations)
13
    {
14
        $array = PhpArray::toArray($translations);
15
16
        //for a simple json translation dictionary, one domain is supported
17
        $values = current($array);
18
19
        // remove meta / header data
20
        if (array_key_exists('', $values)) {
21
            unset($values['']);
22
        }
23
24
        $handle = fopen('php://memory', 'w');
25
26
        //map to a simple csv dictionary (no plurals)
27
        foreach ($values as $original => $translated) {
0 ignored issues
show
Bug introduced by
The expression $values of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
28
            if (!isset($translated[1])) {
29
                $translated[1] = '';
30
            }
31
            fputcsv($handle, array($original, $translated[1]));
32
        }
33
34
        rewind($handle);
35
        $csv = stream_get_contents($handle);
36
37
        fclose($handle);
38
39
        return $csv;
40
    }
41
}
42