PreOrderVisitor   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 7
c 1
b 0
f 1
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A visit() 0 10 3
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Copyright (C) 2019 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Forest\Visitor;
13
14
use Seboettg\Collection\ArrayList;
15
use Seboettg\Collection\ArrayList\ArrayListInterface;
16
use Seboettg\Forest\General\TreeNodeInterface;
17
18
/**
19
 * Class PreOrderVisitor
20
 * Implements the tree traversal strategy Pre-order.
21
 * In this traversal strategy, the root node is visited first, then the subtrees of the children from left to right
22
 *
23
 * @package Seboettg\Forest\Visitor
24
 */
25
class PreOrderVisitor implements VisitorInterface
26
{
27
    /**
28
     * @param TreeNodeInterface $node
29
     * @return ArrayListInterface
30
     */
31 8
    public function visit(TreeNodeInterface $node): ArrayListInterface
32
    {
33 8
        $resultList = new ArrayList();
34 8
        if ($node !== null) {
35 8
            $resultList->append($node->getItem());
36 8
            foreach($node->getChildren() as $child) {
37 8
                $resultList->merge($child->accept($this));
38
            }
39
        }
40 8
        return $resultList;
41
    }
42
}
43