HtmlBuilder::__call()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 21
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\htmltag;
6
7
use drupol\htmltag\Tag\TagFactory;
8
use drupol\htmltag\Tag\TagInterface;
9
10
/**
11
 * Class HtmlBuilder.
12
 */
13
final class HtmlBuilder implements StringableInterface
14
{
15
    /**
16
     * The tag scope.
17
     *
18
     * @var \drupol\htmltag\Tag\TagInterface|null
19
     */
20
    private $scope;
21
22
    /**
23
     * The storage.
24
     *
25
     * @var \drupol\htmltag\Tag\TagInterface[]|string[]
26
     */
27
    private $storage;
28
29
    public function __call($name, array $arguments = [])
30 1
    {
31
        if ('c' === $name) {
32 1
            if (!isset($arguments[0])) {
33 1
                return $this;
34 1
            }
35
36
            return $this->update(
37 1
                HtmlTag::tag('!--', [], $arguments[0])
38 1
            );
39
        }
40
41
        if ('_' === $name) {
42 1
            $this->scope = null;
43 1
44
            return $this;
45 1
        }
46
47
        $tag = TagFactory::build($name, ...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $attributes of drupol\htmltag\Tag\TagFactory::build() does not expect variable arguments. ( Ignorable by Annotation )

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

47
        $tag = TagFactory::build($name, /** @scrutinizer ignore-type */ ...$arguments);
Loading history...
48 1
49
        return $this->update($tag, true);
50 1
    }
51
52
    public function __toString(): string
53
    {
54
        $output = '';
55
56 1
        foreach ($this->storage as $item) {
57
            $output .= $item;
58 1
        }
59
60 1
        return $output;
61 1
    }
62
63
    /**
64 1
     * Add the current tag to the stack.
65
     *
66
     * @param \drupol\htmltag\Tag\TagInterface $tag
67
     *   The tag
68
     * @param bool $updateScope
69
     *   True if the current scope needs to be updated
70
     *
71
     * @return \drupol\htmltag\HtmlBuilder
72
     *   The HTML Builder object
73
     */
74
    private function update(TagInterface $tag, $updateScope = false)
75
    {
76
        if (null !== $this->scope) {
77
            $this->scope->content($this->scope->getContentAsArray(), $tag);
78 1
        } else {
79
            $this->storage[] = $tag;
80 1
        }
81 1
82
        if (true === $updateScope) {
83 1
            $this->scope = $tag;
84
        }
85
86 1
        return $this;
87 1
    }
88
}
89