Scope   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
eloc 24
c 3
b 0
f 0
dl 0
loc 93
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A html() 0 4 1
A __construct() 0 2 1
A createBlock() 0 7 2
A expand() 0 11 5
A build() 0 21 4
1
<?php
2
3
namespace Lagdo\UiBuilder\Builder\Html;
4
5
use AvpLab\Element\Element as Block;
6
7
use function is_a;
8
use function implode;
9
10
class Scope
11
{
12
    /**
13
     * @var array<Block|ElementBlock>
14
     */
15
    protected $blocks = [];
16
17
    /**
18
     * @var array<Element>
19
     */
20
    protected $children = [];
21
22
    /**
23
     * The constructor
24
     *
25
     * @param Element $parent
26
     */
27
    public function __construct(protected Element $parent)
28
    {}
29
30
    /**
31
     * Create the corresponding elements
32
     *
33
     * @param mixed $element
34
     *
35
     * @return void
36
     */
37
    private function expand(mixed $element): void
38
    {
39
        if (is_a($element, Block::class) ||
40
            is_a($element, Element::class)) {
41
            $this->children[] = $element;
42
            return;
43
        }
44
        if (is_a($element, ElementExpr::class)) {
45
            // Recursively expand the children of the ElementExpr element.
46
            foreach ($element->children as $childElement) {
47
                $this->expand($childElement);
48
            }
49
        }
50
    }
51
52
    /**
53
     * @param Element $element
54
     * @param Scope $scope
55
     *
56
     * @return ElementBlock
57
     */
58
    private function createBlock(Element $element, Scope $scope): ElementBlock
59
    {
60
        $block = new ElementBlock($element, $scope->blocks);
61
        foreach ($element->wrappers as $wrapper) {
62
            $block = new ElementBlock($wrapper, [$block]);
63
        }
64
        return $block;
65
    }
66
67
    /**
68
     * @param array $arguments The arguments passed to the element
69
     *
70
     * @return void
71
     */
72
    public function build(array $arguments): void
73
    {
74
        foreach ($arguments as $argument) {
75
            $this->expand($argument);
76
        }
77
78
        foreach ($this->children as $element) {
79
            if (is_a($element, Block::class)) {
80
                // A children of type Block doesn't need any processing.
81
                $this->blocks[] = $element;
82
                continue;
83
            }
84
85
            $element->onBuild($this->parent);
86
87
            // Recursively build the child scope.
88
            $scope = new Scope($element);
89
            $scope->build($element->children);
90
91
            // Generate the corresponding tag.
92
            $this->blocks[] = $this->createBlock($element, $scope);
93
        }
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function html(): string
100
    {
101
        // Merge all the generated tags.
102
        return implode('', $this->blocks);
103
    }
104
}
105