CsvTrait   A
last analyzed

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