Completed
Push — master ( ae0103...9b98c5 )
by Joao
04:23 queued 56s
created

TextFileIterator::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
class TextFileIterator extends GenericIterator
6
{
7
8
    protected $fields;
9
    protected $fieldexpression;
10
    protected $handle;
11
    protected $current = 0;
12
    protected $currentBuffer = "";
13
14
    /**
15
     * @access public
16
     * @param resource $handle
17
     * @param array $fields
18
     * @param string $fieldexpression
19
     */
20 12
    public function __construct($handle, $fields, $fieldexpression)
21
    {
22 12
        $this->fields = $fields;
23 12
        $this->fieldexpression = $fieldexpression;
24 12
        $this->handle = $handle;
25
26 12
        $this->readNextLine();
27 12
    }
28
29 12
    protected function readNextLine()
30
    {
31 12
        if (!$this->hasNext()) {
32 12
            return;
33
        }
34
35 12
        $buffer = fgets($this->handle, 4096);
36 12
        $this->currentBuffer = false;
0 ignored issues
show
Documentation Bug introduced by
The property $currentBuffer was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
37
38 12
        if (($buffer !== false) && (trim($buffer) != "")) {
39 12
            $this->current++;
40 12
            $this->currentBuffer = $buffer;
41 12
        } else {
42 12
            $this->readNextLine();
43
        }
44 12
    }
45
46
    /**
47
     * @access public
48
     * @return int
49
     */
50 4
    public function count()
51
    {
52 4
        return -1;
53
    }
54
55
    /**
56
     * @access public
57
     * @return bool
58
     */
59 12
    public function hasNext()
60
    {
61 12
        if ($this->currentBuffer !== false) {
62 12
            return true;
63
        }
64
65 12
        if (!$this->handle) {
66 12
            return false;
67
        }
68
69 12
        if (feof($this->handle)) {
70 12
            fclose($this->handle);
71 12
            $this->handle = null;
72 12
            return false;
73
        }
74
75 3
        return true;
76
    }
77
78
    /**
79
     * @access public
80
     * @return Row
81
     */
82 12
    public function moveNext()
83
    {
84 12
        if ($this->hasNext()) {
85 12
            $cols = preg_split($this->fieldexpression, $this->currentBuffer, -1, PREG_SPLIT_DELIM_CAPTURE);
86
87 12
            $sr = new Row();
88
89 12
            for ($i = 0; ($i < count($this->fields)) && ($i < count($cols)); $i++) {
90 12
                $column = $cols[$i];
91
92 12
                if (($i >= count($this->fields) - 1) || ($i >= count($cols) - 1)) {
93 12
                    $column = preg_replace("/(\r?\n?)$/", "", $column);
94 12
                }
95
96 12
                $sr->addField(strtolower($this->fields[$i]), $column);
97 12
            }
98
99 12
            $this->readNextLine();
100 12
            return $sr;
101
        }
102
103
        if ($this->handle) {
104
            fclose($this->handle);
105
        }
106
        return null;
107
    }
108
109
    public function key()
110
    {
111
        return $this->current;
112
    }
113
}
114