PairNode::groupPairNodes()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace dlindberg\BlobChunk\Parser;
6
7
final class PairNode extends BaseParser implements Parse
8
{
9
    public function parse(\DOMElement $node): array
10
    {
11
        $this->manager->setPairParentNode($node);
12
13
        return $this->groupPairNodes($node->firstChild);
14
    }
15
16
    private function groupPairNodes(\DOMNode $node, $carry = []): ?array
17
    {
18
        if ($this->manager->isPairSideANode($node)) {
19
            $a = $this->stringify($node);
20
            ['bNodes' => $b, 'currentNode' => $node] = $this->collectBNodes($node->nextSibling);
21
            $carry[] = [
22
                'a' => $a,
23
                'b' => $b,
24
            ];
25
        }
26
27
        return null !== $node->nextSibling ? $this->groupPairNodes($node->nextSibling, $carry) : $carry;
28
    }
29
30
    private function collectBNodes(?\DOMNode $node, array $nodes = []): array
31
    {
32
        if ($this->manager->isPairSideBNode($node)) {
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $input of dlindberg\BlobChunk\Mana...Node::isPairSideBNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

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

32
        if ($this->manager->isPairSideBNode(/** @scrutinizer ignore-type */ $node)) {
Loading history...
33
            $nodes[] = $this->stringify($node);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $node of dlindberg\BlobChunk\Parser\BaseParser::stringify() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

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

33
            $nodes[] = $this->stringify(/** @scrutinizer ignore-type */ $node);
Loading history...
34
        }
35
        if ($this->manager->isPairSideANode($node)) {
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $input of dlindberg\BlobChunk\Mana...Node::isPairSideANode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        if ($this->manager->isPairSideANode(/** @scrutinizer ignore-type */ $node)) {
Loading history...
36
            $node = $node->previousSibling;
37
        } elseif (null !== $node->nextSibling) {
38
            return $this->collectBNodes($node->nextSibling, $nodes);
39
        }
40
41
        return [
42
            'currentNode' => $node,
43
            'bNodes'      => $nodes,
44
        ];
45
    }
46
}
47