Completed
Push — work-fleets ( 961997...006942 )
by SuperNova.WS
06:22
created

DbResultIterator::getFirstColumn()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 5
ccs 0
cts 1
cp 0
crap 12
rs 9.4285
1
<?php
2
3
/**
4
 * Class DbResultIterator
5
 *
6
 * @property mixed $_result
7
 */
8
abstract class DbResultIterator implements Iterator, Countable {
9
  /**
10
   * @var mixed $_result
11
   */
12
  protected $_result;
13
  /**
14
   * @var int
15
   */
16
  protected $fetchMode;
17
  /**
18
   * @var int
19
   */
20
  protected $position;
21
  /**
22
   * @var array|null
23
   */
24
  protected $currentRow;
25
26
  abstract protected function fetchCurrentRow();
27
28
  /**
29
   * Constructor
30
   *
31
   * @param mixed $result
32
   * @param int   $fetchMode constant (MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH)
33
   */
34
  public function __construct($result, $fetchMode = MYSQLI_ASSOC) {
35
    $this->_result = $result;
36
    $this->fetchMode = $fetchMode;
37
38
    $this->position = 0;
39
    // prefetch the current row
40
    // note that this advances the Results internal pointer.
41
    $this->fetchCurrentRow();
42
  }
43
44
  /**
45
   * Gets first column value (if any) of current row
46
   *
47
   * @return mixed
48
   */
49
  public function getFirstColumn() {
50
    $row = $this->valid() ? $this->current() : null;
51
52
    return is_array($row) ? array_pop($row) : $row;
53
  }
54
55
  /**
56
   * Destructor
57
   * Frees the Result object
58
   */
59
  public function __destruct() {
60
    //TODO
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
//    $this->Result->free();
62
  }
63
64
  /**
65
   * Return the current element
66
   * Returns the row that matches the current position
67
   * @link http://php.net/manual/en/iterator.current.php
68
   * @return mixed Can return any type.
69
   * @since 5.0.0
70
   */
71
  public function current() {
72
    return $this->currentRow;
73
  }
74
75
  /**
76
   * Move forward to next element
77
   * Moves the internal pointer one step forward
78
   * @link http://php.net/manual/en/iterator.next.php
79
   * @return void Any returned value is ignored.
80
   * @since 5.0.0
81
   */
82
  public function next() {
83
    // prefetch the current row
84
    $this->fetchCurrentRow();
85
86
    // and increment internal pointer
87
    ++$this->position;
88
  }
89
90
  /**
91
   * Return the key of the current element - the current position
92
   * @link http://php.net/manual/en/iterator.key.php
93
   * return mixed scalar on success, or null on failure.
94
   * @return int
95
   * @since 5.0.0
96
   */
97
  public function key() {
98
    return $this->position;
99
  }
100
101
  /**
102
   * Checks if current position is valid
103
   * Returns true if the current position is valid, false otherwise.
104
   * @link http://php.net/manual/en/iterator.valid.php
105
   * @return boolean The return value will be casted to boolean and then evaluated.
106
   * Returns true on success or false on failure.
107
   * @since 5.0.0
108
   */
109
  public function valid() {
110
    return $this->position < $this->_result->num_rows;
111
  }
112
113
  /**
114
   * Rewind the Iterator to the first element
115
   * @link http://php.net/manual/en/iterator.rewind.php
116
   * @return void Any returned value is ignored.
117
   * @since 5.0.0
118
   */
119
  public function rewind() {
120
    // data_seek moves the Results internal pointer
121
    $this->_result->data_seek($this->position = 0);
122
123
    // prefetch the current row
124
    // note that this advances the Results internal pointer.
125
    $this->fetchCurrentRow();
126
  }
127
128
129
  /**
130
   * Count elements of an object
131
   * @link http://php.net/manual/en/countable.count.php
132
   * @return int The custom count as an integer.
133
   * </p>
134
   * <p>
135
   * The return value is cast to an integer.
136
   * @since 5.1.0
137
   */
138
  public function count() {
139
    throw new Exception('You should implement ' . get_called_class() . '::count()');
140
  }
141
142
}
143