Completed
Push — master ( 23f593...5271f8 )
by Ron
03:22
created

YieldPolyfillIterator::getStmt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace Kir\MySQL\Builder\Helpers;
3
4
use Closure;
5
use Iterator;
6
use Kir\MySQL\Builder\QueryStatement;
7
use Kir\MySQL\Builder\RunnableSelect;
8
use PDO;
9
10
class YieldPolyfillIterator implements Iterator {
11
	/** @var RunnableSelect */
12
	private $select;
13
	/** @var Closure|null */
14
	private $callback;
15
	/** @var bool */
16
	private $preserveTypes;
17
	/** @var null */
18
	private $stmt = null;
19
	/** @var Closure */
20
	private $statementFactory;
21
	/** @var mixed */
22
	private $currentItem;
23
	/** @var int */
24
	private $idx = -1;
25
26
	/**
27
	 * @param RunnableSelect $select
28
	 * @param Closure|null $callback
29
	 * @param bool $preserveTypes
30
	 * @param callable $statementFactory
31
	 */
32
	public function __construct(RunnableSelect $select, Closure $callback = null, $preserveTypes, $statementFactory) {
33
		$this->select = $select;
34
		$this->callback = $callback;
35
		$this->preserveTypes = $preserveTypes;
36
		$this->statementFactory = $statementFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $statementFactory of type callable is incompatible with the declared type object<Closure> of property $statementFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
	}
38
39
	/**
40
	 */
41
	public function __destruct() {
42
		$this->closeCursor();
43
	}
44
45
	/**
46
	 * @return mixed
47
	 */
48
	public function current() {
49
		$row = $this->currentItem;
50
		if($this->preserveTypes) {
51
			$columnDefinitions = FieldTypeProvider::getFieldTypes($this->getStmt());
0 ignored issues
show
Bug introduced by
It seems like $this->getStmt() can be null; however, getFieldTypes() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52
			$row = FieldValueConverter::convertValues($row, $columnDefinitions);
53
		}
54
		$callback = $this->callback;
55
		if($callback !== null) {
56
			$result = $callback($row);
57
			if($result instanceof DBIgnoreRow) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
58
				// Do nothing in this case
59
			} elseif($result !== null) {
60
				return $result;
61
			} else {
62
				return $row;
63
			}
64
		} else {
65
			return $row;
66
		}
67
	}
68
69
	/**
70
	 */
71
	public function next() {
72
		$this->idx++;
73
		$this->currentItem = $this->getStmt()->fetch(PDO::FETCH_ASSOC);
74
	}
75
76
	/**
77
	 * @return mixed
78
	 */
79
	public function key() {
80
		return $this->idx;
81
	}
82
83
	/**
84
	 * @return boolean
85
	 */
86
	public function valid() {
87
		$result = !!$this->currentItem;
88
		if(!$result) {
89
			$this->closeCursor();
90
		}
91
		return $result;
92
	}
93
94
	/**
95
	 */
96
	public function rewind() {
97
		if($this->stmt !== null) {
98
			throw new \Exception("It's not possible to rewind this iterator");
99
		}
100
		$this->stmt = call_user_func($this->statementFactory);
101
		$this->idx = -1;
102
		$this->next();
103
	}
104
105
	/**
106
	 * @return QueryStatement
107
	 */
108
	private function getStmt() {
109
		if($this->stmt === null) {
110
			$this->rewind();
111
		}
112
		return $this->stmt;
113
	}
114
115
	/**
116
	 */
117
	private function closeCursor() {
118
		if($this->stmt instanceof QueryStatement) {
119
			$this->stmt->closeCursor();
120
		}
121
		$this->stmt = null;
122
	}
123
}
124