MarkdownPipedToSymfonyConsoleFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A write() 0 25 1
A convertFilteredChangesToMarkdownBulletList() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\Formatter;
6
7
use Roave\BackwardCompatibility\Change;
8
use Roave\BackwardCompatibility\Changes;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use function array_filter;
11
use function array_map;
12
use function implode;
13
use function iterator_to_array;
14
use function str_replace;
15
use function trim;
16
17
final class MarkdownPipedToSymfonyConsoleFormatter implements OutputFormatter
18
{
19
    /** @var OutputInterface */
20
    private $output;
21
22
    public function __construct(OutputInterface $output)
23
    {
24
        $this->output = $output;
25
    }
26
27
    public function write(Changes $changes) : void
28
    {
29
        $arrayOfChanges = iterator_to_array($changes);
30
31
        $this->output->writeln(
32
            "# Added\n"
33
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
34
                static function (Change $change) : bool {
35
                    return $change->isAdded();
36
                },
37
                ...$arrayOfChanges
38
            ))
39
            . "\n# Changed\n"
40
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
41
                static function (Change $change) : bool {
42
                    return $change->isChanged();
43
                },
44
                ...$arrayOfChanges
45
            ))
46
            . "\n# Removed\n"
47
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
48
                static function (Change $change) : bool {
49
                    return $change->isRemoved();
50
                },
51
                ...$arrayOfChanges
52
            ))
53
        );
54
    }
55
56
    /** @return string[] */
57
    private function convertFilteredChangesToMarkdownBulletList(callable $filterFunction, Change ...$changes) : array
58
    {
59
        return array_map(
60
            static function (Change $change) : string {
61
                return ' - ' . str_replace(['ADDED: ', 'CHANGED: ', 'REMOVED: '], '', trim($change->__toString())) . "\n";
62
            },
63
            array_filter($changes, $filterFunction)
64
        );
65
    }
66
}
67