RecursiveNode::findRecursionParent()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace dlindberg\BlobChunk\Parser;
6
7
final class RecursiveNode extends BaseParser implements Parse
8
{
9
    public function parse(\DOMElement $node): array
10
    {
11
        return $this->collectRecursiveChildren($node->firstChild);
12
    }
13
14
    private function collectRecursiveChildren(?\DOMNode $node, array $nodes = []): array
15
    {
16
        if ($node instanceof \DOMElement && $node->hasChildNodes()) {
17
            ['parent' => $parent, 'children' => $children] = $this->collectRecursions($node);
18
            $nodes[] = $children ? [$parent, $children] : $parent;
19
        }
20
21
        return null !== $node->nextSibling ? $this->collectRecursiveChildren($node->nextSibling, $nodes) : $nodes;
22
    }
23
24
    private function collectRecursions(\DOMElement $node): array
25
    {
26
        $children = $this->findRecursionParent($node->firstChild);
27
        if ($children instanceof \DOMElement) {
28
            $node->removeChild($children);
29
            $children = $this->parse($children);
30
        }
31
32
        return ['parent' => $this->stringify($node), 'children' => $children];
33
    }
34
35
    private function findRecursionParent(\DOMNode $node): ?\DOMElement
36
    {
37
        if ($node instanceof \DOMElement && $this->manager->isRecursiveParentNode($node)) {
38
            return $node;
39
        } elseif (null !== $node->nextSibling) {
40
            return $this->findRecursionParent($node->nextSibling);
41
        } else {
42
            return null;
43
        }
44
    }
45
}
46