Passed
Branch dev (f56f10)
by Wilmer
04:41 queued 01:34
created

TraversableObject::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestSupport;
6
7
/**
8
 * TraversableObject
9
 *
10
 * Object that implements `\Traversable` and `\Countable`, but counting throws an exception;
11
 * Used for testing support for traversable objects instead of arrays.
12
 */
13
class TraversableObject implements \Iterator, \Countable
14
{
15
    protected array $data = [];
16
    private int $position = 0;
17
18
    public function __construct(array $array)
19
    {
20
        $this->data = $array;
21
    }
22
23
    /**
24
     * @throws \Exception
25
     */
26
    #[\ReturnTypeWillChange]
27
    public function count()
28
    {
29
        throw new \Exception('Count called on object that should only be traversed.');
30
    }
31
32
    #[\ReturnTypeWillChange]
33
    public function current()
34
    {
35
        return $this->data[$this->position];
36
    }
37
38
    #[\ReturnTypeWillChange]
39
    public function next()
40
    {
41
        $this->position++;
42
    }
43
44
    #[\ReturnTypeWillChange]
45
    public function key()
46
    {
47
        return $this->position;
48
    }
49
50
    #[\ReturnTypeWillChange]
51
    public function valid()
52
    {
53
        return array_key_exists($this->position, $this->data);
54
    }
55
56
    #[\ReturnTypeWillChange]
57
    public function rewind()
58
    {
59
        $this->position = 0;
60
    }
61
}
62