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

TraversableObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
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
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