Passed
Pull Request — master (#380)
by Wilmer
04:10 queued 01:20
created

TraversableObject::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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