PDOStatementIterator::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace itertools;
4
5
use Iterator;
6
use PDO;
7
use PDOStatement;
8
9
10
class PDOStatementIterator implements Iterator
11
{
12
    protected $stmtFactory;
13
	protected $fetchStyle;
14
	protected $currentValue;
15
	protected $currentKey;
16
17
    public function __construct($stmt, $fetchStyle = PDO::FETCH_BOTH)
18
    {
19
		if(is_callable($stmt)) {
20
			$this->stmtFactory = $stmt;
21
		} else {
22
			$this->stmtFactory = function() use ($stmt) { return $stmt; };
23
		}
24
		$this->fetchStyle = $fetchStyle;
25
        $this->stmt = $stmt;
0 ignored issues
show
Bug introduced by
The property stmt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    public function rewind()
29
    {
30
		$this->stmt = call_user_func($this->stmtFactory);
31
		$this->next();
32
		$this->currentKey = 0;
33
    }
34
35
    public function valid()
36
    {
37
        return false !== $this->currentValue;
38
    }
39
40
    public function current()
41
    {
42
        return $this->currentValue;
43
    }
44
45
    public function key()
46
    {
47
        return $this->currentKey;
48
    }
49
50
    public function next()
51
    {
52
		$this->currentKey += 1;
53
		$this->currentValue = $this->stmt->fetch($this->fetchStyle);
54
    }
55
}
56
57