Conditions | 4 |
Paths | 4 |
Total Lines | 27 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 4 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
19 | 1 | public function add(NodeInterface ...$nodes): NodeInterface |
|
20 | { |
||
21 | 1 | foreach ($nodes as $node) { |
|
22 | 1 | $data = $node->getValue(); |
|
|
|||
23 | |||
24 | 1 | $hash = hash('sha256', $node->getKey() . $data); |
|
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 | } |
||
67 |