CsvIterator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 9.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 10
loc 102
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPath() 0 5 1
A getPath() 0 4 1
A rewind() 10 10 2
A current() 0 4 1
A valid() 0 4 1
A getSeparator() 0 4 1
A setSeparator() 0 5 1
A key() 0 4 1
A next() 0 10 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
namespace FMUP\Import\Iterator;
3
4
use FMUP\Import\Exception;
5
6
/**
7
 * Permet de parcourir un csv ligne par ligne
8
 *
9
 * @author jyamin
10
 *
11
 */
12
class CsvIterator implements \Iterator
13
{
14
    const COMMA_SEPARATOR = ',';
15
    const SEMICOLON_SEPARATOR = ';';
16
    const TABULATION_SEPARATOR = "\t";
17
18
    protected $path;
19
    private $fHandle;
20
    private $current;
21
    private $line;
22
    private $separator;
23
24
    /**
25
     * @param string $path
26
     * @param string $separator optional (if null, autodetect)
27
     */
28 3
    public function __construct($path = "", $separator = self::SEMICOLON_SEPARATOR)
29
    {
30 3
        $this->setPath($path)->setSeparator($separator);
31 3
    }
32
33
    /**
34
     * @param string $path
35
     * @return $this
36
     */
37 3
    public function setPath($path)
38
    {
39 3
        $this->path = (string)$path;
40 3
        return $this;
41
    }
42
43 1
    public function getPath()
44
    {
45 1
        return $this->path;
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51 2 View Code Duplication
    public function rewind()
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...
52
    {
53 2
        if (!file_exists($this->path)) {
54 1
            throw new Exception("Le fichier specifie n'existe pas ou est introuvable");
55
        }
56 1
        $this->fHandle = fopen($this->path, "r");
57 1
        rewind($this->fHandle);
58 1
        $this->next();
59 1
        $this->line = 0;
60 1
    }
61
62
    /**
63
     * @return mixed
64
     */
65 1
    public function current()
66
    {
67 1
        return $this->current;
68
    }
69
70 1
    public function next()
71
    {
72 1
        if (feof($this->fHandle)) {
73 1
            fclose($this->fHandle);
74 1
            $this->current = null;
75
        } else {
76 1
            $this->current = fgetcsv($this->fHandle, 0, $this->getSeparator());
77
        }
78 1
        $this->line++;
79 1
    }
80
81
    /**
82
     * @return bool
83
     */
84 1
    public function valid()
85
    {
86 1
        return !is_null($this->current());
87
    }
88
89
    /**
90
     * Get separator
91
     * @return string
92
     */
93 2
    public function getSeparator()
94
    {
95 2
        return $this->separator;
96
    }
97
98
    /**
99
     * Set separator
100
     * @param string $separator
101
     * @return self
102
     */
103 3
    public function setSeparator($separator)
104
    {
105 3
        $this->separator = (string)$separator;
106 3
        return $this;
107
    }
108
109 1
    public function key()
110
    {
111 1
        return $this->line;
112
    }
113
}
114