TrieNode::append()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
rs 10
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\Node;
11
12
use Exception;
13
14
/**
15
 * Class TrieNode.
16
 */
17
class TrieNode extends KeyValueNode
18
{
19 1
    public function add(NodeInterface ...$nodes): NodeInterface
20
    {
21 1
        foreach ($nodes as $node) {
22 1
            $data = $node->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on loophp\phptree\Node\NodeInterface. It seems like you code against a sub-type of loophp\phptree\Node\NodeInterface such as loophp\phptree\Node\ValueNodeInterface or loophp\phptree\Node\ValueNode or loophp\phptree\Node\ValueNode. ( Ignorable by Annotation )

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

22
            /** @scrutinizer ignore-call */ 
23
            $data = $node->getValue();
Loading history...
23
24 1
            $hash = hash('sha256', $node->getKey() . $data);
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on loophp\phptree\Node\NodeInterface. It seems like you code against a sub-type of loophp\phptree\Node\NodeInterface such as loophp\phptree\Node\KeyValueNode or loophp\phptree\Node\KeyValueNodeInterface or loophp\phptree\Node\KeyValueNode or loophp\phptree\Node\KeyValueNode. ( Ignorable by Annotation )

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

24
            $hash = hash('sha256', $node->/** @scrutinizer ignore-call */ getKey() . $data);
Loading history...
25
26 1
            $node = new self($hash, substr($data, 0, 1));
27 1
            $parent = $this->append($node);
28
29 1
            $dataWithoutFirstLetter = substr($data, 1);
30
31 1
            if ('' < $dataWithoutFirstLetter) {
32 1
                $parent->add(new self($hash, $dataWithoutFirstLetter));
33
            } else {
34 1
                $nodes = [$node->getValue()];
35
36
                /** @var KeyValueNodeInterface $ancestor */
37 1
                foreach ($node->getAncestors() as $ancestor) {
38 1
                    $nodes[] = $ancestor->getValue();
39
                }
40 1
                array_pop($nodes);
41 1
                $node->append(new self($hash, strrev(implode('', $nodes))));
42
            }
43
        }
44
45 1
        return $this;
46
    }
47
48
    /**
49
     * @throws Exception
50
     *
51
     * @return NodeInterface|ValueNodeInterface
52
     */
53 1
    private function append(ValueNodeInterface $node)
54
    {
55
        /** @var ValueNodeInterface $child */
56 1
        foreach ($this->children() as $child) {
57 1
            if ($node->getValue() === $child->getValue()) {
58 1
                return $child;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $child returns the type loophp\phptree\Node\NodeInterface[] which is incompatible with the documented return type loophp\phptree\Node\Node...Node\ValueNodeInterface.
Loading history...
59
            }
60
        }
61
62 1
        parent::add($node);
63
64 1
        return $node;
65
    }
66
}
67