InOrder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 33
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A doTraverse() 0 16 4
A traverse() 0 5 1
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