Completed
Push — master ( 945442...621107 )
by Oscar
05:33
created

CsvTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 32.65 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 49
rs 10
wmc 6
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsCsvEscapeChar() 0 8 2
A fgetcsv() 8 8 2
A fputcsv() 8 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\Utils;
4
5
use Gettext\Translations;
6
7
/*
8
 * Trait to provide the functionality of read/write csv.
9
 */
10
trait CsvTrait
11
{
12
    private static $csvEscapeChar;
13
14
    /**
15
     * Check whether support the escape_char argument to fgetcsv/fputcsv or not
16
     *
17
     * @return bool
18
     */
19
    private static function supportsCsvEscapeChar()
20
    {
21
        if (self::$csvEscapeChar === null) {
22
            self::$csvEscapeChar = version_compare(PHP_VERSION, '5.5.4') >= 0;
23
        }
24
25
        return self::$csvEscapeChar;
26
    }
27
28
    /**
29
     * @param resource $handle
30
     * @param array $options
31
     *
32
     * @return array
33
     */
34 View Code Duplication
    private static function fgetcsv($handle, $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
35
    {
36
        if (self::supportsCsvEscapeChar()) {
37
            return fgetcsv($handle, 0, $options['delimiter'], $options['enclosure'], $options['escape_char']);
38
        }
39
40
        return fgetcsv($handle, 0, $options['delimiter'], $options['enclosure']);
41
    }
42
43
    /**
44
     * @param resource $handle
45
     * @param array $fields
46
     * @param array $options
47
     *
48
     * @return bool|int
49
     */
50 View Code Duplication
    private static function fputcsv($handle, $fields, $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
51
    {
52
        if (self::supportsCsvEscapeChar()) {
53
            return fputcsv($handle, $fields, $options['delimiter'], $options['enclosure'], $options['escape_char']);
54
        }
55
56
        return fputcsv($handle, $fields, $options['delimiter'], $options['enclosure']);
57
    }
58
}
59