Passed
Pull Request — master (#21)
by De Cramer
07:15
created

Csv::setStream()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
ccs 0
cts 7
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
            $this->setStream(fopen($this->filePath, 'r'));
32
        }
33
    }
34
35
    public function setStream($file)
36
    {
37
        if (!is_null($file)) {
38
            throw new \LogicException("Can't set a stream, stream already open!");
39
        }
40
41
        $this->file = $file;
42
        $headers = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape);
43
44
        if (is_null($this->headers)) {
0 ignored issues
show
introduced by
The condition is_null($this->headers) is always false.
Loading history...
45
            $this->headers = $headers;
46
        }
47
48
        $this->next();
49
    }
50
51
    /**
52
     * Return file headers
53
     *
54
     * @return \string[]
55
     */
56
    public function getHeaders()
57
    {
58
        $this->init();
59
60
        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...
61
    }
62
63
    /**
64
     * Set file headers
65
     *
66
     * @param array $headers File headers to set
67
     */
68
    public function setHeaders($headers)
69
    {
70
        $this->init();
71
72
        $this->headers = $headers;
73
    }
74
75
    /**
76
     * Return the current element
77
     *
78
     * @link http://php.net/manual/en/iterator.current.php
79
     * @return mixed Can return any type.
80
     * @since 5.0.0
81
     */
82
    public function current()
83
    {
84
        $this->init();
85
86
        return $this->current;
87
    }
88
89
    /**
90
     * Move forward to next element
91
     *
92
     * @link http://php.net/manual/en/iterator.next.php
93
     * @return void Any returned value is ignored.
94
     * @since 5.0.0
95
     */
96
    public function next()
97
    {
98
        $this->init();
99
        $this->count++;
100
101
        $current = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape);
102
103
        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...
104
            $this->current = array_combine($this->headers, $current);
105
            return;
106
        }
107
108
        $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...
109
    }
110
111
    /**
112
     * Return the key of the current element
113
     *
114
     * @link http://php.net/manual/en/iterator.key.php
115
     * @return mixed scalar on success, or null on failure.
116
     * @since 5.0.0
117
     */
118
    public function key()
119
    {
120
        $this->init();
121
122
        return $this->count;
123
    }
124
125
    /**
126
     * Checks if current position is valid
127
     *
128
     * @link http://php.net/manual/en/iterator.valid.php
129
     * @return boolean The return value will be casted to boolean and then evaluated.
130
     * Returns true on success or false on failure.
131
     * @since 5.0.0
132
     */
133
    public function valid()
134
    {
135
        $this->init();
136
137
        return $this->file !== false && $this->current() != false;
138
    }
139
140
    /**
141
     * Rewind the Iterator to the first element
142
     *
143
     * @link http://php.net/manual/en/iterator.rewind.php
144
     * @return void Any returned value is ignored.
145
     * @since 5.0.0
146
     */
147
    public function rewind()
148
    {
149
        if (!is_null($this->file)) {
0 ignored issues
show
introduced by
The condition is_null($this->file) is always false.
Loading history...
150
            fclose($this->file);
151
            $this->count = 0;
152
            $this->file = null;
153
        }
154
155
        $this->init();
156
    }
157
}
158