Passed
Push — master ( 9f7d35...3f1c7e )
by Wilmer
08:50 queued 06:32
created

TraversableObject::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestUtility;
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
17
    private int $position = 0;
18
19
    public function __construct(array $array)
20
    {
21
        $this->data = $array;
22
    }
23
24
    /**
25
     * @throws \Exception
26
     */
27
    public function count()
28
    {
29
        throw new \Exception('Count called on object that should only be traversed.');
30
    }
31
32 45
    public function current()
33
    {
34 45
        return $this->data[$this->position];
35
    }
36
37 45
    public function next()
38
    {
39 45
        $this->position++;
40 45
    }
41
42 44
    public function key()
43
    {
44 44
        return $this->position;
45
    }
46
47 45
    public function valid()
48
    {
49 45
        return array_key_exists($this->position, $this->data);
50
    }
51
52 45
    public function rewind()
53
    {
54 45
        $this->position = 0;
55 45
    }
56
}
57