Passed
Pull Request — master (#7448)
by Ilya
14:31
created

TreeWalkerChainIterator::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query;
6
7
use ArrayAccess;
8
use Doctrine\ORM\Query;
9
use Iterator;
10
use function key;
11
use function next;
12
use function reset;
13
14
class TreeWalkerChainIterator implements Iterator, ArrayAccess
15
{
16
    /** @var TreeWalker[] */
17
    private $walkers = [];
18
    /** @var TreeWalkerChain */
19
    private $treeWalkerChain;
20
    /** @var Query */
21
    private $query;
22
    /** @var ParserResult */
23
    private $parserResult;
24
25 98
    public function __construct(TreeWalkerChain $treeWalkerChain, $query, $parserResult)
26
    {
27 98
        $this->treeWalkerChain = $treeWalkerChain;
28 98
        $this->query           = $query;
29 98
        $this->parserResult    = $parserResult;
30 98
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 98
    public function rewind()
36
    {
37 98
        return reset($this->walkers);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 98
    public function current()
44
    {
45 98
        return $this->offsetGet(key($this->walkers));
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function key()
52
    {
53
        return key($this->walkers);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 92
    public function next()
60
    {
61 92
        next($this->walkers);
62
63 92
        return $this->offsetGet(key($this->walkers));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->offsetGet(key($this->walkers)) also could return the type object which is incompatible with the return type mandated by Iterator::next() of void.
Loading history...
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 98
    public function valid()
70
    {
71 98
        return key($this->walkers) !== null;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 98
    public function offsetExists($offset)
78
    {
79 98
        return isset($this->walkers[$offset]);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 98
    public function offsetGet($offset)
86
    {
87 98
        if ($this->offsetExists($offset)) {
88 98
            return new $this->walkers[$offset](
89 98
                $this->query,
90 98
                $this->parserResult,
91 98
                $this->treeWalkerChain->getQueryComponents()
92
            );
93
        }
94
95 92
        return null;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 98
    public function offsetSet($offset, $value)
102
    {
103 98
        if ($offset === null) {
104 98
            $this->walkers[] = $value;
105
        } else {
106
            $this->walkers[$offset] = $value;
107
        }
108 98
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function offsetUnset($offset)
114
    {
115
        if ($this->offsetExists($offset)) {
116
            unset($this->walkers[$offset]);
117
        }
118
    }
119
}
120