Passed
Push — master ( aa3b71...5e2219 )
by Nico
55:07 queued 26:20
created

InformationWrapper::dumpTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Information;
6
7
class InformationWrapper implements InformationInterface
8
{
9
    /** @var array<string> */
10
    private array $informations;
11
12
    /**
13
     * @param array<string> $informations
14
     */
15 36
    public function __construct(array $informations = [])
16
    {
17 36
        $this->informations = $informations;
18
    }
19
20 16
    public function addInformation(?string $information): InformationInterface
21
    {
22 16
        if ($information !== null) {
23 16
            $this->informations[] = $information;
24
        }
25
26 16
        return $this;
27
    }
28
29
    public function addInformationf(string $information, ...$args): InformationInterface
30
    {
31
        $this->addInformation(vsprintf(
32
            $information,
33
            $args
34
        ));
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param array<string> $informations
41
     */
42 7
    public function addInformationArray(array $informations, bool $isHead = false): InformationWrapper
43
    {
44 7
        if ($isHead) {
45 1
            $this->informations = array_merge($informations, $this->informations);
46
        } else {
47 7
            $this->informations = array_merge($this->informations, $informations);
48
        }
49
50 7
        return $this;
51
    }
52
53 2
    public function addInformationWrapper(?InformationWrapper $informations, bool $isHead = false): InformationWrapper
54
    {
55 2
        if ($informations !== null) {
56 2
            $this->addInformationArray($informations->getInformations(), $isHead);
57
        }
58
59 2
        return $this;
60
    }
61
62
    /**
63
     * @return array<string>
64
     */
65 29
    public function getInformations(): array
66
    {
67 29
        return $this->informations;
68
    }
69
70
    public function getInformationsAsString(): string
71
    {
72
        return implode(PHP_EOL, $this->getInformations());
73
    }
74
75
    public function dumpTo(InformationInterface $informations): void
76
    {
77
        array_walk(
78
            $this->informations,
79
            function (string $info) use ($informations): void {
80
                $informations->addInformation($info);
81
            }
82
        );
83
    }
84
85 4
    public function isEmpty(): bool
86
    {
87 4
        return empty($this->getInformations());
88
    }
89
}
90