Passed
Pull Request — master (#372)
by Wilmer
16:16
created

TraversableObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 39
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 3 1
A current() 0 3 1
A key() 0 3 1
A valid() 0 3 1
A __construct() 0 2 1
A rewind() 0 3 1
A count() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support;
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
    private int $position = 0;
16
17
    public function __construct(protected array $data)
18
    {
19
    }
20
21
    /**
22
     * @throws \Exception
23
     */
24
    public function count(): int
25
    {
26
        throw new \Exception('Count called on object that should only be traversed.');
27
    }
28
29
    public function current(): mixed
30
    {
31
        return $this->data[$this->position];
32
    }
33
34
    public function next(): void
35
    {
36
        $this->position++;
37
    }
38
39
    public function key(): mixed
40
    {
41
        return $this->position;
42
    }
43
44
    public function valid(): bool
45
    {
46
        return array_key_exists($this->position, $this->data);
47
    }
48
49
    public function rewind(): void
50
    {
51
        $this->position = 0;
52
    }
53
}
54