Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

FlattenNodes::leaveNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Stempler\Visitor;
6
7
use Spiral\Stempler\Node\Aggregate;
8
use Spiral\Stempler\Node\Block;
9
use Spiral\Stempler\Node\HTML\Tag;
10
use Spiral\Stempler\Node\Raw;
11
use Spiral\Stempler\Node\Template;
12
use Spiral\Stempler\VisitorContext;
13
use Spiral\Stempler\VisitorInterface;
14
15
/**
16
 * Flatten all block, template and aggregate blocks from the template and inject their content to the parent template.
17
 * The visitor also merged multiple raw nodes together.
18
 *
19
 * This visitor is required to accurately calculate element indent level.
20
 */
21
final class FlattenNodes implements VisitorInterface
22
{
23 14
    public function enterNode(mixed $node, VisitorContext $ctx): mixed
24
    {
25 14
        if (!$node instanceof Tag) {
26 14
            return null;
27
        }
28
29 5
        $flatten = [];
30 5
        foreach ($node->nodes as $child) {
31 5
            if ($child instanceof Block || $child instanceof Template || $child instanceof Aggregate) {
32 1
                foreach ($child->nodes as $childNode) {
33 1
                    $flatten[] = $childNode;
34
                }
35 1
                continue;
36
            }
37
38 5
            $flatten[] = $child;
39
        }
40
41 5
        $node->nodes = $this->mergeRaw($flatten);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->mergeRaw($flatten) of type array is incompatible with the declared type Spiral\Stempler\Node\HTML\list of property $nodes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
43 5
        return null;
44
    }
45
46 14
    public function leaveNode(mixed $node, VisitorContext $ctx): mixed
47
    {
48 14
        return null;
49
    }
50
51 5
    private function mergeRaw(array $nodes): array
52
    {
53 5
        $result = [];
54 5
        foreach ($nodes as $node) {
55
            if (
56 5
                $node instanceof Raw
57 5
                && isset($result[\count($result) - 1])
58 5
                && $result[\count($result) - 1] instanceof Raw
59
            ) {
60 1
                $result[\count($result) - 1]->content .= $node->content;
61 1
                continue;
62
            }
63
64 5
            $result[] = $node;
65
        }
66
67 5
        return $result;
68
    }
69
}
70