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

TraversableObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 47
ccs 10
cts 14
cp 0.7143
rs 10
c 1
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 4 1
A rewind() 0 4 1
A __construct() 0 3 1
A next() 0 4 1
A current() 0 4 1
A valid() 0 4 1
A count() 0 4 1
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