InOrder::doTraverse()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 5
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\phptree\Traverser;
11
12
use loophp\phptree\Node\NodeInterface;
13
use Traversable;
14
15
/**
16
 * Class InOrder.
17
 */
18
class InOrder implements TraverserInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    private $index = 0;
24
25 2
    public function traverse(NodeInterface $node): Traversable
26
    {
27 2
        $this->index = 0;
28
29 2
        return $this->doTraverse($node);
30
    }
31
32
    /**
33
     * @return Traversable<NodeInterface>
34
     */
35 2
    private function doTraverse(NodeInterface $node): Traversable
36
    {
37 2
        $middle = floor($node->degree() / 2);
38
39 2
        foreach ($node->children() as $key => $child) {
40 2
            if ((int) $key === (int) $middle) {
41 2
                yield $this->index => $node;
42 2
                ++$this->index;
43
            }
44
45 2
            if ($child->isLeaf()) {
46 2
                yield $this->index => $child;
47 2
                ++$this->index;
48
            }
49
50 2
            yield from $this->doTraverse($child);
51
        }
52 2
    }
53
}
54