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

SqlRelayIterator::hasNext()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 34
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 26
nc 11
nop 0
crap 72
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use ByJG\AnyDataset\Exception\IteratorException;
6
7
class SqlRelayIterator extends GenericIterator
8
{
9
10
    const RECORD_BUFFER = 50;
11
12
    private $rowBuffer;
13
    protected $currentRow = 0;
14
    protected $moveNextRow = 0;
15
16
    /**
17
     * @var resource
18
     */
19
    private $cursor;
20
21
    /**
22
     *
23
     * @param resource $cursor
24
     */
25
    public function __construct($cursor)
26
    {
27
        $this->cursor = $cursor;
28
        $this->rowBuffer = array();
29
    }
30
31
    /**
32
     * @return int
33
     */
34
    public function count()
35
    {
36
        return sqlrcur_rowCount($this->cursor);
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    public function hasNext()
43
    {
44
        if (count($this->rowBuffer) >= SqlRelayIterator::RECORD_BUFFER) {
45
            return true;
46
        }
47
48
        if (is_null($this->cursor)) {
49
            return (count($this->rowBuffer) > 0);
50
        }
51
52
        if ($this->currentRow < $this->count()) {
53
            $sr = new Row();
54
55
            $colCount = sqlrcur_colCount($this->cursor);
56
            for ($col = 0; $col < $colCount; $col++) {
57
                $fieldName = strtolower(sqlrcur_getColumnName($this->cursor, $col));
58
                $value = sqlrcur_getField($this->cursor, $this->currentRow, $col);
59
60
                if (is_null($value)) {
61
                    $sr->addField($fieldName, "");
62
                } elseif (is_object($value)) {
63
                    $sr->addField($fieldName, "[OBJECT]");
64
                } else {
65
                    $value = AnyDataset::fixUTF8($value);
66
                    $sr->addField($fieldName, $value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by \ByJG\AnyDataset\Dataset...ataset::fixUTF8($value) on line 65 can also be of type array; however, ByJG\AnyDataset\Dataset\Row::addField() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
67
                }
68
            }
69
70
            $this->currentRow++;
71
72
            // Enfileira o registo
73
            array_push($this->rowBuffer, $sr);
74
            // Traz novos até encher o Buffer
75
            if (count($this->rowBuffer) < DbIterator::RECORD_BUFFER) {
76
                $this->hasNext();
77
            }
78
79
            return true;
80
        }
81
82
        sqlrcur_free($this->cursor);
83
        $this->cursor = null;
84
85
        return (count($this->rowBuffer) > 0);
86
    }
87
88
    public function __destruct()
89
    {
90
        if (!is_null($this->cursor)) {
91
            @sqlrcur_free($this->cursor);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
92
            $this->cursor = null;
93
        }
94
    }
95
96
    /**
97
     * @return Row
98
     * @throws IteratorException
99
     */
100 View Code Duplication
    public function moveNext()
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...
101
    {
102
        if (!$this->hasNext()) {
103
            throw new IteratorException("No more records. Did you used hasNext() before moveNext()?");
104
        }
105
106
        $sr = array_shift($this->rowBuffer);
107
        $this->moveNextRow++;
108
        return $sr;
109
    }
110
111
    public function key()
112
    {
113
        return $this->moveNextRow;
114
    }
115
}
116