StringBuffer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 23
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 4 1
A __construct() 0 3 1
A append() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Output;
6
7
use AlecRabbit\Spinner\Core\Output\Contract\IBuffer;
8
use Generator;
9
10
use function is_iterable;
11
12
final class StringBuffer implements IBuffer
13
{
14
    public function __construct(
15
        protected string $buffer = '',
16
    ) {
17
    }
18
19
    public function flush(): Generator
20
    {
21
        yield $this->buffer;
22
        $this->buffer = '';
23
    }
24
25
    public function append(iterable|string $message): IBuffer
26
    {
27
        if (!is_iterable($message)) {
28
            $message = [$message];
29
        }
30
        /** @var string $item */
31
        foreach ($message as $item) {
32
            $this->buffer .= $item;
33
        }
34
        return $this;
35
    }
36
}
37