ScopedContentTrait::append()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
use function array_map;
8
use function array_unshift;
9
use function join;
10
11
trait ScopedContentTrait
12
{
13
    private array $content = [];
14
    private int $emptyLinesBuffer = 0;
15
    protected array $dependencyAwareChildren = [];
16
17
    /**
18
     * @param GeneratorInterface|string ...$values
19
     */
20 15
    public function append(...$values): self
21
    {
22 15
        if (end($values) instanceof BlockInterface) {
23 9
            $this->content[] = [...$values];
24
        } else {
25 13
            $this->content[] = [...$values, ';'];
26
        }
27
28 15
        foreach ($values as $value) {
29 15
            if ($value instanceof DependencyAwareGenerator) {
30 12
                $this->dependencyAwareChildren[] = $value;
31
            }
32
        }
33
34 15
        return $this;
35
    }
36
37
    /**
38
     * @param GeneratorInterface|string ...$values
39
     */
40 1
    public function prepend(...$values): self
41
    {
42 1
        if (end($values) instanceof BlockInterface) {
43 1
            array_unshift($this->content, [...$values]);
44
        } else {
45 1
            array_unshift($this->content, [...$values, ';']);
46
        }
47
48 1
        foreach ($values as $value) {
49 1
            if ($value instanceof DependencyAwareGenerator) {
50 1
                $this->dependencyAwareChildren[] = $value;
51
            }
52
        }
53
54 1
        return $this;
55
    }
56
57 9
    public function emptyLine(): self
58
    {
59 9
        $this->content[] = [];
60
61 9
        return $this;
62
    }
63
64 3
    public function clearContent(): self
65
    {
66 3
        $this->content = [];
67
68 3
        return $this;
69
    }
70
71 36
    protected function generateContent(): string
72
    {
73 36
        $content = '';
74
75 36
        if (!empty($this->content)) {
76 19
            $content = Utils::indent(join(
77 19
                "\n",
78 19
                array_map(fn($line) => join('', $line), $this->content)
79
            ));
80
        }
81
82 36
        return rtrim($content);
83
    }
84
}
85