loophp /
phptree
| 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
Loading history...
|
|||||
| 23 | |||||
| 24 | 1 | $hash = hash('sha256', $node->getKey() . $data); |
|||
|
0 ignored issues
–
show
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
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
|
|||||
| 59 | } |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | 1 | parent::add($node); |
|||
| 63 | |||||
| 64 | 1 | return $node; |
|||
| 65 | } |
||||
| 66 | } |
||||
| 67 |