Passed
Pull Request — master (#163)
by Wilmer
17:48 queued 02:54
created

TraversableObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 42
rs 10
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
    public function current()
33
    {
34
        return $this->data[$this->position];
35
    }
36
37
    public function next()
38
    {
39
        $this->position++;
40
    }
41
42
    public function key()
43
    {
44
        return $this->position;
45
    }
46
47
    public function valid()
48
    {
49
        return array_key_exists($this->position, $this->data);
50
    }
51
52
    public function rewind()
53
    {
54
        $this->position = 0;
55
    }
56
}
57