Passed
Push — master ( 9f7d35...3f1c7e )
by Wilmer
08:50 queued 06:32
created

TraversableObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 42
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

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