Completed
Pull Request — master (#2)
by Joao
05:22
created

Oci8Iterator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 10.42 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 10
loc 96
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A count() 0 4 1
B hasNext() 0 30 5
A __destruct() 0 7 2
A moveNext() 10 10 2
A key() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use ByJG\AnyDataset\Exception\IteratorException;
6
7
class Oci8Iterator 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 Cursor
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
     * @access public
33
     * @return int
34
     */
35
    public function count()
36
    {
37
        return -1;
38
    }
39
40
    /**
41
     * @access public
42
     * @return bool
43
     */
44
    public function hasNext()
45
    {
46
        if (count($this->rowBuffer) >= Oci8Iterator::RECORD_BUFFER) {
47
            return true;
48
        }
49
50
        if (is_null($this->cursor)) {
51
            return (count($this->rowBuffer) > 0);
52
        }
53
54
        $rowArray = oci_fetch_array($this->cursor, OCI_ASSOC + OCI_RETURN_NULLS);
55
        if (!empty($rowArray)) {
56
            $rowArray = array_change_key_case($rowArray, CASE_LOWER);
57
            $singleRow = new Row($rowArray);
58
59
            $this->currentRow++;
60
61
            // Enfileira o registo
62
            array_push($this->rowBuffer, $singleRow);
63
            // Traz novos até encher o Buffer
64
            if (count($this->rowBuffer) < DbIterator::RECORD_BUFFER) {
65
                $this->hasNext();
66
            }
67
            return true;
68
        }
69
70
        oci_free_statement($this->cursor);
71
        $this->cursor = null;
72
        return (count($this->rowBuffer) > 0);
73
    }
74
75
    public function __destruct()
76
    {
77
        if (!is_null($this->cursor)) {
78
            oci_free_statement($this->cursor);
79
            $this->cursor = null;
80
        }
81
    }
82
83
    /**
84
     * @return mixed
85
     * @throws IteratorException
86
     */
87 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...
88
    {
89
        if (!$this->hasNext()) {
90
            throw new IteratorException("No more records. Did you used hasNext() before moveNext()?");
91
        } else {
92
            $sr = array_shift($this->rowBuffer);
93
            $this->moveNextRow++;
94
            return $sr;
95
        }
96
    }
97
98
    public function key()
99
    {
100
        return $this->moveNextRow;
101
    }
102
}
103