Passed
Push — master ( f4eeb8...0d22ee )
by Sergei
11:47 queued 01:39
created

TraversableObject::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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
    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 56
    public function current()
34
    {
35 56
        return $this->data[$this->position];
36
    }
37
38
    #[\ReturnTypeWillChange]
39 56
    public function next()
40
    {
41 56
        $this->position++;
42
    }
43
44
    #[\ReturnTypeWillChange]
45 55
    public function key()
46
    {
47 55
        return $this->position;
48
    }
49
50
    #[\ReturnTypeWillChange]
51 56
    public function valid()
52
    {
53 56
        return array_key_exists($this->position, $this->data);
54
    }
55
56
    #[\ReturnTypeWillChange]
57 56
    public function rewind()
58
    {
59 56
        $this->position = 0;
60
    }
61
}
62