Passed
Push — 1.x ( 4f0279...e8be9e )
by Ulises Jeremias
02:34
created

Builder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A nodeInstanceByValue() 0 3 1
A tree() 0 6 1
A end() 0 4 1
A leaf() 0 6 1
A setNode() 0 5 1
A getNode() 0 3 1
A leafs() 0 6 2
A value() 0 4 1
1
<?php namespace Mbh\Tree\Traits;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Tree\Interfaces\Node as NodeInterface;
12
13
trait Builder
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function setNode(NodeInterface $node)
19
    {
20
        $this->emptyStack()->pushNode($node);
21
22
        return $this;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getNode()
29
    {
30
        return $this->getNodeAt(count($this->nodeStack) - 1);
0 ignored issues
show
Bug Best Practice introduced by
The property nodeStack does not exist on Mbh\Tree\Traits\Builder. Did you maybe forget to declare it?
Loading history...
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function leaf($value = null)
37
    {
38
        $this->getNode()->addChild(
39
            $this->nodeInstanceByValue($value)
0 ignored issues
show
Bug introduced by
$this->nodeInstanceByValue($value) of type Mbh\Tree\Traits\Node is incompatible with the type Mbh\Tree\Interfaces\Node expected by parameter $child of Mbh\Tree\Interfaces\Node::addChild(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            /** @scrutinizer ignore-type */ $this->nodeInstanceByValue($value)
Loading history...
40
        );
41
        return $this;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function leafs($value1 /*,  $value2, ... */)
48
    {
49
        foreach (func_get_args() as $value) {
50
            $this->leaf($value);
51
        }
52
        return $this;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function tree($value = null)
59
    {
60
        $node = $this->nodeInstanceByValue($value);
61
        $this->getNode()->addChild($node);
0 ignored issues
show
Bug introduced by
$node of type Mbh\Tree\Traits\Node is incompatible with the type Mbh\Tree\Interfaces\Node expected by parameter $child of Mbh\Tree\Interfaces\Node::addChild(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $this->getNode()->addChild(/** @scrutinizer ignore-type */ $node);
Loading history...
62
        $this->pushNode($node);
0 ignored issues
show
Bug introduced by
$node of type Mbh\Tree\Traits\Node is incompatible with the type Mbh\Tree\Interfaces\Node expected by parameter $node of Mbh\Tree\Traits\Builder::pushNode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $this->pushNode(/** @scrutinizer ignore-type */ $node);
Loading history...
63
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function end()
70
    {
71
        $this->popNode();
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function nodeInstanceByValue($value = null)
79
    {
80
        return new Node($value);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function value($value)
87
    {
88
        $this->getNode()->setValue($value);
89
        return $this;
90
    }
91
92
    abstract protected function getNodeAt(int $index): NodeInterface;
93
94
    abstract protected function emptyStack();
95
96
    abstract protected function pushNode(NodeInterface $node);
97
98
    abstract protected function popNode();
99
}
100