Test Setup Failed
Pull Request — master (#3)
by Timur
05:47 queued 03:49
created

ScopedContentTrait::clearContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
16
    /**
17
     * @param GeneratorInterface|string ...$values
18
     */
19
    public function append(...$values): self
20
    {
21
        if (end($values) instanceof BlockInterface) {
22
            $this->content[] = [...$values];
23
        } else {
24
            $this->content[] = [...$values, ';'];
25
        }
26
27
        foreach ($values as $value) {
28
            if ($value instanceof DependencyAwareGenerator) {
29
                $this->dependencyAwareChildren[] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property dependencyAwareChildren does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
            }
31
        }
32
33
        return $this;
34
    }
35
36
    /**
37
     * @param GeneratorInterface|string ...$values
38
     */
39
    public function prepend(...$values): self
40
    {
41
        if (end($values) instanceof BlockInterface) {
42
            array_unshift($this->content, [...$values]);
43
        } else {
44
            array_unshift($this->content, [...$values, ';']);
45
        }
46
47
        foreach ($values as $value) {
48
            if ($value instanceof DependencyAwareGenerator) {
49
                $this->dependencyAwareChildren[] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property dependencyAwareChildren does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
            }
51
        }
52
53
        return $this;
54
    }
55
56
    public function emptyLine(): self
57
    {
58
        $this->content[] = [];
59
60
        return $this;
61
    }
62
63
    public function clearContent(): self
64
    {
65
        $this->content = [];
66
67
        return $this;
68
    }
69
70
    protected function generateContent(): string
71
    {
72
        $content = '';
73
74
        if (!empty($this->content)) {
75
            $content = Utils::indent(join(
76
                "\n",
77
                array_map(fn($line) => join($line), $this->content)
0 ignored issues
show
Bug introduced by
The call to join() has too few arguments starting with pieces. ( Ignorable by Annotation )

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

77
                array_map(fn($line) => /** @scrutinizer ignore-call */ join($line), $this->content)

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
            ));
79
        }
80
81
        return rtrim($content);
82
    }
83
}
84