Failed Conditions
Push — master ( 8fb951...fb1394 )
by Arnold
03:15
created

AbstractLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 96
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A getInnerIterator() 0 3 1
A __construct() 0 4 1
A current() 0 3 1
A valid() 0 3 2
A rewind() 0 6 1
A next() 0 6 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\Container\Loader;
6
7
/**
8
 * Base class for loader using two iterators.
9
 */
10
abstract class AbstractLoader implements \OuterIterator
11
{
12
    /**
13
     * @var \Iterator
14
     */
15
    protected $items;
16
17
    /**
18
     * @var \ArrayIterator
19
     */
20
    protected $entries;
21
22
23
    /**
24
     * Class constructor.
25
     *
26
     * @param \Iterator $items
27
     */
28 16
    public function __construct(\Iterator $items)
29
    {
30 16
        $this->items = $items;
31 16
        $this->prepareNext();
32 15
    }
33
34
    /**
35
     * Prepare new entries
36
     *
37
     * @return void
38
     */
39
    abstract protected function prepareNext(): void;
40
41
    /**
42
     * Return the current element
43
     *
44
     * @return callable
45
     */
46 11
    public function current(): callable
47
    {
48 11
        return $this->entries->current();
49
    }
50
51
    /**
52
     * Move forward to next element
53
     *
54
     * @return void
55
     */
56 11
    public function next(): void
57
    {
58 11
        $this->entries->next();
59
60 11
        while (!$this->entries->valid() && $this->items->valid()) {
61 11
            $this->prepareNext();
62
        }
63 10
    }
64
65
    /**
66
     * Return the key of the current element
67
     *
68
     * @return string|null
69
     */
70 11
    public function key(): ?string
71
    {
72 11
        return $this->entries->key();
73
    }
74
75
    /**
76
     * Checks if current position is valid
77
     *
78
     * @return bool
79
     */
80 13
    public function valid(): bool
81
    {
82 13
        return isset($this->entries) && $this->entries->valid();
83
    }
84
85
    /**
86
     * Rewind the iterator to the first element
87
     *
88
     * @return void
89
     */
90 13
    public function rewind(): void
91
    {
92 13
        $this->items->rewind();
93 13
        $this->entries = null;
0 ignored issues
show
introduced by
Property Jasny\Container\Loader\AbstractLoader::$entries (ArrayIterator) does not accept null.
Loading history...
introduced by
Property Jasny\Container\Loader\AbstractLoader::$entries (ArrayIterator) does not accept null.
Loading history...
94
95 13
        $this->prepareNext();
96 13
    }
97
98
    /**
99
     * Returns the inner iterator for the current entry.
100
     *
101
     * @return \Iterator
102
     */
103 2
    public function getInnerIterator(): \Iterator
104
    {
105 2
        return $this->items;
106
    }
107
}