Completed
Push — master ( aa8bc7...945442 )
by Oscar
02:20
created

Csv   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 5.36 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 3
loc 56
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B fromString() 0 27 5
A getcsv() 3 8 2

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\Utils\HeadersExtractorTrait;
7
8
/**
9
 * Class to get gettext strings from csv.
10
 */
11
class Csv extends Extractor implements ExtractorInterface
12
{
13
    use HeadersExtractorTrait;
14
15
    public static $options = [
16
        'delimiter' => ",",
17
        'enclosure' => '"',
18
        'escape_char' => "\\"
19
    ];
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public static function fromString($string, Translations $translations, array $options = [])
25
    {
26
        $options += static::$options;
27
        $handle = fopen('php://memory', 'w');
28
29
        fputs($handle, $string);
30
        rewind($handle);
31
32
        while ($row = self::getcsv($handle, $options)) {
33
            $context = array_shift($row);
34
            $original = array_shift($row);
35
36
            if ($context === '' && $original === '') {
37
                self::extractHeaders(array_shift($row), $translations);
38
                continue;
39
            }
40
41
            $translation = $translations->insert($context, $original);
42
43
            if (!empty($row)) {
44
                $translation->setTranslation(array_shift($row));
45
                $translation->setPluralTranslations($row);
46
            }
47
        }
48
49
        fclose($handle);
50
    }
51
52
    /**
53
     * @param resource $handle
54
     * @param array $options
55
     *
56
     * @return array
57
     */
58
    private static function getcsv($handle, $options)
59
    {
60 View Code Duplication
        if (version_compare(PHP_VERSION, '5.3.0') >= 0) { // >= 5.3
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...
61
            return fgetcsv($handle, null, $options['delimiter'], $options['enclosure'], $options['escape_char']);
62
        }
63
64
        return fgetcsv($handle, null, $options['delimiter'], $options['enclosure']);
65
    }
66
}
67