Csv::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 7
rs 10
ccs 0
cts 6
cp 0
crap 12
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl\Extract\File;
4
5
use Oliverde8\Component\PhpEtl\Model\File\AbstractCsvFile;
6
7
/**
8
 * Class Csv
9
 *
10
 * @author    de Cramer Oliver<[email protected]>
11
 * @copyright 2018 Oliverde8
12
 * @package Oliverde8\Component\PhpEtl\Loader\File
13
 */
14
class Csv extends AbstractCsvFile implements \Iterator
15
{
16
    /** @var string[] Current line */
17
    protected $current;
18
19
    /** @var int Number of lines read */
20
    protected $count = 0;
21
22
    /** @var string[] CSV file headers */
23
    protected $headers = null;
24
25
    /**
26
     * Initialize the read of the file.
27
     */
28
    protected function init()
29
    {
30
        if (is_null($this->file)) {
0 ignored issues
show
introduced by
The condition is_null($this->file) is always false.
Loading history...
31
            if (is_string($this->filePath)) {
32
                $this->setStream(fopen($this->filePath, 'r'));
33
            } else {
34
                $this->setStream($this->filePath);
35
            }
36
        }
37
    }
38
39
    public function setStream($file)
40
    {
41
        if (!is_null($this->file)) {
0 ignored issues
show
introduced by
The condition is_null($this->file) is always false.
Loading history...
42
            throw new \LogicException("Can't set a stream, stream already open!");
43
        }
44
45
        $this->file = $file;
46
        $headers = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape);
47
48
        if (is_null($this->headers)) {
49
            $this->headers = $headers;
50
        }
51
52
        $this->next();
53
    }
54
55
    /**
56
     * Return file headers
57
     *
58
     * @return \string[]
59
     */
60
    public function getHeaders()
61
    {
62
        $this->init();
63
64
        return $this->headers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headers returns the type string[] which is incompatible with the documented return type string[].
Loading history...
65
    }
66
67
    /**
68
     * Set file headers
69
     *
70
     * @param array $headers File headers to set
71
     */
72
    public function setHeaders($headers)
73
    {
74
        $this->init();
75
76
        $this->headers = $headers;
77
    }
78
79
    /**
80
     * Return the current element
81
     *
82
     * @link http://php.net/manual/en/iterator.current.php
83
     * @return mixed Can return any type.
84
     * @since 5.0.0
85
     */
86
    public function current()
87
    {
88
        $this->init();
89
90
        return $this->current;
91
    }
92
93
    /**
94
     * Move forward to next element
95
     *
96
     * @link http://php.net/manual/en/iterator.next.php
97
     * @return void Any returned value is ignored.
98
     * @since 5.0.0
99
     */
100
    public function next()
101
    {
102
        $this->init();
103
        $this->count++;
104
105
        $current = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape);
106
107
        if ($current) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $current of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
108
            $this->current = array_combine($this->headers, $current);
109
            return;
110
        }
111
112
        $this->current = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type string[] of property $current.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
113
    }
114
115
    /**
116
     * Return the key of the current element
117
     *
118
     * @link http://php.net/manual/en/iterator.key.php
119
     * @return mixed scalar on success, or null on failure.
120
     * @since 5.0.0
121
     */
122
    public function key()
123
    {
124
        $this->init();
125
126
        return $this->count;
127
    }
128
129
    /**
130
     * Checks if current position is valid
131
     *
132
     * @link http://php.net/manual/en/iterator.valid.php
133
     * @return boolean The return value will be casted to boolean and then evaluated.
134
     * Returns true on success or false on failure.
135
     * @since 5.0.0
136
     */
137
    public function valid()
138
    {
139
        $this->init();
140
141
        return $this->file !== false && $this->current() != false;
142
    }
143
144
    /**
145
     * Rewind the Iterator to the first element
146
     *
147
     * @link http://php.net/manual/en/iterator.rewind.php
148
     * @return void Any returned value is ignored.
149
     * @since 5.0.0
150
     */
151
    public function rewind()
152
    {
153
        if (!is_null($this->file)) {
0 ignored issues
show
introduced by
The condition is_null($this->file) is always false.
Loading history...
154
            fclose($this->file);
155
            $this->count = 0;
156
            $this->file = null;
157
        }
158
159
        $this->init();
160
    }
161
}
162