Passed
Branch master (3daac1)
by Vincent
07:53
created

DependencyIterator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 29
c 1
b 0
f 0
dl 0
loc 119
ccs 30
cts 30
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A current() 0 3 1
A next() 0 24 5
A __construct() 0 5 1
A rewind() 0 13 3
A valid() 0 3 2
1
<?php
2
3
namespace Bdf\Form\Aggregate\Collection;
4
5
use Bdf\Form\Child\ChildInterface;
6
use Iterator;
7
8
/**
9
 * Iterate over @see DependencyTree
10
 *
11
 * Iterate first on the last level, and go to lower levels, to root
12
 */
13
final class DependencyIterator implements Iterator
14
{
15
    /**
16
     * @var ChildInterface[]
17
     */
18
    private $children;
19
20
    /**
21
     * @var Level
22
     */
23
    private $first;
24
25
    /**
26
     * @var bool
27
     */
28
    private $reverse;
29
30
    /**
31
     * @var Level|null
32
     */
33
    private $currentLevel;
34
35
    /**
36
     * @var Iterator|null
37
     */
38
    private $levelIterator;
39
40
41
    /**
42
     * DependencyIterator constructor.
43
     *
44
     * @param ChildInterface[] $children
45
     * @param Level $first
46
     * @param bool $reverse Does iterate on reverse order on levels ?
47
     */
48 10
    public function __construct(array $children, Level $first, $reverse = true)
49
    {
50 10
        $this->children = $children;
51 10
        $this->first    = $first;
52 10
        $this->reverse  = $reverse;
53 10
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @return ChildInterface
59
     */
60 8
    public function current()
61
    {
62 8
        return $this->children[$this->key()];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @psalm-suppress PossiblyNullReference
69
     * @psalm-suppress PossiblyNullReference
70
     */
71 10
    public function next()
72
    {
73 10
        $this->levelIterator->next();
0 ignored issues
show
Bug introduced by
The method next() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->levelIterator->/** @scrutinizer ignore-call */ 
74
                              next();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
75
        // The level iterator can be invalid if the level is empty
76
        // We need to skip empty levels
77 10
        while (!$this->levelIterator->valid()) {
78 9
            $this->levelIterator = null;
79 9
            $this->currentLevel  = $this->reverse ? $this->currentLevel->prev() : $this->currentLevel->next();
0 ignored issues
show
Bug introduced by
The method prev() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $this->currentLevel  = $this->reverse ? $this->currentLevel->/** @scrutinizer ignore-call */ prev() : $this->currentLevel->next();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
81
            // There is no more level, the iterator will be "invalid"
82 9
            if ($this->currentLevel === null) {
83 8
                return;
84
            }
85
86
            // Create the new iterator, and reset
87 8
            $this->levelIterator = $this->currentLevel->getIterator();
88 8
            $this->levelIterator->rewind();
89
        }
90
91
        // The children is not already registered
92
        // We should skip this step
93 8
        if (!isset($this->children[$this->key()])) {
94 2
            $this->next();
95
        }
96 8
    }
97
98
    /**
99
     * {@inheritdoc}
100
     *
101
     * @psalm-suppress PossiblyNullReference
102
     */
103 9
    public function key()
104
    {
105 9
        return $this->levelIterator->key();
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 8
    public function valid()
112
    {
113 8
        return $this->levelIterator !== null && $this->levelIterator->valid();
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 10
    public function rewind()
120
    {
121 10
        $this->currentLevel  = $this->first;
122 10
        $this->levelIterator = $this->currentLevel->getIterator();
123 10
        $this->levelIterator->rewind();
124
125
        // The child is not registered, we should go next
126
        // Or the iterator is not valid
127
        if (
128 10
            !$this->levelIterator->valid()
129 10
            || !isset($this->children[$this->key()])
130
        ) {
131 4
            $this->next();
132
        }
133 10
    }
134
}
135