Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

SQLRelayIterator::moveNext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace ByJG\AnyDataset\Repository;
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
        } else if (is_null($this->_cursor)) {
47
            return (count($this->_rowBuffer) > 0);
48
        } else if ($this->_currentRow < $this->count()) {
49
            $sr = new SingleRow();
50
51
            $colCount = sqlrcur_colCount($this->_cursor);
52
            for ($col = 0; $col < $colCount; $col++) {
53
                $fieldName = strtolower(sqlrcur_getColumnName($this->_cursor, $col));
54
                $value = sqlrcur_getField($this->_cursor, $this->_currentRow, $col);
55
56
                if (is_null($value)) {
57
                    $sr->addField($fieldName, "");
58
                } elseif (is_object($value)) {
59
                    $sr->addField($fieldName, "[OBJECT]");
60
                } else {
61
                    $value = AnyDataset::fixUTF8($value);
62
                    $sr->addField($fieldName, $value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by \ByJG\AnyDataset\Reposit...ataset::fixUTF8($value) on line 61 can also be of type array; however, ByJG\AnyDataset\Repository\SingleRow::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...
63
                }
64
            }
65
66
            $this->_currentRow++;
67
68
            // Enfileira o registo
69
            array_push($this->_rowBuffer, $sr);
70
            // Traz novos até encher o Buffer
71
            if (count($this->_rowBuffer) < DBIterator::RECORD_BUFFER) {
72
                $this->hasNext();
73
            }
74
75
            return true;
76
        } else {
77
            sqlrcur_free($this->_cursor);
78
            $this->_cursor = null;
79
80
            return (count($this->_rowBuffer) > 0);
81
        }
82
    }
83
84
    public function __destruct()
85
    {
86
        if (!is_null($this->_cursor)) {
87
            @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...
88
            $this->_cursor = null;
89
        }
90
    }
91
92
    /**
93
     * @return SingleRow
94
     * @throws IteratorException
95
     */
96 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...
97
    {
98
        if (!$this->hasNext()) {
99
            throw new IteratorException("No more records. Did you used hasNext() before moveNext()?");
100
        } else {
101
            $sr = array_shift($this->_rowBuffer);
102
            $this->_moveNextRow++;
103
            return $sr;
104
        }
105
    }
106
107
    function key()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
108
    {
109
        return $this->_moveNextRow;
110
    }
111
}
112