Passed
Pull Request — master (#50)
by Marco
02:46
created

MarkdownPipedToSymfonyConsoleFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B 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 str_replace;
14
use function trim;
15
16
final class MarkdownPipedToSymfonyConsoleFormatter implements OutputFormatter
17
{
18
    /** @var OutputInterface */
19
    private $output;
20
21
    public function __construct(OutputInterface $output)
22
    {
23
        $this->output = $output;
24
    }
25
26
    public function write(Changes $changes) : void
27
    {
28
        $arrayOfChanges = $changes->getIterator()->getArrayCopy();
29
30
        $this->output->writeln(
31
            "# Added\n"
32
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
33
                function (Change $change) : bool {
34
                    return $change->isAdded();
35
                },
36
                ...$arrayOfChanges
37
            ))
38
            . "\n# Changed\n"
39
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
40
                function (Change $change) : bool {
41
                    return $change->isChanged();
42
                },
43
                ...$arrayOfChanges
44
            ))
45
            . "\n# Removed\n"
46
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
47
                function (Change $change) : bool {
48
                    return $change->isRemoved();
49
                },
50
                ...$arrayOfChanges
51
            ))
52
        );
53
    }
54
55
    /** @return string[] */
56
    private function convertFilteredChangesToMarkdownBulletList(callable $filterFunction, Change ...$changes) : array
57
    {
58
        return array_map(
59
            function (Change $change) : string {
60
                return ' - ' . str_replace(['ADDED: ', 'CHANGED: ', 'REMOVED: '], '', trim((string) $change)) . "\n";
61
            },
62
            array_filter($changes, $filterFunction)
63
        );
64
    }
65
}
66