Apply::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
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\Modifier;
11
12
use loophp\phptree\Node\NodeInterface;
13
use loophp\phptree\Traverser\PostOrder;
14
use loophp\phptree\Traverser\TraverserInterface;
15
16
class Apply implements ModifierInterface
17
{
18
    /**
19
     * @var callable
20
     */
21
    private $apply;
22
23
    /**
24
     * @var TraverserInterface
25
     */
26
    private $traverser;
27
28
    /**
29
     * Apply constructor.
30
     */
31 2
    public function __construct(callable $apply, ?TraverserInterface $traverser = null)
32
    {
33 2
        $this->apply = $apply;
34 2
        $this->traverser = $traverser ?? new PostOrder();
35 2
    }
36
37 1
    public function modify(NodeInterface $tree): NodeInterface
38
    {
39 1
        foreach ($this->traverser->traverse($tree) as $item) {
40 1
            ($this->apply)($item);
41
        }
42
43 1
        return $tree;
44
    }
45
}
46